天天看点

HDU 1251:统计难题

#include<cstdio>
#include<cstring>
using namespace std;

const int maxn=400000+100;

int trie[maxn][26],sum[maxn],tot;

void insert(char* ch){
  
  int len=strlen(ch);
  int root=0;
  for(int i=0;i<len;i++){
    
    int ind=ch[i]-'a';
    if(!trie[root][ind]) trie[root][ind]=++tot;
    sum[trie[root][ind]]++;
    root=trie[root][ind];
  }
}

int search(char* ch){
  
  int len=strlen(ch);
  int root=0;
  for(int i=0;i<len;i++){
    
    int ind=ch[i]-'a';
    if(!trie[root][ind]) return 0;
    root=trie[root][ind];
  }
  return sum[root];
}

int main(){
  
  char ch[20];
  while(gets(ch)){
    
    if(ch[0]=='\0') break;
    insert(ch);
  }
  while(scanf("%s",ch)==1){
    
    printf("%d\n",search(ch));
  }
}