【POJ11855】 Buzzwords (后缀数组)
Description
The word “the” is the most common
three-letter word. It even
shows up inside other words, such
as “other” and “mathematics”.
Sometimes it hides, split between
two words, such as “not here”.
Have you ever wondered what the
most common words of lengths
other than three are?
Your task is the following. You
will be given a text. In this text,
find the most common word of
length one. If there are multiple
such words, any one will do. Then
count how many times this most
common word appears in the text. If it appears more than once, output how many times it appears.
Then repeat the process with words of length 2, 3, and so on, until you reach such a length that
there is no longer any repeated word of that length in the text.Input
The input consists of a sequence of lines. The last line of input is empty and should not be processed.
Each line of input other than the last contains at least one but no more than one thousand uppercase
letters and spaces. The spaces are irrelevant and should be ignored.Output
For each line of input, output a sequence of lines, giving the number of repetitions of words of length
1, 2, 3, and so on. When you reach a length such that there are no repeated words of that length,
output one blank line, do not output anything further for that input line, and move on to the next line
of input.
Note: Remember that the last line of the sample input and of the sample output must be blank.
Sample Input
THE OTHER MATHEMATICS NOT HERE
AA
Sample Output
5
4
4
2
2
2
【题意】
给定一个文本,求出长度为1, 2, 3, 4, 5....的字符串最大出现次数,一直找到出现次数不大于1为止。
【分析】
直接for两遍。按枚举的长度分组,求出小组成员个数的max即可。
代码如下:
1 #include<cstdio> 2 #include<cstdlib> 3 #include<cstring> 4 #include<iostream> 5 #include<algorithm> 6 #include<queue> 7 using namespace std; 8 #define Maxl 100010 9 #define INF 0xfffffff 10 11 int l,len; 12 char s[Maxl]; 13 int c[Maxl],cl; 14 15 int mymin(int x,int y) {return x<y?x:y;} 16 int mymax(int x,int y) {return x>y?x:y;} 17 18 void init() 19 { 20 len=strlen(s); 21 cl=0; 22 for(int i=0;i<len;i++) if(s[i]!=' ') 23 c[++cl]=s[i]-'A'+1; 24 } 25 26 int sa[Maxl],rk[Maxl],y[Maxl],wr[Maxl],Rs[Maxl]; 27 void get_sa(int m) 28 { 29 memcpy(rk,c,sizeof(rk)); 30 for(int i=0;i<=m;i++) Rs[i]=0; 31 for(int i=1;i<=cl;i++) Rs[rk[i]]++; 32 for(int i=1;i<=m;i++) Rs[i]+=Rs[i-1]; 33 for(int i=cl;i>=1;i--) sa[Rs[rk[i]]--]=i; 34 35 int ln=1,p=0; 36 while(p<cl) 37 { 38 int k=0; 39 for(int i=cl-ln+1;i<=cl;i++) y[++k]=i; 40 for(int i=1;i<=cl;i++) if(sa[i]>ln) y[++k]=sa[i]-ln; 41 for(int i=1;i<=cl;i++) wr[i]=rk[y[i]]; 42 43 for(int i=0;i<=m;i++) Rs[i]=0; 44 for(int i=1;i<=cl;i++) Rs[wr[i]]++; 45 for(int i=1;i<=m;i++) Rs[i]+=Rs[i-1]; 46 for(int i=cl;i>=1;i--) sa[Rs[wr[i]]--]=y[i]; 47 48 for(int i=1;i<=cl;i++) wr[i]=rk[i]; 49 for(int i=cl+1;i<=cl+ln;i++) wr[i]=0; 50 p=1,rk[sa[1]]=1; 51 for(int i=2;i<=cl;i++) 52 { 53 if(wr[sa[i]]!=wr[sa[i-1]]||wr[sa[i]+ln]!=wr[sa[i-1]+ln]) p++; 54 rk[sa[i]]=p; 55 } 56 ln*=2,m=p; 57 } 58 sa[0]=rk[0]=0; 59 } 60 61 int height[Maxl]; 62 void get_he() 63 { 64 int k=0; 65 for(int i=1;i<=cl;i++) if(rk[i]!=1) 66 { 67 int j=sa[rk[i]-1]; 68 if(k) k--; 69 while(c[i+k]==c[j+k]&&i+k<=cl&&j+k<=cl) k++; 70 height[rk[i]]=k; 71 } 72 } 73 74 void ffind() 75 { 76 for(int i=1;i<=cl;i++)//枚举长度i 77 { 78 int cnt=0,ans=0; 79 for(int j=1;j<=cl;j++) 80 { 81 cnt++; 82 if(height[j+1]<i||j==cl)//是一组的结束 83 { 84 if(cnt!=1) ans=mymax(ans,cnt); 85 cnt=0; 86 } 87 } 88 if(ans<=1) break; 89 printf("%d\n",ans); 90 } 91 } 92 93 int main() 94 { 95 bool ok=0; 96 while(gets(s)) 97 { 98 if(ok) printf("\n"); 99 ok=1; 100 init(); 101 get_sa(30); 102 get_he(); 103 ffind(); 104 } 105 return 0; 106 }
2016-07-19 16:31:07