HDU 1251 统计难题

来源:互联网 发布:支持java的游戏引擎 编辑:程序博客网 时间:2024/06/10 07:52

      纯属拿来练字典树模板的

#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>using namespace std;char ch[100005][15];char str[15];int pos=0,ans=0;#define Max 26struct trie{int v;trie *next[Max];};trie *root;void init(){    trie *t,*p=root;    int length=strlen(ch[pos]);    for(int i=0;i<length;i++){        int id=ch[pos][i]-'a';        if(p->next[id]==NULL){            t=new trie;            for(int j=0;j<26;j++){                t->next[j]=NULL;            }            t->v=0;            p->next[id]=t;        }        p=p->next[id];        p->v++;    }}void find(){int length=strlen(str);trie *p=root;for(int i=0;i<length;i++){int id=str[i]-'a';if(p->next[id]==NULL)break;p=p->next[id];if(i==length-1)ans+=p->v;}}int main(){    root=new trie;    for(int i=0;i<26;i++){        root->next[i]=NULL;        root->v=0;    }while(gets(ch[pos])){        if(strlen(ch[pos])==0)            break;        init();        pos++;}while(scanf("%s",str)!=EOF){find();printf("%d\n",ans);ans=0;}return 0;}

0 0