Pagini recente » Cod sursa (job #3276790) | Cod sursa (job #2791153)
#include <iostream>
#include <cstring>
using namespace std;
const int NMAX = 2000010;
int sl, micutl, ans[1010], ansl;
char s[NMAX + 1], micut[NMAX + 1];
struct Hash {
long long n = 97, m = 0, power = 0, value = 0;
void init(const char * X, const int XL, const int M) {
m = M;
power = 1;
value = 0;
for(int i = XL - 1; i >= 0; --i) {
value = (value + power * X[i] % m) % m;
if(i) power = power * n % m;
}
}
void roll(const char toRem, const char toAdd) {
value = (((value - (1ll * toRem * power) % m + m) * n) % m + toAdd) % m;
}
};
int main()
{
freopen("strmatch.in", "r", stdin);
freopen("strmatch.out", "w", stdout);
scanf("%s %s", micut, s);
micutl = strlen(micut), sl = strlen(s);
if(micutl > sl) {
printf("0");
return 0;
}
Hash mh1, mh2, sh1, sh2;
mh1.init(micut, micutl, 100003);
mh2.init(micut, micutl, 100019);
sh1.init(s, micutl, 100003);
sh2.init(s, micutl, 100019);
for(int i = micutl - 1; i < sl; ++i) {
if(sh1.value == mh1.value && sh2.value == mh2.value) {
++ansl;
if(ansl <= 1000) ans[ansl] = i - micutl + 1;
}
sh1.roll(s[i - micutl + 1], s[i + 1]),
sh2.roll(s[i - micutl + 1], s[i + 1]);
}
printf("%d\n", ansl);
ansl = min(ansl, 1000);
for(int i = 1; i <= ansl; ++i) {
printf("%d ", ans[i]);
}
return 0;
}