Cod sursa(job #2124023)

Utilizator stefzahZaharia Stefan Tudor stefzah Data 6 februarie 2018 20:12:13
Problema Abc2 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.88 kb
#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;
}