这题数据水的一B。直接暴力都能够过。
比赛的时候暴力过的。回头依照正法做了一发。
匹配的时候 失配函数 事实上就是前缀 后缀的匹配长度,之后就是乱搞了。
KMP的题可能不会非常直接的出,可是KMP的思想常常渗透在非常多题目里面,近期须要多练习一下。
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn = 1000005; int _next[maxn],L; char s[maxn]; void kmp_init(){ int i,j; int m = strlen(s); j = _next[0] = -1; i = 0; while(i < m){ while(j != -1 && s[i] != s[j]) j = _next[j]; _next[++i] = ++j; } } bool kmp_solve(int len){ int i,j,m = L - len; i = len; j = 0; while(i < m){ while(j != -1 && s[j] != s[i]) j = _next[j]; i++; j++; if(j >= len){ return true; } } return false; } int main(){ int T; scanf("%d",&T); while(T--){ scanf("%s",s); L = strlen(s); kmp_init(); int i = L - 1; int ans = 0; while(_next[i] >= 0){ if(kmp_solve(_next[i] + 1)){ ans = _next[i] + 1; break; } i = _next[i]; } printf("%d ",ans); } return 0; }