Pagini recente » Cod sursa (job #1463775) | Cod sursa (job #2249746) | Cod sursa (job #1785332) | Cod sursa (job #1022922) | Cod sursa (job #3209589)
#include <bits/stdc++.h>
#define P 777013
#define Q 203459
using namespace std;
ifstream fin("abc2.in");
ofstream fout("abc2.out");
int cnt, L, n;
char text[10000005], cuv[25];
unordered_map<long long, bool> M;
int main()
{
int i, x1, x2, p1, p2;
fin >> text;
while(fin >> cuv)
{
x1 = x2 = 0;
for(i = 0;cuv[i] != 0;i++)
{
x1 = (x1 * 3 + cuv[i] - 'a') % P;
x2 = (x2 * 3 + cuv[i] - 'a') % Q;
}
M[1LL * x1 * 777013 + x2] = 1;
}
L = strlen(cuv);
x1 = x2 = 0;
p1 = p2 = 1;
for(i = 1;i < L;i++)
{
p1 = p1 * 3 % P;
p2 = p2 * 3 % Q;
}
for(i = 0;i < L;i++)
{
x1 = (x1 * 3 + text[i] - 'a') % P;
x2 = (x2 * 3 + text[i] - 'a') % Q;
}
if(M[1LL * x1 * 777013 + x2] == 1)
cnt++;
for(i = L;text[i] != 0;i++)
{
x1 = (x1 - p1 * (text[i - L] - 'a') + P) % P;
x1 = (x1 * 3 + text[i] - 'a') % P;
x2 = (x2 - p2 * (text[i - L] - 'a') + Q) % Q;
x2 = (x2 * 3 + text[i] - 'a') % Q;
if(M[1LL * x1 * 777013 + x2] == 1)
cnt++;
}
fout << cnt << "\n";
fout.close();
return 0;
}