Pagini recente » Cod sursa (job #233201) | Cod sursa (job #3257677) | Cod sursa (job #678581) | Cod sursa (job #1084718) | Cod sursa (job #1240366)
#include <string>
#include <fstream>
#include <vector>
#include <iostream>
using namespace std;
#define MOD 100007
#define BASE 79
ifstream fin("strmatch.in");
ofstream fout("strmatch.out");
int main() {
string s, p;
fin >> p >> s;
int lp = p.size();
int phash = 0, shash = 0, val = 1;
for (int i = 0; i < lp - 1; ++i) {
phash = (phash * BASE + p[i]) % MOD;
shash = (shash * BASE + s[i]) % MOD;
// cout << phash << " " << shash << endl;
val = (val * BASE) % MOD;
}
phash = (phash * BASE + p[lp - 1]) % MOD;
shash = (shash * BASE + s[lp - 1]) % MOD;
// cout << phash << " " << shash << endl;
int total = 0;
vector<int> positions;
if (phash == shash) {
int ok = 1;
for (int j = 0; j < lp; ++j) {
if (p[j] != s[j]) {
ok = 0;
break;
}
}
if (ok) {
++total;
positions.push_back(0);
}
}
for (int i = 1; i < s.size() - lp + 1; ++i) {
shash = (((shash - val * s[i-1]) % MOD + MOD) * BASE + s[i + lp - 1]) % MOD;
// cout << phash << " " << shash << endl;
if (phash == shash) {
int ok = 1;
for (int j = i; j < i + lp; ++j) {
if (p[j - i] != s[j]) {
ok = 0;
break;
}
}
if (ok) {
++total;
positions.push_back(i);
if (total >= 1000) {
break;
}
}
}
}
fout << total << endl;
for (int i = 0; i < positions.size(); ++i) {
fout << positions[i] << " ";
}
return 0;
}