2018 沈阳赛区网络预赛 I.Lattice's basics in digital electronics(模拟)

版权声明:转载请标明出处哦 https://blog.csdn.net/PleasantlY1/article/details/82532915

LATTICE is learning Digital Electronic Technology. He is talented, so he understood all those pieces of knowledge in 10−910^{-9}10−9 second. In the next 10−910^{-9}10−9 second, he built a data decoding device that decodes data encoded with his special binary coding rule to meaningful words.

His coding rule is called "prefix code", a type of code system (typically a variable-length code) 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. Note that his code is composed of only 000 and 111.

LATTICE's device only receives data that perfectly matches LATTICE's rules, in other words, people who send message to LATTICE will always obey his coding rule. However, in the process of receiving data, there are errors that cannot avoid, so LATTICE uses parity check to detect error bytes, after every 888-bit data there is 111 bit called parity bit, which should be '0' if there are odd number of '1's in the previous 888 bits and should be '1' if there are even number of '1's. If the parity bit does not meet the fact, then the whole 999 bits (including the parity bit) should be considered as invalid data and ignored. Data without parity bit is also considered as invalid data. Parity bits will be deleted after the parity check.

For example, consider the given data "101010101010101010101010", it should be divided into 333 parts:"101010101","010101010" and "101010". For the first part, there are 444 '1's in the first 888 bits, and parity bit is '1', so this part passed the check. For the second part, there are 444 '1's and parity bit is '0', so this part failed the check. For the third part, it has less than 999 bits so it contains no parity bit, so this part also failed the check. The data after parity check is "10101010", which is the first 888 bits of first part.

Data passed the parity check will go into a process that decodes LATTICE's code. The process is described in the following example: consider a situation that, "010" represents 'A' and "1011" represents 'B', if the data after parity check is "01010110101011010010", it can be divided into "010"+"1011"+"010"+"1011"+"010"+"010", which means "ABABAA" . LATTICE's device is so exquisite that it can decode all visible characters in the ASCII table .

LATTICE is famous for his Talk show, some reporters have sneaked into his mansion, they stole the data LATTICE to decode in hexadecimal, the coding rule consists of NNN pairs of corresponding relations from a bit string SiS_iSi​ to an ASCII code CiC_iCi​, and the message length MMM, they want to peek his privacy so they come to you to write a program that decodes messages that LATTICE receives.

Input

The first line an integer T (T<35)T\ (T<35)T (T<35) represents the number of test cases.

Every test case starts with one line containing two integers, M (0<M≤100000)M\ (0<M\leq100000)M (0<M≤100000), the number of original characters, and N (1≤N≤256)N\ (1\leq N \leq 256)N (1≤N≤256), then NNN lines, every line contains an integer CiC_iCi​, and a string Si(0<∣Si∣≤10)S_i(0<|S_i|\leq 10)Si​(0<∣Si​∣≤10), means that SiS_iSi​represents CiC_iCi​, the ASCII code to a visible character and SiS_iSi​ only contains '0' or '1' and there are no two numbers iii and jjj that SiS_iSi​ is prefix of SjS_jSj​.

Then one line contains data that is going to be received in hexadecimal. (0<∣data∣<200000)(0<|data|<200000)(0<∣data∣<200000).

Output

For each test case, output the decoded message in a new line, the length of the decoded message should be the same with the length of original characters, which means you can stop decoding having outputted MMM characters. Input guarantees that it will have no less than MMM valid characters and all given ASCII codes represent visible characters.

Hint

Lattice's encoding rule for test case 222:

ASCII code character lattice's code
494949 111 000100010001
505050 222 010010100101001
515151 333 011011011

the device takes this input in hex

14DB24722698

input in binary

0001 0100 1101 1011 0010 0100 0111 0010 0010 0110 1001 1000

formatted into 666 lines, each line contains 888 data bits and one parity bit

