SDUT 1500 Message Flood
2012-08-15 21:08
274 查看
字典树模板题。
题目链接http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1500
View Code
题目链接http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1500
View Code
#include<stdio.h> #include<string.h> #include<stdlib.h> #define N 26 struct node { int num; struct node *next ; }tree[21000]; int t=0; struct node *creat() { int i; struct node *p; //p=&tree[t++]; 为什么这里不能用静态分配内存。用的话会RE。 改成动态就可以了? p=(struct node*)malloc(sizeof(struct node)); for(i=0;i<N;i++) p->next[i]=NULL; p->num=0; return p; } void Insert(struct node *root,char *s) { int i,len,ans; len=strlen(s); struct node *p=root; for(i=0;i<len;i++) { if(s[i]>='A' && s[i]<='Z') ans=s[i]-'A'; else ans=s[i]-'a'; if(p->next[ans]==NULL) p->next[ans]=creat(); p=p->next[ans]; } p->num=1; } int search(struct node *root,char *s) { struct node *p=root; int i,len,ans; len=strlen(s); for(i=0;i<len;i++) { if(s[i]>='A' && s[i]<='Z') ans=s[i]-'A'; else ans=s[i]-'a'; if(p->next[ans]==NULL) return 0; p=p->next[ans]; } if(p->num==1) {p->num=0;return 1;} return 0; } void Delete(struct node *p) { int i; for(i=0;i<N;i++) if(p->next[i]!=NULL) Delete(p->next[i]); delete p; p=NULL; } int main() { int i,n,m,count; char str[12]; struct node *p; while(scanf("%d",&n)!=EOF && n) { scanf("%d",&m); p=creat(); for(i=0;i<n;i++) { scanf("%s",str); Insert(p,str); } count=0; while(m--) { scanf("%s",str); count+=search(p,str); } printf("%d\n",n-count); Delete(p); } return 0; }
相关文章推荐
- SDUT1500_Message Flood(字典树)
- sdut 1500 Message Flood
- SDUT 1500-Message Flood(set)
- sdut 1500 Message Flood
- sdut 1500 Message Flood(Trie树)
- SDUT 1500-Message Flood(set)
- SDUT 1500 Message Flood
- sdut1500 Message Flood
- SDUT 1500 Message Flood(字典树 && 模板)
- sdut 1500 Message Flood
- 【1500】Message Flood (SDUTOJ)
- 【1500】Message Flood (SDUTOJ)
- SDUT 1500 Message Flood trie树
- SDUTOJ 1500 ——Message Flood 字典树或map
- SDUT-1500-(字典树)
- oj1500(Message Flood)字典树
- 1500_Message Flood
- SDUT 1480 数据结构试验: 哈希表
- sdut 2441 屠夫与狼
- sdut 2189 基友记(用一个count数组记录出现的个数)