Pagini recente » Cod sursa (job #3163559) | Cod sursa (job #2431722) | Cod sursa (job #1389493) | Cod sursa (job #1995473) | Cod sursa (job #1413647)
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
#define Nmax 10000002
#define Lmax 22
#define Mod 666013
#define pb push_back
FILE *f = fopen ( "abc2.in", "r" );
FILE *g = fopen ( "abc2.out", "w" );
vector < long long > Hash[Mod+1];
char s[Nmax], aux[Lmax];
long long p3[22];
int lg, LG;
inline int Number ( char x ){
return x - 'a';
}
inline int Key ( long long x ){
return x % Mod;
}
void InsertValue ( long long x ){
Hash[Key(x)].pb ( x );
}
bool FindValue ( long long x ){
vector < long long > :: iterator it;
int k = Key ( x );
for ( it = Hash[k].begin(); it < Hash[k].end(); ++it )
if ( *it == x )
return 1;
return 0;
}
long long GetCode (){
long long ret = 0;
for ( int i = 0, j = lg; i <= lg; ++i, --j )
ret = ret + 1LL * Number ( aux[i] ) * p3[j];
return ret;
}
int main(){
int rez = 0;
long long T = 0;
fscanf ( f, "%s%*c", s );
LG = strlen ( s ) - 1;
fscanf ( f, "%s%*c", aux );
lg = strlen ( aux ) - 1;
p3[0] = 1;
for ( int i = 1; i <= lg; ++i )
p3[i] = 3LL * p3[i-1];
do{
long long x = GetCode();
if ( !FindValue ( x ) )
InsertValue ( x );
}while ( fscanf ( f, "%s%*c", aux ) != EOF );
for ( int i = 0, j = lg ; i <= lg; ++i, --j ){
T = T + 1LL * Number ( s[i] ) * p3[j];
rez += FindValue ( T );
}
//fprintf ( g, "%lld\n", T );
for ( int i = lg + 1, j = 0; i <= LG; ++i, ++j ){
T = T - 1LL * Number ( s[j] ) * p3[lg];
T = T * 3 + Number ( s[i] );
rez += FindValue ( T );
//fprintf ( g, "%lld\n", T );
}
fprintf ( g, "%d", rez );
return 0;
}