Pagini recente » Cod sursa (job #2507578) | Cod sursa (job #566023) | Cod sursa (job #2970662) | Cod sursa (job #2506923) | Cod sursa (job #2985137)
#include <bits/stdc++.h>
#define ll long long
#define nl '\n'
using namespace std;
const ll NMAX = 2e6;
const ll M1 = 1e9 + 7;
const ll M2 = 1e9 + 21;
const ll P = 260;
char A[NMAX + 1], B[NMAX + 1];
ll v[1005];
ll hA1 = 0, hA2 = 0;
int main() {
ifstream in("strmatch.in");
ofstream out("strmatch.out");
in >> A;
in >> B;
ll NA = strlen(A);
ll NB = strlen(B);
ll P1, P2, hash1 = 0, hash2 = 0;
P1 = P2 = 1;
for (int i = 0; i < NA; i++) {//calculez valoarea hash ului Pt sirul A
hA1 = (hA1 * P + A[i]) % M1;
hA2 = (hA2 * P + A[i]) % M2;
if (i != 0)
P1 = (P1 * P) % M1,
P2 = (P2 * P) % M2;
}
if (NA > NB) {
out << 0;
return 0;
}
for(int i = 0; i < NA; i++)
hash1 = (hash1 * P + B[i]) % M1,
hash2 = (hash2 * P + B[i]) % M2;
ll ans = 0;
if (hA1 == hash1 and hA2 == hash2) {
++ans;
v[ans] = 0;
}
for (int i = NA; i < NB; i++) {
hash1 = ((hash1 - (B[i - NA] * P1) % M1 + M1) * P + B[i]) % M1;
hash2 = ((hash2 - (B[i - NA ] * P2) % M2 + M2) * P + B[i]) % M2;
if (hash1 == hA1 and hash2 == hA2) {
++ans;
if (ans <= 1000)
v[ans] = i - NA + 1;
}
}
out << ans << nl;
for (int i = 1; i <= min(ans, (ll)1000); i++)
out << v[i] << ' ';
}