00010100 1
10110110 0
10010001 1
01101001 1
000

parity check of the third line and the last line failed, so ignore those two lines.parity bits should also be ignored.

00010100
10110110
10010001
01101001

arrange those bits by the rules informed

0001 01001 011 011 01001 0001 011 01001

output the result

12332132

样例输入

2
15 9
32 0100
33 11
100 1011
101 0110
104 1010
108 00
111 100
114 0111
119 0101
A6Fd021171c562Fde1
8 3
49 0001
50 01001
51 011
14DB24722698

样例输出

hello world!!!!
12332132

题目大意:给出一个映射(左为ascll值),然后给出一个16进制的数,要求先将16进制转化为2进制然后每9位判断,若前8位有奇数个1且第9位为0则这个子串取,若前8位有偶数个1且第9 位为1也取。取出的串在映射中进行查找,输出对应ascll值的字符。

思路:感觉好长好长的题目,对英语的恐惧心理,唉。导致一直没敢看题,赛后几分钟队友A了。。。。这题标准的模拟题,需要注意字符串的清空与位判断,直接上map就行。具体需要注意的地方参见代码吧。

代码如下:

#include<set>
#include<map>
#include<list>
#include<deque>
#include<cmath>
#include<queue>
#include<stack>
#include<string>
#include<vector>
#include<stdio.h>
#include<sstream>
#include<stdlib.h>
#include<string.h>
//#include<ext/rope>
#include<iostream>
#include<algorithm>
#define pi acos(-1.0)
#define INF 0x3f3f3f3f
#define per(i,a,b) for(int i=a;i<=b;++i)
#define LL long long 
#define swap(a,b) {int t=a;a=b;b=t} 
using namespace std;
//using namespace __gnu_cxx;
int main()
{
	int n,m,k,x;
	string s,s1,s2,s3,s4;
	map<char,string>p1;//字符串转换
	p1['0']="0000";//这样转换成2进制感觉方便
	p1['1']="0001";
	p1['2']="0010";
	p1['3']="0011";
	p1['4']="0100";
	p1['5']="0101";
	p1['6']="0110";
	p1['7']="0111";
	p1['8']="1000";
	p1['9']="1001";
	p1['A']="1010";
	p1['B']="1011";
	p1['C']="1100";
	p1['D']="1101";
	p1['E']="1110";
	p1['F']="1111";
	scanf("%d",&n);
	while(n--)
	{
		s.clear();
		s1.clear();s2.clear();
		s3.clear();s4.clear();
		scanf("%d%d",&m,&k);
		map<string,int>p;//ascll码值对应的关系
		per(i,1,k)
		{
			cin>>x>>s;
			p[s]=x;
		}
		cin>>s1;	
		per(i,0,s1.size()-1)
		{
			if(s1[i]>='a'&&s1[i]<='f') s1[i]=s1[i]-'a'+'A';//注意!转换大小写
			s2+=p1[s1[i]];
		}
		for(int i=0;i<s2.size();i+=9)
		{
			if(i+9>s2.size()) break;//注意!最后一段若长度不够9是要舍弃的
			int fag=0,k=0;
			if(s2[i+8]=='1') fag=1;
			per(j,i,i+7)
			{
				s4+=s2[j];
				if(s2[j]=='1') k++;
			}
                        //cout<<s4<<"?"<<endl;
			if(k%2==0&&k!=1&&fag==1) s3+=s4;//注意k==1的情况
			if(k%2!=0&&fag==0) s3+=s4;
			s4.clear();//注意清空
		}
                //cout<<s3<<"&"<<endl; 
		for(int i=0,j=0;i<s3.size()&&j<m;i++)//注意满足题意中的输出m个字符,长了需要掐掉
		{
			s4+=s3[i];
			if(p[s4])
			{
				printf("%c",p[s4]);j++;
				s4.clear();
			}
		}
		cout<<endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/PleasantlY1/article/details/82532915