列表

详情


NC202155. Prefix Code

描述

A prefix code is a type of code system distinguished by its possession of the "prefix property'', which requires that there is no whole code word in the system that is a prefix (initial segment) of any other code word in the system. It is trivially true for fixed-length code, so only a point of consideration in variable-length code.
For example, a code with code wordshas the prefix property; a code consisting of does not, because "5'' is a prefix of "59'' and also of "55''. A prefix code is a uniquely decodable code: given a complete and accurate sequence, a receiver can identify each word without requiring a special marker between words. However, there are uniquely decodable codes that are not prefix codes; for instance, the reverse of a prefix code is still uniquely decodable (it is a suffix code), but it is not necessarily a prefix code.
Prefix codes are also known as prefix-free codes , prefix condition codes and instantaneous codes. Although Huffman coding is just one of many algorithms for deriving prefix codes, prefix codes are also widely referred to as "Huffman codes'', even when the code was not produced by a Huffman algorithm. The term comma-free code is sometimes also applied as a synonym for prefix-free codes but in most mathematical books and articles a comma-free code is used to mean a self-synchronizing code, a subclass of prefix codes.
Using prefix codes, a message can be transmitted as a sequence of concatenated code words, without any out-of-band markers or (alternatively) special markers between words to frame the words in the message. The recipient can decode the message unambiguously, by repeatedly finding and removing sequences that form valid code words. This is not generally possible with codes that lack the prefix property, for example : a receiver reading a "1'' at the start of a code word would not know whether that was the complete code word "1'', or merely the prefix of the code word "10'' or "11''; so the string "10'' could be interpreted either as a single codeword or as the concatenation of the words "1" then "0".
The variable-length Huffman codes, country calling codes, the country and publisher parts of ISBNs, the Secondary Synchronization Codes used in the UMTS W-CDMA 3G Wireless Standard, and the instruction sets (machine language) of most computer microarchitectures are prefix codes.
Prefix codes are not error-correcting codes. In practice, a message might first be compressed with a prefix code, and then encoded again with channel coding (including error correction) before transmission.
For any uniquely decodable code there is a prefix code that has the same code word lengths. Kraft's inequality characterizes the sets of code word lengths that are possible in a uniquely decodable code.
In this problem, you can give a code with code words. Each word contains only numbers . You need to check whether the code can meet the prefix property: there is no whole code word in the system that is a prefix of any other code word.

输入描述

The first line of the input gives the numbers of test cases, (). test cases follow.
Each test case consists of one line with one integer (), the number of code words.
Then follows lines with one code word on each line. A code word is a
sequence of at most ten digits.

输出描述

For each test case, output one line containing "Case #x: y", where x is the test case number (starting from 1) and y is "Yes" if the code is a prefix code, or "No" if not.

示例1

输入:

3
2
9
55
4
9
5
59
55
4
01
01
123
321

输出:

Case #1: Yes
Case #2: No
Case #3: No

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++(g++ 7.5.0) 解法, 执行用时: 343ms, 内存消耗: 740K, 提交时间: 2022-10-04 19:08:20

#include<iostream>
#include<algorithm>
using namespace std;
int t,n,cnt;
string s[10040];
bool check(int i,int j){
	for(int x=0;x<s[i].length();x++)
		if(s[i][x]!=s[j][x])
			return false;
	return true;	
}
bool solve(){
	cin>>n;
	for(int i=0,cur;i<n;i++)
		cin>>s[i];
	sort(s,s+n);
	for(int i=1;i<n;i++)
		if(check(i-1,i))
			return false;
	return true;
}
int main()
{
	ios::sync_with_stdio(false);
	cin>>t;
	for(int i=1;i<=t;i++){
		cout<<"Case #"<<i<<": ";
		if(solve())
			cout<<"Yes"<<'\n';
		else
			cout<<"No"<<'\n';
	}
	return 0;
}

C++ 解法, 执行用时: 4821ms, 内存消耗: 5968K, 提交时间: 2022-04-24 20:15:47

#include<bits/stdc++.h>
#define maxn 10005
using namespace std;
int n;
string s[maxn];
void work(int t){
	map<string,int> mp;
	int pd=0;
	int n;scanf("%d",&n);
	for(int i=1;i<=n;i++){
		cin>>s[i];
		if(mp[s[i]]) pd=1;
		mp[s[i]]=1;
	}
	for(int i=1;i<=n;i++){
		string now;
		for(int j=0;j<s[i].length()-1;j++){
			now+=s[i][j];
			if(mp[now]) pd=1;
		}
	}
	if(pd==1) printf("Case #%d: No\n",t);
	else printf("Case #%d: Yes\n",t);
}
int main()
{
	int t;
	cin>>t;
	for(int i=1;i<=t;i++) work(i);
}

Python3(3.5.2) 解法, 执行用时: 2102ms, 内存消耗: 5216K, 提交时间: 2020-02-16 14:34:50

def OK(words):
    words.sort()
    for i in range(len(words)-1):
        if words[i+1].startswith(words[i]):
            return False
    return True

T = int(input())
for t in range(1, T+1):
    N = int(input())
    words = [input() for n in range(N)]
    print("Case #%d: %s"%(t, "Yes" if OK(words) else "No"))

上一题