Pagini recente » Cod sursa (job #787396) | Cod sursa (job #2953802) | Cod sursa (job #2832103) | Cod sursa (job #628993) | Cod sursa (job #1096740)
/* Potrivirea sirurilor algoritmul KMP */
#include <fstream>
#include <string>
#include <vector>
using namespace std;
#define MAX 2000001
ifstream f("strmatch.in");
ofstream g("strmatch.out");
vector<int>Sol;
string a, b;
int Pre[MAX];
void Prefix()
{
for (int i = 2, K = 0; i < a.length(); i++)
{
if (a[K + 1] == a[i])
{
K++;
Pre[i] = K;
}
else
{
K = Pre[K];
}
}
}
void KMP()
{
for (int i = 1, K = 0; i < b.length(); i++)
{
if (a[K + 1] == b[i])
{
K++;
if (K == a.length() - 1)
{
if (Sol.size() < 1000)
{
Sol.push_back(i - a.length() + 1);
}
K = Pre[K];
}
}
else
{
K = Pre[K];
}
}
}
int main()
{
f >> a;
f >> b;
a = " " + a;
b = " " + b;
Prefix();
KMP();
g << Sol.size() << "\n";
for (int i = 0; i < Sol.size(); i++)
{
g << Sol[i] << " ";
}
}