FZU - 2128 最長子串(KMP?)
阿新 • • 發佈:2018-11-22
問題很簡單,給你一個字串s,問s的子串中不包含s1,s2...sn的最長串有多長。
Input
輸入包含多組資料。第一行為字串s,字串s的長度1到10^6次方,第二行是字串s不能包含的子串個數n,n<=1000。接下來n行字串,長度不大於100。
字串由小寫的英文字元組成。
Output 最長子串的長度 Sample Inputlgcstraightlalongahisnstreet 5 str long tree biginteger ellipseSample Output
12
聽說strstr比kmp還快……但是權當把這道題練手kmp了
#include <stdio.h> #include <string.h> #include <algorithm> #include <iostream> using namespace std; const int maxn = 1e6 + 5; int st[maxn], ed[maxn], tol = 0; int Next[105]; void kmpinit(char x[], int m) { int j = -1, i = 0; Next[0] = -1; while (i < m) { while (j != -1 && x[i] != x[j]) j = Next[j]; Next[++i] = ++j; } } void kmp(char x[], int m, char y[], int n) { int i = 0, j = 0; kmpinit(x, m); while (i<n) { while (j != -1 && x[j] != y[i]) j = Next[j]; ++i; ++j; if (j >= m) { st[tol] = i - m; ed[tol] = i - 1; tol++; j = Next[j]; } } } char s[maxn], s1[105]; int main() { while (scanf("%s", s) != EOF) { int n; scanf("%d", &n); tol = 0; for (int i = 0; i < n; i++) { scanf("%s", s1); kmp(s1, strlen(s1), s, strlen(s)); } int ans = 0; st[tol] = ed[tol] = 0; sort(st, st + tol+1); sort(ed, ed + tol+1); st[0] = -1; ed[++tol] = strlen(s); for (int i = 1; i <= tol; i++) ans = max(ans, ed[i] - st[i - 1] - 1); cout << ans << endl; } return 0; }