Pagini recente » Cod sursa (job #1432033) | Cod sursa (job #1425760) | Cod sursa (job #6773) | Cod sursa (job #1589465) | Cod sursa (job #1526290)
#include <fstream>
#include <vector>
#include <cstring>
#define MXS 2000010
std::fstream in, out;
std::vector<int> pos, nextt;
int n, m;
char text[MXS], pattern[MXS];
void leUrmator()
{
int k = -1, x;
nextt[0] = nextt[1] = 0;
for (x = 1; x < m; ++x)
{
while (k > 0 && pattern[k + 1] != pattern[x]) k = nextt[x];
if (pattern[k + 1] == pattern[x]) ++k;
nextt[x] = k;
}
}
int main()
{
in.open("strmatch.in", std::fstream::in);
out.open("strmatch.out", std::fstream::out);
int i, x = -1;
//in >> pattern >> text;
in.getline(pattern, MXS);
in.getline(text, MXS);
n = strlen(text); //text.size();
m = strlen(pattern); //pattern.size();
nextt.resize(m);
leUrmator();
for (i = 0; i < n; ++i)
{
while (x > 0 && pattern[x + 1] != text[i]) x = nextt[x];
if (pattern[x + 1] == text[i]) ++x;
if (x == m - 1)
{
pos.push_back(i - m + 1);
x = nextt[x];
}
}
int vlen = pos.size();
out << vlen << "\n";
// std::sort(pos.begin(), pos.end());
for (i = 0; i < vlen && i < 1000; ++i) out << pos[i] << ' ';
return 0;
}