Pagini recente » Cod sursa (job #825074) | Cod sursa (job #2384161) | Cod sursa (job #1945880) | Cod sursa (job #2068536) | Cod sursa (job #1413614)
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <tr1/unordered_set>
using namespace std;
using namespace tr1;
#define Nmax 10000002
#define Lmax 22
FILE *f = fopen ( "abc2.in", "r" );
FILE *g = fopen ( "abc2.out", "w" );
unordered_multiset < long long > S;
char s[Nmax], aux[Lmax];
long long p3[22];
int lg, LG;
inline int Number ( char x ){
return x - 'a';
}
void Codifica (){
long long T = 0;
for ( int i = 0, j = lg - 1 ; i < lg; ++i, --j )
T = T + ( 1LL * Number ( s[i] ) * p3[j] );
S.insert ( T );
for ( int i = lg, j = 0; i < LG - lg; ++i, ++j ){
T = T - ( 1LL * Number ( s[j] ) * p3[lg-1] );
T *= 3;
T += Number ( s[i] );
S.insert ( T );
}
}
long long GetCode (){
long long T = 0;
for ( int i = 0, j = lg - 1 ; i < lg; ++i, --j )
T = T + ( 1LL * Number ( aux[i] ) * p3[j] );
return T;
}
int main(){
int rez = 0;
fscanf ( f, "%s%*c", s );
LG = strlen ( s );
fscanf ( f, "%s%*c", aux );
lg = strlen ( aux );
p3[0] = 1;
for ( int i = 1; i <= lg; ++i )
p3[i] = 3 * p3[i-1];
Codifica();
do{
long long x = GetCode();
rez += S.count ( x );
}while ( fscanf ( f, "%s%*c", aux ) != EOF );
fprintf ( g, "%d", rez );
return 0;
}