正文
增加 12 行删除 12 行
43 行未改动44454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001014 行未改动43 行未改动}void f(char t[], int m){ans[0] = 0;int j = 0;for(int i = 1; i < m; i++){while(j && t[i] != t[j]){j = ans[j - 1];}if(t[i] == t[j]){j++;}ans[i] = j;}}int kmp(char a[], int n, char b[], int m){if(m == 0) return 0;f(b, m);int j = 0;for(int i = 0; i < n; i++){while(j && a[i] != b[j]){j = ans[j - 1];}if(a[i] == b[j]){j++;}if(j == m){return i - m + 2;}}return 0;}signed main(){ios::sync_with_stdio(false);cin.tie(0), cout.tie(0);cin >> l >> s;int n = strlen(l), m = strlen(s);for(int i = 0; i < m; i++){p[i] = match(s[i]);}cout << kmp(l, n, p, m);return 0;}```4 行未改动43 行未改动44454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001014 行未改动43 行未改动}void f(char t[], int m){ans[0] = 0;int j = 0;for(int i = 1; i < m; i++){while(j && t[i] != t[j]){j = ans[j - 1];}if(t[i] == t[j]){j++;}ans[i] = j;}}int kmp(char a[], int n, char b[], int m){if(m == 0) return 0;f(b, m);int j = 0;for(int i = 0; i < n; i++){while(j && a[i] != b[j]){j = ans[j - 1];}if(a[i] == b[j]){j++;}if(j == m){return i - m + 2;}}return 0;}signed main(){ios::sync_with_stdio(false);cin.tie(0), cout.tie(0);cin >> l >> s;int n = strlen(l), m = strlen(s);for(int i = 0; i < m; i++){p[i] = match(s[i]);}cout << kmp(l, n, p, m);return 0;}```4 行未改动