poj 2046Power Strings

View Code
 1 #include<stdio.h>
 2 #include<string.h>
 3 char str[1000010];
 4 int next[1000010];
 5 int get_next(int x)
 6 {
 7     int i=0,j=-1;
 8     next[0]=-1;
 9     while(i<x)
10     {
11         if(j==-1||str[i]==str[j])
12         {
13             i++;
14             j++;
15             if(str[i]!=str[j])
16             next[i]=j;
17             else next[i]=next[j];
18         }
19         else j=next[j];
20     }
21     return next[x];
22     
23 }
24 int main()
25 {
26     int l,len;
27     while(~scanf("%s",str),str[0]!='.')
28     {
29         len=strlen(str);
30         l=len-get_next(len);
31         if(len%l) printf("1\n");
32         else printf("%d\n",len/l);
33         
34     }
35 }

 

水题一枚,暴力!

http://poj.org/problem?id=2406

View Code
 1 #include<stdio.h>
 2 #include<string.h>
 3 int main()
 4 {
 5     char str[1000010]; 
 6     char str2[1000010];
 7     int n;//字符串长度 
 8     int i,j,k;//循环子串计数、母串计数器 
 9     while(~scanf("%s",str),str[0]!='.')
10     {
11         k=0;
12         
13         strcpy(str2,str);
14         n=strlen(str);
15         for(i=1;i<=n;i++)//循环节为i 
16         {
17             j=0;
18             if(n%i==0)//不能整除排除 
19             {
20                 k=i;
21                 while(k<n) 
22                 {
23                     if(j==i) j=0;
24                     if(str[k]==str2[j])
25                     {
26                         k++;
27                         j++;
28                     }
29                     else break;
30                 }
31                 if(k==n) break;
32             } 
33         }  
34         printf("%d\n",n/i);
35     }
36 }

 

posted on 2012-08-30 12:08  仁者无敌8勇者无惧  阅读(156)  评论(0编辑  收藏  举报

导航