hdu 1305 Immediate Decodability

字典树。建树的过程中,一边建树一边判断有没有重复前缀的。

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
char s[1000]; int i, j, summ;
struct nn{ int tot, ling, yi; }dt[50000];
int main()
{
    int aa = 1;
    while (~scanf("%s", s))
    {
        if (strcmp("9", s) == 0) break;
        summ = 0;
        int flag = 0;
        for (i = 0; i < 50000; i++) dt[i].ling = -1, dt[i].yi = -1, dt[i].tot = 0;

        int t = 0;
        int yy = strlen(s);
        for (i = 0; s[i]; i++)
        {
            int jian = 0;
            if (s[i] == ‘0‘)
            {
                if (dt[t].ling == -1)//建立节点
                {
                    summ++;
                    dt[t].ling = summ;
                    dt[summ].tot = 1;
                    jian = 1;
                }
                t = dt[t].ling; //跳到这个节点
                if ( (jian == 0 && dt[t].tot == 1 && i == yy - 1)) flag = 1;
                if (i == yy - 1) dt[t].tot = 5201314;
            }
            else if (s[i] == ‘1‘)
            {
                if (dt[t].yi == -1)//建立节点
                {
                    summ++;
                    dt[t].yi = summ;
                    dt[summ].tot = 1;
                    jian = 1;
                }
                t = dt[t].yi;//跳到这个节点
                if ( (jian == 0 && dt[t].tot == 1 && i == yy - 1)) flag = 1;
                if (i == yy - 1) dt[t].tot = 5201314;
            }
        }

        while (1)
        {
            scanf("%s", s);
            if (strcmp("9", s) == 0) break;
            int t = 0;
            int yy = strlen(s);
            for (i = 0; s[i]; i++)
            {
                int jian = 0;
                if (s[i] == ‘0‘)
                {
                    if (dt[t].ling == -1)//建立节点
                    {
                        summ++;
                        dt[t].ling = summ;
                        dt[summ].tot = 1;
                        jian = 1;
                    }
                    t = dt[t].ling; //跳到这个节点
                    if (dt[t].tot == 5201314 || (jian == 0 && dt[t].tot == 1 && i == yy - 1)) flag = 1;
                    if (i == yy - 1) dt[t].tot = 5201314;
                }
                else if (s[i] == ‘1‘)
                {
                    if (dt[t].yi == -1)//建立节点
                    {
                        summ++;
                        dt[t].yi = summ;
                        dt[summ].tot = 1;
                        jian = 1;
                    }
                    t = dt[t].yi;//跳到这个节点
                    if (dt[t].tot == 5201314 || (jian == 0 && dt[t].tot == 1 && i == yy - 1)) flag = 1;
                    if (i == yy - 1) dt[t].tot = 5201314;
                }
            }
        }
        if (flag == 0) printf("Set %d is immediately decodable\n", aa++);
        else if (flag == 1) printf("Set %d is not immediately decodable\n", aa++);
    }
    return 0;
}
时间: 2024-08-25 01:14:12

hdu 1305 Immediate Decodability的相关文章

HDU 1305 Immediate Decodability 可直接解码吗?

题意:一个码如果是另一个码的前缀,则 is not immediately decodable,不可直接解码,也就是给一串二进制数字给你,你不能对其解码,因解码出来可能有多种情况. 思路:将每个码按长度从短到长排序,逐个与其后面的码比较,若前面相同位数完全一样了,那就可以输出了. 1 #include <iostream> 2 #include <stdio.h> 3 #include <string> 4 #include <map> 5 #include

Immediate Decodability HDU - 1305(模板trie)

求这些01串是否有一个是另一个的前缀.. 就是求次数就好了嘛...emm... 网上竟然都用指针写.... #include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #define maxn 2000010 #define mem(a, b) memset(a, b, sizeof(a)) using namespace std; int tot, n, m, rt;

poj1056 &amp; hdu1305 &amp; zoj1808 Immediate Decodability(字典树变形)

题目链接 poj  1056 :http://poj.org/problem?id=1056 hdu 1305 :http://acm.hdu.edu.cn/showproblem.php?pid=1305 zoj  1808 :http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=808 Description An encoding of a set of symbols is said to be immediately de

【数据结构】Trie树

1.Trie树简介 Trie树,又称字典树.前缀树,被用于信息检索(information retrieval)的数据结构.Trie一词便来自于单词retrieval.基本思想:用字符串的公共前缀降低查询时间.比如,在最优的查询二叉树中查询关键字的时间复杂度为M * log N,M是字符串最大长度,N为字符串数量:而用Trie树时,只需O(M)时间. [1] 中给出一个简单Trie树例子,蓝色表示一个单词结尾:该Trie树存储的单词为the, their, there, a, any, answ

转载:hdu 题目分类 (侵删)

转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028.1029. 1032.1037.1040.1048.1056.1058.1061.1070.1076.1089.1090.1091.1092.1093. 1094.1095.1096.1097.1098.1106.1108.1157.116

HDU分类

模拟题, 枚举 1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 1049 1050 1057 1062 1063 1064 1070 1073 1075 1082 1083 1084 1088 1106 1107 1113 1117 1119 1128 1129 1144 1148 1157 1161 1170 1172 1177 1197 1200 1201 12

POJ 1056 IMMEDIATE DECODABILITY (字典树)

IMMEDIATE DECODABILITY Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12972   Accepted: 6222 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 s

hdu 1247:Hat’s Words(字典树,经典题)

Hat's Words Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7282    Accepted Submission(s): 2639 Problem Description A hat's word is a word in the dictionary that is the concatenation of exactly

HDU——PKU题目分类

HDU 模拟题, 枚举1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 1049 1050 1057 1062 1063 1064 1070 1073 1075 1082 1083 1084 1088 1106 1107 1113 1117 1119 1128 1129 1144 1148 1157 1161 1170 1172 1177 1197 1200 1201