Pagini recente » Cod sursa (job #3175828) | Cod sursa (job #2295468) | Cod sursa (job #959202) | Cod sursa (job #2644204) | Cod sursa (job #1843471)
#include <fstream>
#include <string>
#include <vector>
using namespace std;
vector<int> FindPrefixes(const string &s)
{
vector<int> pref;
pref.push_back(0);
int k = 0;
for (int i = 1; i < s.size(); ++i) {
while (k > 0 && s[i] != s[k])
k = pref[k - 1];
if (s[i] == s[k])
++k;
pref.push_back(k);
}
return pref;
}
pair<int, vector<int>> KMP(const string &w, const string &t)
{
auto prefixes = FindPrefixes(w);
vector<int> matches;
int result = 0;
int k = 0;
for (int i = 0; i < t.size(); ++i) {
while (k > 0 && t[i] != w[k])
k = prefixes[k - 1];
if (t[i] == w[k])
++k;
if (k == w.size()) {
++result;
if (matches.size() < 1000)
matches.push_back(i - w.size() + 1);
k = prefixes[k - 1];
}
}
return { result, matches };
}
int main()
{
ifstream fin("strmatch.in");
ofstream fout("strmatch.out");
string word;
getline(fin, word);
string text;
getline(fin, text);
auto result = KMP(word, text);
fout << result.first << "\n";
for (int pos : result.second)
fout << pos << " ";
return 0;
}