poj 1056 IMMEDIATE DECODABILITY & poj 3630 Phone List
2014-04-06 21:44
369 查看
给你一些01串,判断是否会出现其中一个串刚好是另外一个串的前缀。
数据规模比较小,不过为了练习trie还是用了trie
先把所有的01串都插入到树中,然后依次查找每一个串,看路径上是否有别的串
poj 3630把01串改成了电话号码,完全一样的思路囧
数据规模比较小,不过为了练习trie还是用了trie
先把所有的01串都插入到树中,然后依次查找每一个串,看路径上是否有别的串
#include <cstdio> #include <sstream> #include <fstream> #include <cstring> #include <iostream> #include <algorithm> #include <map> #include <cctype> #include <ctime> #include <set> #include <climits> #include <vector> #include <queue> #include <stack> #include <cstdlib> #include <cmath> #include <string> #include <list> #define INPUT_FILE "in.txt" #define OUTPUT_FILE "out.txt" using namespace std; typedef long long LL; const int INF = INT_MAX / 2; void setfile() { freopen(INPUT_FILE,"r",stdin); freopen(OUTPUT_FILE,"w",stdout); } const int maxn = 105; const int maxlen = 205; const int maxnode = maxn * maxlen + 5; const int sigma_size = 2; char dict[maxn][maxlen]; bool dok = true; struct Trie { int ch[maxnode][sigma_size],val[maxnode * sigma_size],sz; Trie() { sz = 1; memset(ch[0],0,sizeof(ch[0])); } void clear() { sz = 1; memset(ch,0,sizeof(ch)); memset(val,0,sizeof(val)); } inline int idx(char c) { return c - '0'; } void insert(char *str) { int len = strlen(str); int u = 0; for(int i = 0;i < len;i++) { int c = idx(str[i]); if(ch[u][c] == 0) { memset(ch[sz],0,sizeof(ch[sz])); val[sz] = 0; ch[u][c] = sz++; } u = ch[u][c]; } if(val[u] == 0) { val[u] = 1; } else dok = false; } bool query(char *str) { int len = strlen(str); int u = 0; for(int i = 0;i < len;i++) { int c = idx(str[i]); if(val[u] == 1) return false; u = ch[u][c]; } return true; } }; Trie trie; int main() { int kase = 1,cnt = 0; while(~scanf("%s",dict[cnt++])) { if(dict[cnt - 1][0] != '9') { trie.insert(dict[cnt - 1]); } else { bool ok = true; for(int i = 0;i < cnt - 1 && ok;i++) { ok = ok && trie.query(dict[i]); } if(ok && dok) { printf("Set %d is immediately decodable\n",kase++); } else { printf("Set %d is not immediately decodable\n",kase++); } dok = true; trie.clear(); cnt = 0; } } return 0; }
poj 3630把01串改成了电话号码,完全一样的思路囧
#include <cstdio> #include <sstream> #include <fstream> #include <cstring> #include <iostream> #include <algorithm> #include <map> #include <cctype> #include <ctime> #include <set> #include <climits> #include <vector> #include <queue> #include <stack> #include <cstdlib> #include <cmath> #include <string> #include <list> #define INPUT_FILE "in.txt" #define OUTPUT_FILE "out.txt" using namespace std; typedef long long LL; const int INF = INT_MAX / 2; void setfile() { freopen(INPUT_FILE,"r",stdin); freopen(OUTPUT_FILE,"w",stdout); } const int maxn = 10000 + 5; const int maxlen = 11; const int maxnode = maxn * maxlen; const int sigma_size = 10; char dict[maxn][maxlen]; struct Trie_Node { int next[sigma_size],val; }; Trie_Node node[maxnode]; int trie_sz; void init() { trie_sz = 1; memset(&node[0],0,sizeof(Trie_Node)); } inline int idx(char c) { return c - '0'; } bool insert(char *str) { int len = strlen(str); int u = 0; for(int i = 0;i < len;i++) { int c = idx(str[i]); if(node[u].next[c] == 0) { memset(&node[trie_sz],0,sizeof(Trie_Node)); node[u].next[c] = trie_sz; node[trie_sz].val = 0; trie_sz++; } u = node[u].next[c]; } if(node[u].val == 0) node[u].val = 1; else return false; return true; } bool query(char *str) { int len = strlen(str); int u = 0; for(int i = 0;i < len;i++) { int c = idx(str[i]); if(node[u].val == 1) return false; u = node[u].next[c]; } return true; } int main() { int t,n; scanf("%d",&t); while(t--) { init(); scanf("%d",&n); bool ok = true; for(int i = 0;i < n;i++) { scanf("%s",dict[i]); ok = ok && insert(dict[i]); } for(int i = 0;i < n;i++) { ok = ok && query(dict[i]); } if(ok) puts("YES"); else puts("NO"); } return 0; }
相关文章推荐
- hdu 1672Phone List&&poj 3630Phone List
- HDU 1671 Phone List && POJ 3630 Phone List
- POJ1056 IMMEDIATE DECODABILITY & POJ3630 Phone List
- hdu 1671 Phone List && POJ 3630 Phone List(静态申请空间)
- Trie树学习小记 Poj 3630 & Hdu1671 Phone List
- HDU 1671 & POJ 3630 Phone List(字典树基础题)
- POJ3630 Phone List 题解&代码
- poj 3630 / hdu 1671 Phone List 【Trie字典树 动态创建&静态创建】
- POJ 3630 && HDU 1671 Phone list(静态字典树)
- POJ 3630 Phone List/POJ 1056 【字典树】
- hdu 1671 & poj 3630 Phone List
- POJ 3630 && HDU 1671 Phone list(静态字典树)
- 【POJ 3630】Phone List 中文题意&题解&代码(C++)
- HDU 1671 & POJ 3630 Phone List
- hdu 1671&&poj 3630 Phone List 【字典树】
- HDU 1671 && POJ 3630 Phone List (Trie树 好题)
- POJ_3630_Phone List_Trie树
- 【POJ 3630 Phone List】+ 字典树
- poj 3630 Phone List(字典树)
- POJ 3630/HDU 1671 Phone List(字典树)