Pagini recente » Profil Maria-Mary | Cod sursa (job #395064) | Cod sursa (job #2084087) | Cod sursa (job #3220943) | Cod sursa (job #786931)
Cod sursa(job #786931)
#include <cassert>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long int64;
const int N = 2000005;
const int MOD = 10233049;
//const int MOD2 = 31309;
const int BASE = 123;
char text[N], pattern[N];
int textHash, patternHash;
int putere[N];
int sol[N];
void read() {
assert(freopen("strmatch.in", "r", stdin) != NULL);
assert(freopen("strmatch.out", "w", stdout) != NULL);
assert(scanf("%s %s", pattern + 1, text + 1) == 2);
}
void solve() {
int n = strlen(pattern + 1);
int m = strlen(text + 1);
putere[0] = 1;
for (int i = 1; i < n; ++i)
putere[i] = (putere[i - 1] * BASE) % MOD;
for (int i = 1; i <= n; ++i)
patternHash = (patternHash + (pattern[i] * putere[n - i])) % MOD;
for (int i = 1; i <= n; ++i)
textHash = (textHash + (text[i] * putere[n - i])) % MOD;
int i = n;
while (i <= m) {
if (textHash == patternHash)
sol[++sol[0]] = i - n;
textHash = textHash - text[i - n + 1] * putere[n - 1];
textHash = (textHash % MOD + MOD) % MOD;
++i;
textHash = (textHash * BASE + text[i]) % MOD;
}
}
void write() {
printf("%d\n", sol[0]);
for (int i = 1; i <= min(sol[0], 1000); ++i)
printf("%d ", sol[i]);
}
int main() {
read();
solve();
write();
}