poj 1056 IMMEDIATE DECODABILITY & poj 3630 Phone List

给你一些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串改成了电话号码,完全一样的思路囧

  1 #include <cstdio>
  2 #include <sstream>
  3 #include <fstream>
  4 #include <cstring>
  5 #include <iostream>
  6 #include <algorithm>
  7 #include <map>
  8 #include <cctype>
  9 #include <ctime>
 10 #include <set>
 11 #include <climits>
 12 #include <vector>
 13 #include <queue>
 14 #include <stack>
 15 #include <cstdlib>
 16 #include <cmath>
 17 #include <string>
 18 #include <list>
 19 
 20 #define INPUT_FILE "in.txt"
 21 #define OUTPUT_FILE "out.txt"
 22 
 23 using namespace std;
 24 
 25 typedef long long LL;
 26 const int INF = INT_MAX / 2;
 27 
 28 void setfile() {
 29     freopen(INPUT_FILE,"r",stdin);
 30     freopen(OUTPUT_FILE,"w",stdout);
 31 }
 32 
 33 const int maxn = 10000 + 5;
 34 const int maxlen = 11;
 35 const int maxnode = maxn * maxlen;
 36 const int sigma_size = 10;
 37 
 38 char dict[maxn][maxlen];
 39 
 40 struct Trie_Node {
 41     int next[sigma_size],val;
 42 };
 43 
 44 Trie_Node node[maxnode];
 45 int trie_sz;
 46 
 47 void init() {
 48     trie_sz = 1;
 49     memset(&node[0],0,sizeof(Trie_Node));
 50 }
 51 
 52 inline int idx(char c) {
 53     return c - '0';
 54 }
 55 
 56 bool insert(char *str) {
 57     int len = strlen(str);
 58     int u = 0;
 59     for(int i = 0;i < len;i++) {
 60         int c = idx(str[i]);
 61         if(node[u].next[c] == 0) {
 62             memset(&node[trie_sz],0,sizeof(Trie_Node));
 63             node[u].next[c] = trie_sz;
 64             node[trie_sz].val = 0;
 65             trie_sz++;
 66         }
 67         u = node[u].next[c];
 68     }
 69     if(node[u].val == 0) node[u].val = 1;
 70     else return false;
 71     return true;
 72 }
 73 
 74 bool query(char *str) {
 75     int len = strlen(str);
 76     int u = 0;
 77     for(int i = 0;i < len;i++) {
 78         int c = idx(str[i]);
 79         if(node[u].val == 1) return false;
 80         u = node[u].next[c];
 81     }
 82     return true;
 83 }
 84 
 85 int main() {
 86     int t,n; scanf("%d",&t);
 87     while(t--) {
 88         init();
 89         scanf("%d",&n);
 90         bool ok = true;
 91         for(int i = 0;i < n;i++) {
 92             scanf("%s",dict[i]);
 93             ok = ok && insert(dict[i]);
 94         }
 95         for(int i = 0;i < n;i++) {
 96             ok = ok && query(dict[i]);
 97         }
 98         if(ok) puts("YES");
 99         else puts("NO");
100     }
101     return 0;
102 }

 

posted @ 2014-04-06 21:44  acm_roll  阅读(202)  评论(0编辑  收藏  举报