Cod sursa(job #1652691)

Utilizator bogdanboboc97Bogdan Boboc bogdanboboc97 Data 15 martie 2016 11:58:19
Problema Abc2 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.84 kb
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
ifstream in("abc2.in");
ofstream out("abc2.out");
struct trie{
    int nr;
    trie *next[3];
};
#define ab(x) (x-'a')
trie* t;
void ins(trie* t,char s[],int pos)
{
    if( t->next[ab(s[pos])]==NULL)
        t->next[ab(s[pos])]=new trie;
    t->next[ab(s[pos])]->nr ++;
    if(s[pos+1]!='\0')
        ins(t->next[ab(s[pos])],s,pos+1);
}
int cnt(trie* t,char x[],int pos)
{
    if( t->next[ab(x[pos])] == NULL )return 0;
    if(x[pos+1]=='\0')return t->next[ab(x[pos])]->nr;
    return cnt(t->next[ab(x[pos])],x,pos+1);
}
char s[10000001],x[22];
int main()
{
    in>>s;
    t=new trie;
    int n=strlen(s);
    for(int i=0;i<n;i++)
        ins(t,s,i);
    int sol=0;
    while(in>>x)
        sol+=cnt(t,x,0);
    out<<sol<<'\n';
    return 0;
}