Pagini recente » Cod sursa (job #1298728) | Cod sursa (job #408372) | Cod sursa (job #1044736) | Cod sursa (job #2100552) | Cod sursa (job #2144467)
#include <iostream>
#include <cstring>
#include <fstream>
#include <vector>
using namespace std;
#define nmax 2000005
ifstream f("strmatch.in");
ofstream g("strmatch.out");
vector<int>stk;
char a[nmax],b[nmax];
int preKMP[nmax];
int len1,len2;
void read()
{
f.getline(a,nmax);
f.getline(b,nmax);
len1=strlen(a);
len2=strlen(b);
}
void prekmp()
{
int j=0;
for (int i=1; i<len1; ++i)
{
while (j>0&&a[j]!=a[i])
{
j=preKMP[j-1];
}
if (a[j]==a[i])
++j;
preKMP[i]=j;
}
}
void kmp()
{
int j=0;
for (int i=0; i<len2; ++i)
{
while (j>0&&a[j]!=b[i])
{
j=preKMP[j-1];
}
if (b[i]==a[j])
++j;
if (j==len1)
{
if (stk.size()==1000)
{
i=len2;
break;
}
stk.push_back(i-len1+1);
}
}
g<<stk.size()<<'\n';
for (auto w:stk)
g<<w<<' ';
}
void solve()
{
prekmp();
kmp();
}
int main()
{
read();
solve();
return 0;
}