Pagini recente » Cod sursa (job #1385658) | Cod sursa (job #1546816) | Cod sursa (job #1637607) | Cod sursa (job #1574101) | Cod sursa (job #977983)
Cod sursa(job #977983)
#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
using namespace std;
ifstream fin("strmatch.in");
ofstream fout("strmatch.out");
const int N = 2000110;
char P[N], T[N];
int n, m, sol, pi[N];
vector <int> poz;
void Prefix()
{
int k = 0; pi[1] = 0;
for(int q=2; q<=m; q++)
{
while(k && P[k+1] != P[q])
k = pi[k];
if(P[k+1] == P[q]) k++;
pi[q] = k;
}
}
void Compute()
{
int q = 0;
for(int i=1; i<=n; i++)
{
cout<<q<<endl;
while(q && P[q+1] != T[i])
q = pi[q];
if(P[q+1] == T[i]) q++;
if(q == m)
{
sol++;
q = pi[q];
if(sol <= 1000) poz.push_back(i-m);
}
}
}
int main()
{
fin.getline(P, N); fin.getline(T, N);
m = strlen(P), n = strlen(T);
for(int i=n; i>0; i--) T[i] = T[i-1]; T[0] =' ';
for(int i=m; i>0; i--) P[i] = P[i-1]; P[0] =' ';
Prefix(); Compute();
fout<<sol<<'\n';
sol = min(sol, 1000);
for(unsigned i=0; i<sol; i++)
fout<<poz[i]<<' ';
return 0;
}