Pagini recente » Cod sursa (job #2367694) | Cod sursa (job #933592) | Cod sursa (job #3253654) | Cod sursa (job #1600718) | Cod sursa (job #928291)
Cod sursa(job #928291)
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
#define NM 2000005
#define PB push_back
int p[NM], N, M;
string S, T;
vector <int> sol;
void compute_prefix()
{
p[0] = -1;
p[1] = 0;
for (int i = 2; i <= N; ++i)
{
int x = i-1;
while (x >= 1 && S[p[x]] != S[i-1]) x = p[x];
p[i] = p[x] + 1;
}
}
void apply_kmp()
{
int x = 0, s = 0;
while (s < M)
{
if (T[s + x] == S[x])
{
x++;
if (x == N)
{
//cout << s << " ";
sol.PB (s);
s = s + x - p[x];
x = p[x];
}
continue;
}
else
{
while (x >= 1 && T[s + x] != S[x]) x = p[x];
s = s + x - p[x];
}
}
}
int main()
{
ifstream f ("strmatch.in");
ofstream g ("strmatch.out");
f >> S >> T;
N = S.length();
M = T.length();
compute_prefix();
//for (int i = 0; i <= N; ++i) cout << i << " -> " << p[i] << endl;
apply_kmp();
g << sol.size() << '\n';
for (unsigned int i = 0; i < sol.size(); ++ i) g << sol[i] << " ";
return 0;
}