Pagini recente » Borderou de evaluare (job #2076839) | Borderou de evaluare (job #2892239) | Borderou de evaluare (job #2980329) | Borderou de evaluare (job #2283581) | Cod sursa (job #3333409)
#include <fstream>
#include <cstring>
#include <string>
#define P 73
#define MOD1 100007
#define MOD2 100021
using namespace std;
ifstream cin ("strmatch.in");
ofstream cout ("strmatch.out");
char A[2000001];
char B[2000001];
int NA, NB;
int hashA1, hashA2, P1, P2;
char match[2000001];
int main()
{
cin.get(A, 2000000, '\n');
cin.get(B, 2000000, 'EOF');
strcpy(B, B + 1);
NA = strlen(A);
NB = strlen(B);
P1 = P2 = 1;
hashA1 = hashA2 = 0;
for (int i = 0; i < NA; i++)
{
hashA1 = (hashA1 * P + A[i]) % MOD1;
hashA2 = (hashA2 * P + A[i]) % MOD2;
if (i != 0)
P1 = (P1 * P) % MOD1,
P2 = (P2 * P) % MOD2;
}
if (NA > NB)
{
cout << "0" << endl;
return 0;
}
int hash1 = 0, hash2 = 0;
for (int i = 0; i < NA; i++)
hash1 = (hash1 * P + B[i]) % MOD1,
hash2 = (hash2 * P + B[i]) % MOD2;
int Nr = 0;
if (hash1 == hashA1 && hash2 == hashA2)
match[0] = 1, Nr++;
for (int i = NA; i < NB; i++)
{
hash1 = ((hash1 - (B[i - NA] * P1) % MOD1 + MOD1) * P + B[i]) % MOD1;
hash2 = ((hash2 - (B[i - NA] * P2) % MOD2 + MOD2) * P + B[i]) % MOD2;
if (hash1 == hashA1 && hash2 == hashA2)
match[ i - NA + 1 ] = 1, Nr++;
}
cout << Nr << endl;
Nr = 0;
for (int i = 0; i < NB && Nr < 1000; i++)
if (match[i])
{
Nr++;
cout << i << " ";
}
cout << endl;
return 0;
}