2018 瀋陽賽區網路預賽 I.Lattice's basics in digital electronics(模擬)

大白QQly成長日記發表於2018-09-08

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;
}

 

相關文章