UVA 644- Immediate Decodability (寻找是否有相同前缀)

Immediate Decodability 

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 a data file. 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.

The Sample Input describes the examples above.

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

题意:对于二进制编码,找是否有有一个二进制编码是另一个的前缀,如果有输出 not  immediately,没有输出 immediately。

思路:暴力比较,也可以用字典树,改天更新

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
using namespace std;
int cmp(char *str1,char *str2)
{
    int i;
    int len,len1,len2;
    len1=strlen(str1);
    len2=strlen(str2);
    len=min(len1,len2);
    for(i=0;i<len;i++)
    {
        if(str1[i]!=str2[i])
            return 1;
    }
    return 0;
}
int main()
{
    char str[20][20];
    int n,i,j;
    int t=1;
    int flag;
    while(scanf("%s",str[0])==1)
    {
        n=1;
        flag=0;
        while(scanf("%s",str[n])&&str[n][0]!='9')
        {
            n++;
        }
        for(i=0;i<n;i++)
        {
            for(j=i+1;j<n;j++)
            {
                if(cmp(str[i],str[j])==0)
                {
                    flag=1;
                    break;
                }
            }
            if(flag)
                    break;
        }
        if(!flag)
            printf("Set %d is immediately decodable\n",t++);
        else
            printf("Set %d is not immediately decodable\n",t++);
    }
    return 0;
}
时间: 2024-10-07 03:39:45

UVA 644- Immediate Decodability (寻找是否有相同前缀)的相关文章

UVa 644 - Immediate Decodability

题目:给你一些01串,判断是不是某些串是其它串的前缀. 分析:字符串,字典树. 首先,将字符串按长度排序,这样前缀一定在前面: 然后,再插入字典树的过程中,判断是否覆盖即可. 说明:注意数组的大小. #include <iostream> #include <cstdlib> #include <cstring> #include <cstdio> using namespace std; char words[1004][104]; /* Trie def

UVA 644 Immediate Decodability (字符处理)

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 sa

UVA - 11354Bond最小生成树,LCA寻找最近公共祖先

/* Author: 2486 Memory: 0 KB Time: 2222 MS Language: C++11 4.8.2 Result: Accepted VJ RunId: 4236841 Real RunId: 15859210 */ #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #include

hihocoder 1496:寻找最大值(高维前缀最大次大值)

[题目链接] https://hihocoder.com/problemset/problem/1496 [题目大意] 给定N个数A1, A2, A3, ... AN, 从中找到两个数Ai和Aj(i≠j)使得乘积Ai*Aj*(Ai&Aj)最大 [题解] 我们可以枚举x&y的结果z,找出两个数x&y==z使得x*y最大,更新答案即可, 条件可以被削弱为z为x&y的子集,这种条件放缩不会导致最优解的丢失, z为x&y的子集等价于z为x的子集并且z为y的子集. 那么我们只

UVA题目分类

题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics 10300 - Ecological Premium 458 - The Decoder 494 - Kindergarten Counting Game 414 - Machined Surfaces 490 - Rotating Sentences 445 - Marvelous Mazes

寒假练习 02

今天刷了小白书的字符串专题,各种WA以及PE.UVaOJ中有时候会把PE判成WA,这样会导致很难查错. UVa 401 这道题目有个坑,只有表格中列出的才是镜像字母,没有列出了的表示没有镜像字母,在这上WA了一次. #include <iostream> #include <string> using namespace std; const char pAlphabet[] = { 'A', '*', '*', '*', '3', '*', '*', 'H', 'I', 'L',

4.7-4.9补题+水题+高维前缀和

题目链接:51nod 1718 Cos的多项式  [数学] 题解: 2cosx=2cosx 2cos2x=(2cosx)^2-2 2cos3x=(2cosx)^3-3*(2cosx) 数归证明2cos(nx)能表示成关于2cosx的多项式,设为f(n) f(1)=x,f(2)=x^2-2(其中的x就是2cosx) 假设n=1~k时均成立(k>=3) 当n=k+1时 由cos((k+1)x)=cos(kx)cos(x)-sin(kx)sin(x) cos((k-1)x)=cos(kx)cos(x)

后缀树

在<字符串匹配算法>一文中,我们熟悉了字符串匹配问题的形式定义: 文本(Text)是一个长度为 n 的数组 T[1..n]: 模式(Pattern)是一个长度为 m 且 m≤n 的数组 P[1..m]: T 和 P 中的元素都属于有限的字母表 Σ 表: 如果 0≤s≤n-m,并且 T[s+1..s+m] = P[1..m],即对 1≤j≤m,有 T[s+j] = P[j],则说模式 P 在文本 T 中出现且位移为 s,且称 s 是一个有效位移(Valid Shift). 比如上图中,目标是找出

从头到尾彻底理解KMP(2014年8月22日版)

从头到尾彻底理解KMP 作者:July 时间:最初写于2011年12月,2014年7月21日晚10点 全部删除重写成此文,随后的半个多月不断反复改进. 1. 引言 本KMP原文最初写于2年多前的2011年12月,因当时初次接触KMP,思路混乱导致写也写得混乱.所以一直想找机会重新写下KMP,但苦于一直以来对KMP的理解始终不够,故才迟迟没有修改本文. 然近期因在北京开了个算法班,专门讲解数据结构.面试.算法,才再次仔细回顾了这个KMP,在综合了一些网友的理解.以及跟我一起讲算法的两位讲师朋友曹博