Pagini recente » Cod sursa (job #2093980) | Cod sursa (job #2461700)
#include<fstream>
#include<iostream>
#include<vector>
using namespace std;
string a,b,concat;
int z[4000010];
int main()
{
ifstream t1("strmatch.in");
ofstream t2("strmatch.out");
t1>>a;
t1>>b;
concat=a+'!'+b;
cout<<concat<<'\n';
int len=a.length();
int i,l,r;
l=r=0;
int sol=0;
vector<int> poz;
int conlen=concat.length();
for(i=1;i<conlen;i++)
{
if(i>r)
{
l=r=i;
while(r<conlen && concat[r]==concat[r-l])
r++;
z[i]=r-l;
r--;
}
else
{
int k=i-l;
if( z[k] <r-i+1)
{
z[i]=z[k];
}
else
{
l=i;
while(r<conlen && concat[r]==concat[r-l])
r++;
z[i]=r-l;
r--;
}
}
if(z[i]==len)
{
sol++;
poz.push_back(i-len-1);
}
}
t2<<sol<<'\n';
for(i=0;i<min(1000,(int)poz.size());i++)
{
t2<<poz[i]<<' ';
}
t2<<'\n';
t1.close();
t2.close();
return 0;
}