Pagini recente » Cod sursa (job #871898) | Cod sursa (job #1702105) | Cod sursa (job #184866) | Cod sursa (job #2065895) | Cod sursa (job #3149992)
#include <bits/stdc++.h>
using namespace std;
#define cin fin
#define cout fout
ifstream fin("strmatch.in");
ofstream fout("strmatch.out");
int main(){
string a, b;
vector<int> pos, aux;
cin >> a >> b;
aux.resize(a.size());
aux[0] = -1;
for(int i = 1, j = -1; i < aux.size(); i++){
aux[i] = -1;
if(a[i] == a[j + 1]){
j++;
aux[i] = j;
}else{
while(j != -1 && a[i] != a[j + 1]){
j = aux[j];
}
if(a[i] == a[j + 1]){
j++;
aux[i] = j;
}
}
}
for(int i = 0, j = -1, enterAux; i < b.size() && pos.size() < 1000; i++){
enterAux = 1;
if(b[i] == a[j + 1]){
j++;
enterAux = 0;
if(pos.size() < 1000 && j == a.size() - 1){
pos.push_back(i - j);
j = aux[j];
enterAux = 1;
}
}
while(enterAux && j != -1 && b[i] != a[j + 1]){
j = aux[j];
}
if(b[i] == a[j + 1]){
j++;
}
}
cout << pos.size() << '\n';
for(auto& e : pos)
{
cout << e << ' ';
}
return 0;
}