Cod sursa(job #1413624)

Utilizator BLz0rDospra Cristian BLz0r Data 1 aprilie 2015 23:29:06
Problema Abc2 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.55 kb
#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];
char s[Nmax], aux[Lmax];
long long p3[22];
int lg, LG;

inline int Number ( char x ){
	return x - 'a';
}

inline long long Key ( long long x ){
	return x % Mod;
}

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] ;
	
	Hash[ Key( T ) ].pb ( T );
	
	for ( int i = lg, j = 0; i <= LG - lg; ++i, ++j ){
		T = T - ( 1LL * Number ( s[j] ) * p3[lg-1] );
		T = ( T * 3 + Number ( s[i] ) ) % Mod;
		Hash[ Key( T ) ].pb ( 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 Count ( int x ){
	int cnt = 0;
	vector < long long > :: iterator it;
	
	int k = Key ( x );
	
	for ( it = Hash[k].begin(); it < Hash[k].end(); ++it )
		cnt += ( *it == x );
	
	return cnt;
}

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] = 3LL * p3[i-1];
	
	Codifica();
	
	do{
		long long x = GetCode();
		rez += Count ( x );
		
	}while ( fscanf ( f, "%s%*c", aux ) != EOF );
	
	fprintf ( g, "%d", rez );
	
	return 0;
}