hdu 1305 Immediate Decodability
2015-02-20 19:00
369 查看
题意:给你一些0 1串,判断这些0 1串之间没有一个串是另一个串的前缀。
做法:Trie树,用count函数判断以它为头字符串的字符串的个数是否大于1就行了
做法:Trie树,用count函数判断以它为头字符串的字符串的个数是否大于1就行了
#include<iostream> #include<string> #include<vector> #include<cstring> #include<cstdio> using namespace std; #define maxn 500 struct node { char* s; int count1; bool isword; node* next[2]; node() { s=NULL; count1=0; isword=false; memset(next,0,sizeof(next)); } }*root; void insert(node* root,char* s) { node* p=root; for(int i=0;s[i];i++) { int x=s[i]-'0'; p->s=s+i; if(p->next[x]==NULL) p->next[x]=new node; p=p->next[x]; p->count1++; } p->isword=true; } bool Search(node* root,const char* s) { node* p=root; for(int i=0;s[i];i++) { int x=s[i]-'0'; if(p->next[x]==NULL) return false; p=p->next[x]; } return p->isword; } int count(node* root,const char* s) { node* p=root; for(int i=0;s[i];i++) { int x=s[i]-'0'; if(p->next[x]==NULL) return 0; p=p->next[x]; } return p->count1 ; } int main() { char s[maxn]; bool flag; int cas = 1; while(scanf("%s",s) != EOF) { flag = true; root = new node; vector<string> v; insert(root,s); string str(s); v.push_back(str); while(scanf("%s",s)) { if(s[0]=='9') break; insert(root,s); string str1(s); v.push_back(str1); } for(int i = 0;i < (int)v.size(); i++) { if(count(root,v[i].c_str()) > 1) { flag = false; break; } } if(flag) printf("Set %d is immediately decodable\n",cas++); else printf("Set %d is not immediately decodable\n",cas++); } return 0; }
相关文章推荐
- hdu1305之字典树
- HDU1305 Immediate Decodability(水题字典树)
- (UVA)644 (POJ)1056 (HDU)1305 - Immediate Decodability【字典树】
- hdu 1305(字典树)注意本题的输入!!!
- hdu 1305 Immediate Decodability (字典树入门)
- 杭电 hdu 1305 Immediate Decodability
- hdu 1305 Immediate Decodability
- hdu1305-Immediate Decodability 字典树
- 【水题-前缀码】HDU 1305 Immediate Decodability
- hdu 1305 Immediate Decodability E字典树
- hdu1305 Immediate Decodability(字典树)
- Hdu 1305 【字典树】.cpp
- 毕达哥拉斯三元组:poj 1305+佩尔方程:poj 1320,hdu 3292(特殊不定方程)
- HDU - 1305 Immediate Decodability 字典树
- hdu 1305 Immediate Decodability
- Hdu 1305 Immediate Decodability 字典树
- Immediate Decodability(HDU-1305)(字典树的简单应用)
- (step5.1.2)hdu 1305(Immediate Decodability——字典树)
- HDU 1305:Immediate Decodability
- hdu 1305 Immediate Decodability(字典树)