您的位置:首页 > 其它

hdu1251 tire树入门

2015-08-20 10:14 253 查看
#include<stdio.h>
#include<string>
#include<map>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
const int N=1e4+10;
const int MOD=1e9+7;
int n,m,k;
struct node{
node* child[26];
int cnt;
node(){
cnt=0;
for(int i=0;i<26;i++) child[i]=NULL;
}
};
node *root=new node;
node *p,*newp;
void insert(char* s){
p=root;
int c;
int len=strlen(s)-1;
for(int i=0;i<len;i++){
c=s[i]-'a';
if(p->child[c]!=NULL){
p=p->child[c];
p->cnt++;
}else{
newp=new node;
p->child[c]=newp;
p=newp;
p->cnt++;
}
}

}
int search(char *s){
int len=strlen(s);
p=root;
for(int i=0;i<len;i++){
int c=s[i]-'a';
if(p->child[c]==NULL) return 0;
else p=p->child[c];
}
return p->cnt;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("aaa","r",stdin);
#endif
int T;
char str[20];
while(fgets(str,100,stdin),strlen(str)!=1) insert(str);
while(~scanf("%s",str)) printf("%d\n",search(str));
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: