Pagini recente » competition/solutie | Cod sursa (job #2124023)
#include <fstream>
#include <vector>
#include <map>
#include <cstring>
using namespace std;
ifstream fin("abc2.in");
ofstream fout("abc2.out");
char s[10000005],x[50005];
int n,ct,N;
unsigned int S,R,i;
map<unsigned int,int>H;
map<unsigned int,bool>viz;
int main()
{fin>>s;
S=0;
fin>>x;
N=strlen(x);
R=1;
for(i=1;i<=N;i++)
R=R*3;
for(i=0;s[i]!=0;i++)
{if(s[i]=='b')S+=1;
else if(s[i]=='c')S+=2;
H[S]++;
//fout<<H[S]<<" ";
//fout<<S<<"\n";
S=(1LL*S*3)%R;
}
S=0;
N=strlen(x);
for(i=0;i<N;i++)
{if(x[i]=='b')S+=1;
else if(x[i]=='c')S+=2;
if(i!=N-1)S=(1LL*S*3)%R;
}
ct+=H[S];viz[S]=1;
while(fin>>x)
{S=0;
N=strlen(x);
for(i=0;i<N;i++)
{if(x[i]=='b')S+=1;
else if(x[i]=='c')S+=2;
if(i!=N-1)S=(1LL*S*3)%R;
}
if(!viz[S]){ct+=H[S];viz[S]=1;}
}
fout<<ct;
}