Pagini recente » Cod sursa (job #448122) | Cod sursa (job #3120661) | Cod sursa (job #2586805) | Cod sursa (job #2989431) | Cod sursa (job #169491)
Cod sursa(job #169491)
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int maxn = 10000002;
const int maxl = 22;
const int maxc = 50001;
const int mod1 = 1 << 18;
const int mod2 = 1 << 17;
const int pp1 = 101;
const int pp2 = 73;
FILE *in = fopen("abc2.in","r"), *out = fopen("abc2.out","w");
char a[maxn];
int n, m;
char c[maxc][maxl];
unsigned char h1[(mod1 >> 3) + 1];
unsigned char h2[(mod2 >> 3) + 1];
vector<unsigned short> l[mod1];
int p1;
int p2;
int pow(int a, int b, int mod)
{
int s = 1;
for ( int i = 1; i <= b; ++i )
s *= a,
s = s & (mod - 1);
return s;
}
int j = 1;
void getnr()
{
int hash1 = 0, hash2 = 0;
for ( int i = 0; i < n; ++i )
{
hash1 = (hash1 * pp1 + c[j][i]) & (mod1 - 1);
hash2 = (hash2 * pp2 + c[j][i]) & (mod2 - 1);
}
h1[ hash1 >> 3 ] |= (1 << (hash1 & 7));
h2[ hash2 >> 3 ] |= (1 << (hash2 & 7));
l[ hash1 ].push_back(j);
}
char t[maxl];
int mystrcmp(char x[], char y[])
{
for ( int i = 0; x[i]; ++i )
if ( x[i] != y[i] )
return 0;
return 1;
}
int check(int what, int from, int to)
{
for ( int i = from; i <= to; ++i )
t[i - from] = a[i];
t[to - from + 1] = '\0';
for ( int i = l[what].size() - 1; i != -1; --i )
if ( mystrcmp(c[ l[ what ][i] ], t) )
return 1;
return 0;
}
int cnt;
int main()
{
fscanf(in, "%s", a);
m = strlen(a);
fscanf(in, "%s", c[1]);
n = strlen(c[1]);
p1 = pow(pp1, n - 1, mod1),
p2 = pow(pp2, n - 1, mod2);
getnr();
++j;
while ( fscanf(in, "%s", c[j]) == 1 )
getnr(), ++j;
unsigned int hash1 = 0, hash2 = 0;
for ( int i = 0; i < n; ++i )
{
hash1 = (hash1 * pp1 + a[i]) & (mod1 - 1);
hash2 = (hash2 * pp2 + a[i]) & (mod2 - 1);
}
if ( ( h1[ hash1 >> 3 ] & (1 << (hash1 & 7)) ) && ( h2[ hash2 >> 3 ] & (1 << (hash2 & 7)) ) )
if ( check(hash1, 0, n - 1) )
++cnt;
for ( int i = n; i < m; ++i )
{
hash1 = ((hash1 - ((a[i - n] * p1) & (mod1 - 1)) + mod1) * pp1 + a[i]) & (mod1 - 1);
hash2 = ((hash2 - ((a[i - n] * p2) & (mod2 - 1)) + mod2) * pp2 + a[i]) & (mod2 - 1);
if ( ( h1[ hash1 >> 3 ] & (1 << (hash1 & 7)) ) && ( h2[ hash2 >> 3 ] & (1 << (hash2 & 7)) ) )
if ( check(hash1, i - n + 1, i) )
++cnt;
}
fprintf(out, "%d\n", cnt);
return 0;
}