Trie树模板 POJ1056

IMMEDIATE DECODABILITY

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 12907   Accepted: 6188

Description

An encoding of a set of symbols is said to be immediately decodable if no code for one symbol is the prefix of a code for another symbol. We will assume for this problem that all codes are in binary, that no two codes within a set of codes are the same, that each code has at least one bit and no more than ten bits, and that each set has at least two codes and no more than eight.

Examples: Assume an alphabet that has symbols {A, B, C, D}

The following code is immediately decodable: 
A:01 B:10 C:0010 D:0000

but this one is not: 
A:01 B:10 C:010 D:0000 (Note that A is a prefix of C)

Input

Write a program that accepts as input a series of groups of records from standard input. Each record in a group contains a collection of zeroes and ones representing a binary code for a different symbol. Each group is followed by a single separator record containing a single 9; the separator records are not part of the group. Each group is independent of other groups; the codes in one group are not related to codes in any other group (that is, each group is to be processed independently).

Output

For each group, your program should determine whether the codes in that group are immediately decodable, and should print a single output line giving the group number and stating whether the group is, or is not, immediately decodable.

Sample Input

01
10
0010
0000
9
01
10
010
0000
9

Sample Output

Set 1 is immediately decodable
Set 2 is not immediately decodable

思路:很裸的Trie吧,check给定的字符串有无某一子串是另一子串的前缀1.sort 短的字符串在前 ps.刚开始慌着套模板,忘了排序了2.套模板 ps.‘\0‘表示的val[u] = 1,u为下一个节点

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <stack>
#include <string>
#include <queue>
#include <vector>
#include <algorithm>
#include <ctime>
using namespace std;

#define EdsonLin

#ifdef EdsonLin
#define debug(...) fprintf(stderr,__VA_ARGS__)
#else
#define debug(...)
#endif //EdsonLin

typedef long long ll;
typedef double db;
const int inf = 0x3f3f3f;
const int MAXN = 1e3;
const int MAXNN = 2e6+100;
//const int MAXM = 1e6;
//const int MAXM = 3e4+100;
const int MOD = 1000000007;
const db eps = 1e-3;
#define PB push_back

int readint(){int x;scanf("%d",&x);return x;}
inline int code(char x){return x-‘0‘;};

struct Tire{
    int ch[MAXN][26];
    int val[MAXN];
    int sz;
    bool sg;
   /* Tire(){
        sz = 1;
        sg = true;
        sizeof(ch[0],0,sizeof(ch[0][0]));
    }*/
    void Insert(string &s){
        int n = s.length();
        int u = 0;
        for(int i=0;i<n;i++){
            int c = code(s[i]);
            if(!ch[u][c]){
                memset(ch[sz],0,sizeof(ch[sz]));
                val[u] = 0;
                ch[u][c] = sz++;
            }else if(val[ch[u][c]]==1){
                sg = false;
            }
            u = ch[u][c];
        }
        val[u] = 1;
    }
    void init(){
        sz = 1;
        sg = true;
        memset(ch[0],0,sizeof(ch[0]));
        val[0] = 0;
    }
}solver;

using namespace std;

int main()
{
    string s[MAXN],ts;
    int mc = 0,x;
    while(cin>>ts){
        solver.init();
        x = 0;
        while(ts.compare("9")!=0){
            s[x++] = ts;
            cin>>ts;
        }
        sort(s,s+x);
        for(int i=0;i<x;i++)
            solver.Insert(s[i]);
        if(!solver.sg){
            printf("Set %d is not immediately decodable\n",++mc);
            continue;
        }else{
            printf("Set %d is immediately decodable\n",++mc);
        }
    }
    //cout << "Hello world!" << endl;
    return 0;
}

时间: 2024-08-04 11:30:57

Trie树模板 POJ1056的相关文章

poj3630 Phone List (trie树模板题)

Phone List Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26328   Accepted: 7938 Description Given a list of phone numbers, determine if it is consistent in the sense that no number is the prefix of another. Let's say the phone catalogu

HDU 1251 Trie树模板题

1.HDU 1251 统计难题  Trie树模板题,或者map 2.总结:用C++过了,G++就爆内存.. 题意:查找给定前缀的单词数量. #include<iostream> #include<cstring> #include<cmath> #include<queue> #include<algorithm> #include<cstdio> #define max(a,b) a>b?a:b #define F(i,a,b

Trie树 模板

typedef struct node { int count; struct node *next[MAX]; }Trie; Trie *Newnode()//建立结点&初始化a { int i; Trie *T; T = (Trie *)malloc(sizeof(Trie)); T->count = 0; for(i=0;i<MAX;i++) T->next[i] = NULL; return T; } void creatTrie(Trie *root, char *st

hdu 4828 Xor Sum (trie 树模板题,经典应用)

hdu 4825 题目链接 题意:给定n个数,然后给出m个询问,每组询问一个数x,问n中的数y使得x和y的异或和最大. 思路:字典树..把每个数转化成二进制,注意补全前导0,使得所有数都有相同的位数. 如果想要异或和最大,那么每一位尽可能都是1. 所以做法是,先构建字典树,然后每次find的时候,尽可能按照和当前寻找的数的位相反的位的方向走(如果有的话) 比如当前位是1,那我就往0的方向走. 需要注意的是,多组数据,每次要重新初始化一遍. 做法是 在struct 中重新 root = new N

8.10 trie树模板

字典树是一种很简单的数据结构,就是将字符串存在书上,相同前缀进行压缩 模板题:http://acm.hdu.edu.cn/showproblem.php?pid=1251 #include<bits/stdc++.h> #define fi first #define se second #define rep( i ,x ,y ) for( int i= x; i<= y ;i++ ) #define reb( i ,y ,x ) for( int i= y; i>= x ;i-

trie树模板(统计难题)

统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submission(s): 36675    Accepted Submission(s): 13637 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的

Trie树模板例题

一.模板 结构体 struct Trie{ int val; Trie *nex[26]; Trie(){ val = 0; for(int i = 0; i < 26; ++i) nex[i] = NULL; } }; 建树 void build(string x, Trie *root){ int len = x.size(); for(int i = 0; i < len; ++i){ int cur = x[i] - 'a'; if(root -> nex[cur] == NUL

Trie树模板

struct Trie{ int ch[maxnnode][sigma_size]; int val[maxnnode]; int sz; Trie(){sz=1;memset(ch[0],0,sizeof(ch[0]));} int idx(char c) {return c-'a';} void Insert(char *s,int v){ int u=0,n=strlen(s); for(int i=0;i<n;i++) { int c=idx(s[i]); if(!ch[u][c]){

HiHo1014 : Trie树(Trie树模板题)

描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程的学习道路上一同前进. 这一天,他们遇到了一本词典,于是小Hi就向小Ho提出了那个经典的问题:“小Ho,你能不能对于每一个我给出的字符串,都在这个词典里面找到以这个字符串开头的所有单词呢?” 身经百战的小Ho答道:“怎么会不能呢!你每给我一个字符串,我就依次遍历词典里的所有单词,检查你给我的字符串是不是这个单词的前缀不就是了?” 小Hi笑道:“你啊,还是太年轻了!~假设这本词典里有10万个单