博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
sdut 1500 Message Flood(Trie树)
阅读量:6945 次
发布时间:2019-06-27

本文共 1536 字,大约阅读时间需要 5 分钟。

题目:

View Code
1 #include
2 #include
3 #include
4 #include
5 using namespace std; 6 struct node 7 { 8 int flag; 9 node *next[26];10 };11 int num;12 node *build()13 {14 node *p;15 int i;16 p=new node;17 p->flag=0;18 for(i=0;i<26;i++)19 p->next[i]=NULL;20 return p;21 }22 void insert(node *p, char *s)23 {24 int len,i,t;25 len=strlen(s);26 for(i=0;i
='A'&&s[i]<='Z')29 t=s[i]-'A';30 else31 t=s[i]-'a';32 if(p->next[t]==NULL)33 p->next[t]=build();34 p=p->next[t];35 }36 p->flag=1;37 }38 int search(node *p,char *s)39 {40 int t,i,len;41 len=strlen(s);42 for(i=0;i
='A'&&s[i]<='Z')45 t=s[i]-'A';46 else47 t=s[i]-'a';48 if(p->next[t]==NULL)49 return 0;50 p=p->next[t];51 }52 if(p->flag==1)53 {54 num++;55 p->flag=-1;56 return 1;57 }58 return 0;59 }60 void deal(node *p)61 {62 int i;63 if(p)64 {65 for(i = 0 ;i < 26 ; i++)66 if(p->next[i])67 deal(p->next[i]);68 }69 free(p);70 p=NULL;71 }72 int main()73 {74 int m,n,i,k;75 char str[25];76 node *p;77 while(~scanf("%d",&m))78 {79 if(m==0)80 break;81 scanf("%d",&n);82 p=build();83 num=0;84 for(k=1;k<=m;k++)85 {86 scanf("%s",str);87 insert(p,str);88 }89 while(n--)90 {91 scanf("%s",str);92 search(p,str);93 }94 printf("%d\n",m-num);95 deal(p);96 }97 return 0;98 }

转载于:https://www.cnblogs.com/wanglin2011/archive/2012/08/13/2636974.html

你可能感兴趣的文章
爪哇国新游记之十一----用异常控制流程
查看>>
Oracle中rownum用法警示
查看>>
【转】Delphi调用webservice总结
查看>>
为你的应用加速 - 安卓优化指南
查看>>
【USACO 2.1】Hamming Codes
查看>>
【GoLang】GoLang 中 make 与 new的区别
查看>>
《Unix内核源码剖析》
查看>>
Windows phone 应用开发[4]-开发人员Metro UI设计指南
查看>>
保卫萝卜官方PC版——含绿色版 V1.0.6Beta
查看>>
聚合类新闻client产品功能点详情分析
查看>>
突袭HTML5之WebSocket入门5 - 包管理工具npm
查看>>
HDU-4255 A Famous Grid BFS
查看>>
每日英语:Everything You Think You Know About China Is Wrong
查看>>
带线的无限级下拉树列表-完整示例篇
查看>>
Clipboard with Custom Clipboard Formats - Delphi
查看>>
[Step By Step]SAP HANA PAL 异态检测算法Anomaly Detection实现例程ANOMALYDETECTION
查看>>
linux上配置boost手记
查看>>
IIS状态监测(如果状态错误则重启IIS)
查看>>
PostgreSQL中,database,schema,table之间关系
查看>>
12个球一个天平,现知道只有一个和其它的重量不同,问怎样称才能用三次就找到那个球(13个呢?)...
查看>>