0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-24 07:28:48 +00:00
OI-codes/S2OJ/165/165.cpp

29 lines
616 B
C++
Raw Normal View History

#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;
}