#1014 : Trie树

来源:互联网 发布:全境封锁优化怎么样 编辑:程序博客网 时间:2024/05/17 02:23

模版题,练手。

指针式动态版。

#include<bits/stdc++.h>using namespace std;#define sigma_size 26int n,m;char buf[110];struct Trie{    int cnt;    Trie* next[sigma_size];    Trie(){        cnt=0;        for(int i=0;i<sigma_size;i++)            this->next[i]=NULL;    }   }tr;void insert(char* s){    Trie* p = &tr;     while(*s!=0){        if(p->next[*s-'a']==NULL){            p->next[*s-'a']=new Trie;        }        p=p->next[*s-'a'];        p->cnt++;        s++;    }}int search(char *s){    Trie* p = &tr;    while(*s!=0){        if(p->next[*s-'a']==NULL)return 0;        p = p->next[*s-'a'];        s++;    }    return p->cnt;}int main(){    ios::sync_with_stdio(false);    cin>>n;    for(int i=1;i<=n;i++)        cin>>buf,insert(buf);    cin>>m;    for(int i=1;i<=m;i++)        cin>>buf,cout<<search(buf)<<endl;       return 0;}
0 0
原创粉丝点击