HDU 1305 Immediate Decodability(与1671类似)
2015-08-14 10:37
375 查看
题意:在多个字符串里寻找,若他们至少有一个串是另一个串的前缀,则按题目要求输出
思路:建立字典树,因为只有0 1两数,数组开2大就行,判断是前缀的方法:每次插入一个字符串,在字典树中查询,若当到达已经存在了的字符串,即cur->cnt==1 ,也就是插入字符串的前缀,或者在字典树中能够找到插入的字符串
思路:建立字典树,因为只有0 1两数,数组开2大就行,判断是前缀的方法:每次插入一个字符串,在字典树中查询,若当到达已经存在了的字符串,即cur->cnt==1 ,也就是插入字符串的前缀,或者在字典树中能够找到插入的字符串
#include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #include<iostream> #include<algorithm> #include<sstream> #include<fstream> #include<vector> #include<map> #include<stack> #include<list> #include<set> #include<queue> #define LL long long #define lson l,m,rt<<1 #define rson m+1,r,rt<<1 | 1 using namespace std; const int child_size=2; struct node { int num,id; node *next[child_size]; node() { num=0;id=0;memset(next,0,sizeof(next));} }; node *p,*root; string str[10005]; void Insert(string s) { p=root; for(int i=0;i<s.size();i++) { int id=s[i]-'0'; if(p->next[id]==0) p->next[id]=new node(); p=p->next[id]; //p->num++; } p->num=1; } int Query(string s) { p=root; for(int i=0;i<s.size();i++) { int id=s[i]-'0'; if(p->next[id]==0) return 1; p=p->next[id]; if(p->num==1&&(i<s.size()-1)) return 0; } // if(p->num>1) return 0; return 1; } void Delete(node *rt) { for(int i=0;i<child_size;i++) { if(rt->next[i]) { Delete(rt->next[i]); delete(rt->next[i]); } } } int main() { int n,t,Case=1; cin.sync_with_stdio(false); //cin>>t; while(cin>>str[0]&&str[0]!="9") { int i=1; root=new node(); int flag=0; Insert(str[0]); while(cin>>str[i++]&&str[i-1]!="9") Insert(str[i-1]); for(int j=0;j<i-1;j++) { if(!Query(str[j])) { flag=1; break; } } if(flag) cout<<"Set "<<Case++<<" is not immediately decodable"<<endl; else cout<<"Set "<<Case++<<" is immediately decodable"<<endl; Delete(root); } return 0; }
相关文章推荐
- Linux编译+更换3.4版本内核教程 [复制链接]
- 初学者快速学习使用Logcat进行简单调试
- win2008,IP安全策略下配置一个IP段
- 【UVa 10815】Andy's First Dictionary
- Exchanger两个线程之间交换数据
- 合并merge into
- oracle表空间使用率统计查询
- Android自定义类似ProgressDialog效果的Dialog
- cocos2d-x《农场模拟经营养成》游戏完整源码
- android实现activity半透明色
- HDU 3342 -- Legal or Not【裸拓扑排序 &&水题 && 邻接表实现】
- SVN的使用方法
- IT运维的五大基础知识
- 九度oj 1027
- java 如何跳出当前的多重嵌套循环
- linux,windows 可执行文件(ELF、PE)
- POJ----- 3299 Humidex
- winfrom 实现读取修改xml
- Android开源项目分类汇总
- c语言语法(三)