Pagini recente » Cod sursa (job #634605) | Cod sursa (job #348419) | Cod sursa (job #414156) | Cod sursa (job #601258) | Cod sursa (job #2112275)
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
ifstream f("strmatch.in");
ofstream g("strmatch.out");
string a,b;
int urm[2000000];
int n,m;
void urma()
{
int k=-1;
urm[0]=0;
for(int x=1;x<m;x++)
{
while(k>0 && a[k+1]!=a[k]) k=urm[k];
if(a[k+1]==a[k]) k++;
urm[x]=k;
}
}
int main()
{
int x=-1;
f>>a;
f>>b;
n=b.length;
m=a.length;
g<<a<<"\n"<<b;
urma();
for(int i=0;i<n;i++)
{
while(x>0&&a[x+1]!=b[i]) x=urm[x];
if(a[x+1]==b[i]) x++;
if(x==m-1)
{
g<<i-m+1;
x=urm[x];
}
}
return 0;
}