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
x
1
14DB24722698
input in binary
xxxxxxxxxx
1
1
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
xxxxxxxxxx
6
1
00010100 1
2
10110110 0
3
10010001 1
4
10010001 0
5
01101001 1
6
000
parity check of the third line and the last line failed, so ignore those two lines.parity bits should also be ignored.
xxxxxxxxxx
4
1
00010100
2
10110110
3
10010001
4
01101001
arrange those bits by the rules informed
xxxxxxxxxx
1
1
0001 01001 011 011 01001 0001 011 01001
output the result
xxxxxxxxxx
1
1
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
#include<stdio.h>
#include<iostream>
#include<map>
#include<string>
#include<set>
using namespace std;
int main()
{
int n;
map<char,string>p1;
p1['0']="0000";
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";
p1['a']="1010";
p1['b']="1011";
p1['c']="1100";
p1['d']="1101";
p1['e']="1110";
p1['f']="1111";
string s,s1,s2,s3,s4,s5;
scanf("%d",&n);
while(n--)
{
s4.clear();
s3.clear();
s2.clear();
s1.clear();
int m,k,x;
s.clear();
scanf("%d%d",&m,&k);
map<string,int>p;
for(int i=1;i<=k;i++)
{
cin>>x>>s;
p[s]=x;
}
cin>>s1;
for(int i=0;i<s1.size();i++)
{
s2+=p1[s1[i]];
}
//cout<<s2<<endl;
for(int i=0;i<s2.size();i++)
{
if((i+1)%9==0)
{
int sum=0;
for(int j=0;j<s3.size();j++)
{
if(s3[j]=='1')
sum++;
}
if((sum%2==0&&s2[i]=='1')||(sum%2!=0&&s2[i]=='0'))
{
s4+=s3;
}
s3.clear();
i++;
}
if(i<s2.size())
s3+=s2[i];
}
int xm=0;
for(int i=0;i<s4.size();i++)
{
s5+=s4[i];
if(p[s5])
{
printf("%c",p[s5]);
xm++;
s5.clear();
}
if(xm==m)
break;
}
printf("\n");
}
return 0;
}