求解题思路
感觉采用枚举法效率太低,应该还有更好的方法,不求源码,只求解题关键思路。
KMP
wabdefgabcdefgabcdefgabcdefgabcde 33 efgabcdefghabc 14 efgabcdefghabcefg 14 aaaaabaaaaabaaaaabaa 6 abcdeabcdeabcde 5 efgabcdefgabcde 7 bcabcab 3 AAAAAAAAAAAAAAAAAAAAAAAAA 1 AAAAB 5 ABCED 5 AAAABBBBBBBBBBBBBBBBB 21 AAAABB 6 AABBAA 4 AAAABBAAAAAA 12 ABABABABABAB 2 abababa 2 ababag 6 bcabcabcabcab 3 这么多数据测试都通过了,为什么还显示wa啊???
附源码: #define HUST_1010 #ifdef HUST_1010 #include <iostream> #include<string.h> #include<stdio.h> using namespace std; #define N 1000002 char B[N]; int nextx[N]; void getnext(int next[],char s[],int l) { int i=1,j=0; next[1]=0; while(i<l) { if(j==0 || s[i]==s[j]) { i++;j++; next[i]=j; } else { j=next[j]; } } } int main() { freopen(\"TEST\\\\1010-input.txt\",\"r\",stdin); freopen(\"TEST\\\\1010-output.txt\",\"w\",stdout); int len; int sum; while(scanf(\"%s\",B+1)!=EOF) { sum = 0; len = strlen(B+1); if(len == 1) printf(\"1\\n\"); else { getnext(nextx,B,len); sum = len - nextx[len]; if( (nextx[len] != 1) && ( (B[len] == B[len%sum]) || (len%sum == 0 && (B[len] == B[sum]) ) ) ) printf(\"%d\\n\",sum); else printf(\"%d\\n\",len); } } return 0; } #endif
已结。非常感谢Lucifer的帮助!