Power Strings
Time Limit: 1000ms Memory limit: 65536K 有疑問。點這裡^_^
題目描述 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).
輸入 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.
輸出 For each s you should print the largest n such that s = a^n for some string a.
樣本輸入
abcdaaaaababab.
樣本輸出
143
提示 This problem has huge input, use scanf instead of cin to avoid time limit exceed.
來源
樣本程式
一道KMP的變形問題。大體意思是判斷字串最多由幾部分重複串組成。
多思考,一遍有一遍的收穫。
#include<stdio.h>#include<string.h>char str[1000010];int next[1000010],len;void getnext()// 算是KMP得到next[]的模版{ int i = 0,j = -1; next[0] = -1; while(i < len) { if(j == -1 || str[i] == str[j]) { i++; j++; next[i]=j; } else j = next[j]; }}int main(){ while(scanf("%s",str)!=EOF) { if(str[0] == '.') break; len = strlen(str); getnext(); if((len % (len - next[len]))==0 )//關鍵,仔細領悟 printf("%d\n",len / (len - next[len])); else printf("1\n"); } return 0;}