mirror of
https://git.sb/baoshuo/OI-codes.git
synced 2024-12-24 18:52:02 +00:00
parent
4850a27856
commit
aad9d251b1
28
S2OJ/165/165.cpp
Normal file
28
S2OJ/165/165.cpp
Normal file
@ -0,0 +1,28 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
using std::cin;
|
||||
using std::cout;
|
||||
const char endl = '\n';
|
||||
|
||||
int nxt[1000005];
|
||||
std::string s, p;
|
||||
|
||||
int main() {
|
||||
cin >> s >> p;
|
||||
nxt[0] = -1;
|
||||
for (int i = 1, j = -1; i < p.size(); i++) {
|
||||
while (j >= 0 && p[j + 1] != p[i]) j = nxt[j];
|
||||
if (p[j + 1] == p[i]) j++;
|
||||
nxt[i] = j;
|
||||
}
|
||||
for (int i = 0, j = -1; i < s.size(); i++) {
|
||||
while (j != -1 && s[i] != p[j + 1]) j = nxt[j];
|
||||
if (s[i] == p[j + 1]) j++;
|
||||
if (j == p.size() - 1) {
|
||||
cout << i - j + 1 << endl;
|
||||
j = nxt[j];
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user