Pagini recente » Cod sursa (job #1912897) | Cod sursa (job #2076032) | Cod sursa (job #2446585) | Cod sursa (job #138974) | Cod sursa (job #2008245)
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
ifstream in("strmatch.in");
ofstream out("strmatch.out");
typedef unsigned int ULL;
const int C = 2, C2 = 3;
int n, m;
char a[2000002], b[2000002];
ULL ha, ha2, hb[2000001], hb2[2000001], powC[2000001], powC2[2000001];
int sol[1001];
inline void preprocess() {
for (int i = 0; i < n; ++i) {
powC[i] = powC[i - 1] * C;
powC2[i] = powC2[i - 1] * C2;
}
for (int i = n; i >= 0; --i) {
hb[i] = b[i] * powC[0] + hb[i + 1] * C;
hb2[i] = b[i] * powC2[0] + hb2[i + 1] * C2;
}
}
void hash_a() {
for (int i = 0; i < n; ++i) {
ha += a[i] * powC[i];
ha2 += a[i] * powC2[i];
}
}
int main()
{
in >> a;
in >> b;
n = strlen(b) - 1;
m = strlen(a);
preprocess();
hash_a();
int stop = n - m + 1;
for (int i = 0; i <= stop; ++i) {
int l = i, r = i + m - 1;
ULL h = hb[l] - hb[r + 1] * powC[r - l + 1];
ULL h2 = hb2[l] - hb2[r + 1] * powC2[r - l + 1];
if (h == ha && h2 == ha2) {
sol[0]++;
if (sol[0] <= 1000) sol[sol[0]] = i;
}
}
out << sol[0] << "\n";
stop = min(sol[0], 1000);
for (int i = 1; i <= stop; ++i) {
out << sol[i] << " ";
}
return 0;
}