Pagini recente » Cod sursa (job #2765877) | Cod sursa (job #1715318) | Cod sursa (job #2868833) | Cod sursa (job #2837070) | Cod sursa (job #977937)
Cod sursa(job #977937)
#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;
}
}
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(); int q = 0;
for(int i=1; i<=n; i++)
{
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);
}
}
fout<<sol<<'\n';
if(sol > 1000) sol = 1000;
for(unsigned i=0; i<sol; i++)
fout<<poz[i]<<' ';
return 0;
}