pku2406kmp

来源:互联网 发布:韩国美臀小姐走红网络 编辑:程序博客网 时间:2024/06/12 00:36
 

题目来源:http://poj.org/problem?id=2406

题目分类:KMP next数组

此题心得:加深了对next数组的理解

作者:王耀宣

时间:2011-7-21

Power Strings

Time Limit: 3000MS

 

Memory Limit: 65536K

Total Submissions: 17868

 

Accepted: 7460

Description

Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcdef". If we think of concatenation as multiplication, exponentiation by a non-negative integer is defined in the normal way: a^0 = "" (the empty string) and a^(n+1) = a*(a^n).

Input

Each test case is a line of input representing s, a string of printable characters. The length of s will be at least 1 and will not exceed 1 million characters. A line containing a period follows the last test case.

Output

For each s you should print the largest n such that s = a^n for some string a.

Sample Input

abcd
aaaa
ababab
.

Sample Output

1
4
3

Hint

This problem has huge input, use scanf instead of cin to avoid time limit exceed.

Source

Waterloo local 2002.07.01

[Submit]   [Go Back]   [Status]   [Discuss]

Home Page    Go Back   To top


All Rights Reserved 2003-2011 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator

 

题意与分析

题目:给你一个串,让你在这个串当中找到一个最长的前缀串,保证这个串是其某一个前缀s的k倍,也就是由几个s连接起来构成,求这个k的值最大能多大。

分析:其实就是利用了KMP的next数组的性质。对每个位置,next数组都指向前一个可能匹配的位置。对i处,next[i]指向的位置作为结尾的就能匹配以i为结尾的前缀的有用部分。 如果一个串能被划分成几个字串的连接,则next[len]一定是指向倒数第二个的位置,那么只有len%(len-next[len]为0时才能保证是由一个串不相交连接而成的。。。

源代码

·                //pku 2046
·                #include<iostream>
·                using namespace std;
·                 
·                const int N=5000100;
·                char s[N];
·                int a[N], next[N], len, n, m, flag;
·                 
·                void getnext(char *s)
·                {
·                      int j, k;
·                      j = 0;
·                      k = -1;
·                      next[0] = -1;
·                      while(j<len)
·                      {
·                            if(k==-1 || s[j]==s[k])
·                            {
·                                  j++;
·                                  k++;
·                                  next[j] = k;
·                            }
·                            else
·                                  k = next[k];
·                      }
·                }
·                 
·                int main()
·                {
·                      int i, j, k, cas, cas1=1;
·                      int ans;
·                      while(scanf("%s", s)!=EOF)
·                      {
·                            if(s[0]=='.')
·                                  break;
·                            ans = 0;
·                            len = strlen(s);
·                            getnext(s);
·                            //for(i=0; i<=len; i++)
·                            //    printf("%d ", next[i]);
·                            //printf("\n");
·                            k = len;
·                            ans = 1;
·                            if(len%(len-next[len])==0)
·                                  ans = len / (len-next[len]);
·                            printf("%d\n", ans);
·                      }
·                      
·                      return 0;
·                }

                                            

原创粉丝点击