Pagini recente » Borderou de evaluare (job #1796740) | Cod sursa (job #1191403) | Cod sursa (job #3182446) | Borderou de evaluare (job #2510040) | Cod sursa (job #2985133)
#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];
bool 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[0] = 1;
}
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) {
v[i - NA + 1] = 1;
++ans;
}
}
out << ans << nl;
for (int i = 0; i < min(NB, (ll)1000); i++)
if (v[i])
out << i << ' ';
}