Pagini recente » Cod sursa (job #255129) | Cod sursa (job #2485447) | Cod sursa (job #1065179) | Cod sursa (job #451090) | Cod sursa (job #2977424)
#include <fstream>
#include <cstring>
using namespace std;
ifstream fin("strmatch.in");
ofstream fout("strmatch.out");
const int BASE = 73;
const int MOD1 = 100007;
const int MOD2 = 100021;
const int DIM = 2000001;
char a[DIM], b[DIM];
bool match[DIM];
int main() {
fin >> a >> b;
int aLen = strlen(a);
int bLen = strlen(b);
if (aLen > bLen) {
fout << 0;
return 0;
}
int p1 = 1, p2 = 1;
int hashA1 = 0, hashA2 = 0;
int hashB1 = 0, hashB2 = 0;
for (int i = 0; i < aLen; i++) {
hashA1 = (hashA1 * BASE + a[i]) % MOD1;
hashA2 = (hashA2 * BASE + a[i]) % MOD2;
hashB1 = (hashB1 * BASE + b[i]) % MOD1;
hashB2 = (hashB2 * BASE + b[i]) % MOD2;
if (i != 0) {
p1 = (p1 * BASE) % MOD1;
p2 = (p2 * BASE) % MOD2;
}
}
int matchCount = 0;
if (hashA1 == hashB1 && hashA2 == hashB2) {
match[0] = true;
matchCount++;
}
for (int i = aLen; i < bLen; i++) {
hashB1 = ((hashB1 - (b[i - aLen] * p1) % MOD1 + MOD1) * BASE + b[i]) % MOD1;
hashB2 = ((hashB2 - (b[i - aLen] * p2) % MOD2 + MOD2) * BASE + b[i]) % MOD2;
if (hashA1 == hashB1 && hashA2 == hashB2) {
match[i - aLen + 1] = true;
matchCount++;
}
}
fout << matchCount << '\n';
int solCnt = 0;
for (int i = 0; i < bLen && solCnt < 1000; i++) {
if (match[i]) {
fout << i << ' ';
solCnt++;
}
}
return 0;
}