poj2406Power Strings 回文数,字符匹配

都说是用KMP,粗人,我就暴力一点过了吧

 1 #include <iostream>
 2 using namespace std;
 3 char str[100000005];
 4 int main()
 5 {
 6     int i, k, p, len;
 7     while (scanf("%s", str) != EOF && str[0] != '.')
 8     {
 9         len = strlen(str);
10         p = k = 1;
11         for (i=0; i<len; i++)
12         {
13             if (str[i] != str[i % p ])
14             {
15                 if (p == k) //若上次匹配失败的话就要加1
16                     k++;
17                 p=k;
18             }
19             else
20                 k++;
21         }
22         if (len % p)
23             puts("1");
24         else
25             printf("%d\n",len/p);
26     }
27     return 0;
28 }

附点测试数据:

abcd
abaabaabaaba
abaabaabaabac
abaabacabaabac
abaabacabaabacd
aabaabaa
aabaabaaba

posted @ 2013-05-13 11:00  旅行的蜗牛  阅读(154)  评论(0编辑  收藏  举报