Pagini recente » Cod sursa (job #1028857) | Cod sursa (job #722157) | Cod sursa (job #2556356) | Cod sursa (job #3184628) | Cod sursa (job #3225375)
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("strmatch.in");
ofstream fout ("strmatch.out");
int lps[2000005];
vector <int> ap;
int main()
{
fin.tie(0); fin.sync_with_stdio(false);
string pre; fin>>pre;
string s; fin>>s;
int match=0, index=1;
lps[0]=0;
while (index<(int)pre.size()) {
//cout<<pre[index]<<' '<<pre[match]<<endl;
if (pre[index]==pre[match]) {
match++;
lps[index]=match;;
index++;
}
else {
if (match==0) index++;
else match=lps[match-1];
}
}
for (int i=0; i<(int)pre.size(); i++) cout<<lps[i]<<' ';
index=0; match=0;
while (index<(int)s.size()) {
if (s[index]==pre[match]) {
index++;
match++;
if (match==(int)pre.size()) {
ap.push_back(index-match);
match=lps[match-1];
}
}
else {
if (match==0) index++;
else match=lps[match-1];
}
}
fout<<ap.size()<<'\n';
for (int i=0; i<min((int)ap.size(), 1000); i++) {
fout<<ap[i]<<' ';
}
return 0;
}