mirror of
https://git.sb/baoshuo/OI-codes.git
synced 2024-11-08 12:58:48 +00:00
NC232778 [模板]KMP字符串匹配
https://ac.nowcoder.com/acm/contest/view-submission?submissionId=50597340
This commit is contained in:
parent
b639acb766
commit
b99b79dfe4
37
NowCoder/232778/232778.cpp
Normal file
37
NowCoder/232778/232778.cpp
Normal file
@ -0,0 +1,37 @@
|
||||
#pragma GCC optimize("Ofast")
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
using std::cin;
|
||||
using std::cout;
|
||||
#define endl '\n'
|
||||
|
||||
const int N = 1000005;
|
||||
|
||||
int f[N], next[N];
|
||||
std::string s1, s2;
|
||||
|
||||
int main() {
|
||||
std::ios::sync_with_stdio(false);
|
||||
cin >> s1 >> s2;
|
||||
next[0] = -1;
|
||||
for (int i = 1, j = -1; i < s2.size(); i++) {
|
||||
while (j != -1 && s2[i] != s2[j + 1]) j = next[j];
|
||||
if (s2[i] == s2[j + 1]) j++;
|
||||
next[i] = j;
|
||||
}
|
||||
for (int i = 0, j = -1; i < s1.size(); i++) {
|
||||
while (j != -1 && (j == s2.size() || s1[i] != s2[j + 1])) j = next[j];
|
||||
if (s1[i] == s2[j + 1]) j++;
|
||||
if (j == s2.size() - 1) {
|
||||
cout << i - j + 1 << endl;
|
||||
f[i] = j;
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < s2.size(); i++) {
|
||||
cout << next[i] + 1 << ' ';
|
||||
}
|
||||
cout << endl;
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user