Pagini recente » Cod sursa (job #447669) | Cod sursa (job #839850) | Cod sursa (job #1747722) | Cod sursa (job #2825585) | Cod sursa (job #2498559)
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
ifstream f ("strmatch.in");
ofstream g ("strmatch.out");
const int NMAX = 2000001;
int lgT, lgP;
char T[NMAX], P[NMAX];
int pi[NMAX], pos[1001];
void prefix()
{
int k = 0;
pi[0] = 0;
for (int i = 1; i < lgP; i++)
{
while (k && P[i] != P[k])
k = pi[k - 1];
if (P[k] == P[i])
k++;
pi[i] = k;
}
}
int main()
{
int k = 0, n = 0;
f.getline (P, NMAX);
lgP = f.gcount() - 1;
f.getline (T, NMAX);
lgT = f.gcount() - 1;
prefix();
for (int i = 0; i < lgT; i++)
{
while (k > 0 && P[k] != T[i])
k = pi[k - 1];
if (P[k] == T[i])
k++;
if (k == lgP)
{
k = pi[k-1];
n++;
pos[n] = i - lgP +1;
}
}
cout << n << '\n';
for (int i = 1; i <= n; i++)
cout << pos[i] << ' ';
return 0;
}