字典树——HDU2072

题目链接

字典树模板题

题目代码

#include
#include
#include<string.h>
#include
using namespace std;
const int maxn=1e4+7;
string str1,str2;
int tree[maxn][30],flag[maxn],tot;
bool insert(string s){
    int len=s.size();
    int rt=0;
    for(int i=0;i){
        int id=s[i]-'a';
        if(!tree[rt][id])tree[rt][id]=++tot;
        rt=tree[rt][id];
    }
    if(flag[rt])return false;
    flag[rt]=1;
    return true;
}
void init(){
    tot=0;
    memset(tree,0,sizeof(tree));
    memset(flag,0,sizeof(flag));
}
int main(){
    while(getline(cin,str1)){
        if(str1=="#")return 0;
        init();
        int sum=0;
        stringstream is(str1);
        while(is>>str2){
            if(insert(str2))sum++;
        }
        printf("%d\n",sum);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/helman/p/11304731.html

你可能感兴趣的:(字典树——HDU2072)