Trie树
来源:互联网 发布:知花作品全集 编辑:程序博客网 时间:2024/06/11 01:07
Trie树关于前缀计算的应用:
HDUOJ 1251统计难题 http://acm.hdu.edu.cn/showproblem.php?pid=1251
HDUOJ 1671PhoneList http://acm.hdu.edu.cn/showproblem.php?pid=1671
统计难题
题意就是给出几个word 再给几个word问有几个是已这几个word作为前缀的word
直接模版就可以了
PhoneList
题意就是问给的一系列数字组合中有木有数字组合是其他数字组合的前缀
要把所有输入的数字组合都保存insert 然后find 看find的值是否大于1
敲了板子之后提交报错 内存超了 看了别人的代码 发现就是要释放内存 加个release函数
贴个代码:
#include <iostream>#include <cstdio>#include <cstring>using namespace std;char a[10005][10];struct node{ int cnt; node *next[10]; node() { for(int i=0;i<10;i++) next[i]=NULL; }};node *root;void insert(char *str){ node *p,*newnode; p=root; for(;*str;str++) { if(p->next[*str-'0']!=NULL) { p=p->next[*str-'0']; p->cnt++; } else { newnode=new node; p->next[*str-'0']=newnode; p=p->next[*str-'0']; p->cnt=1; } }}int find(char *str){ node *p; p=root; for(;*str;str++) { if(p->next[*str-'0']!=NULL) p=p->next[*str-'0']; else return 0; } return p->cnt;}void release(struct node *now)//释放内存{ for(int i=0;i<10;i++) if(now->next[i]) release(now->next[i]); delete now;}int main(){ int tc; scanf("%d",&tc); while(tc--) { root=new node; root->cnt=0; int n; scanf("%d",&n); getchar(); for(int i=0;i<n;i++) { scanf("%s",a[i]); insert(a[i]); } int flag=0; for(int i=0;i<n;i++) { if(find(a[i])>1) { flag=1; break; } else continue; } // cout<<flag<<endl; if(flag) puts("NO"); else puts("YES"); release(root); } return 0;}
语言翻译应用
HD 1075
题意就是将火星文翻译成英文输出
http://acm.hdu.edu.cn/showproblem.php?pid=1075
insert()将模版中的cnt换成翻译后的字符串 绑在要被翻译的字符串最后一个字符上
find()依次判断每个字符是不是跟初始状态一样 加上对最后一个字符的判断
一开始用 char *s; 简直蛋疼 指针真的好难搞
string 有时还是蛮好用的 有些函数还是值得用的
贴个代码:
#include <iostream>#include <string>#include <cstring>#include <cstdio>using namespace std;struct node{ string s; node *next[26]; node() { s=""; memset(next,0,sizeof(next)); }};node *root;void insert(string s1,string s2){ node *p; p=root; for(int i=0;i<s2.size();i++) { if(p->next[s2[i]-'a']==NULL) p->next[s2[i]-'a']=new node(); p=p->next[s2[i]-'a']; } p->s=s1;}void find(string s){ //cout<<" "<<s<<" "; node *p; p=root; for(int i=0;i<s.size();i++) { int num=s[i]-'a'; if(p->next[num]==NULL) { cout<<s; return; } p=p->next[num]; } if(p->s=="") cout<<s; else cout<<p->s;}int main(){ string s,s1,s2; root=new node(); cin>>s;//start while(cin>>s1&&s1!="END") { cin>>s2; //cout<<s1<<" "<<s2<<endl; insert(s1,s2); } cin>>s;//start getchar();//\n while(getline(cin,s)&&s!="END") { int n=0; for(int i=0;i<s.size();i++) { if(s[i]<'a'||s[i]>'z') { s2=s.substr(n,i-n);//截取字符串 j~i-1 // cout<<endl<<s1<<endl; find(s2); cout<<s[i]; n=i+1; } } cout<<endl; } return 0;}
//待续 欢迎大家评论 给点建议
0 0
- hihoCoder1014 Trie树 [Trie]
- TRIE树
- TRIE树
- TRIE树
- trie 树
- Trie树
- Trie树
- Trie树
- Trie树
- trie树
- trie树
- Trie 树
- Trie树
- Trie 树
- trie 树
- Trie树
- Trie树
- Trie树
- 010004 在选择中挣扎
- UICollectionView的使用
- ShareSDK上线运营统计、手游录像、短信验证码三大功能
- 13.2 重载"+"与StringBuilder
- 修改IIS 设置让下载INI文件
- Trie树
- Setter类的属性
- Linux环境下配置php开发环境(我转载的原因是里面有我要的配置虚拟主机的提示)
- 13.5 格式化输出
- gstreamer编译环境
- python简单的多人聊天室
- 13.5.4 格式化说明符
- property基础介绍
- LoadRunner-遇到的问题及解决方法