ACM-ICPC 2018 沈阳赛区网络预赛 I. Lattice's basics in digital electronics 字典树

LATTICE is learning Digital Electronic Technology. He is talented, so he understood all those pieces of knowledge in 10^{-9}10
−9
second. In the next 10^{-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 00 and 11.

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 88-bit data there is 11 bit called parity bit, which should be ‘0’ if there are odd number of '1’s in the previous 88 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 99 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 33 parts:“101010101”,“010101010” and “101010”. For the first part, there are 44 '1’s in the first 88 bits, and parity bit is ‘1’, so this part passed the check. For the second part, there are 44 '1’s and parity bit is ‘0’, so this part failed the check. For the third part, it has less than 99 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 88 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 NN pairs of corresponding relations from a bit string S_iS
i
​ to an ASCII code C_iC
i
​ , and the message length MM, 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) represents the number of test cases.

Every test case starts with one line containing two integers,$ M\ (0<M\leq100000)M (0<M≤100000),$ the number of original characters, and N\ (1\leq N \leq 256)N (1≤N≤256), then NN lines, every line contains an integer C_iC
i
​ , and a string S_i(0<|S_i|\leq 10)S
i
​ (0<∣S
i
​ ∣≤10), means that S_iS
i
​ represents C_iC
i
​ , the ASCII code to a visible character and S_iS
i
​ only contains ‘0’ or ‘1’ and there are no two numbers ii and jj that S_iS
i
​ is prefix of S_jS
j
​ .

Then one line contains data that is going to be received in hexadecimal. (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 MM characters. Input guarantees that it will have no less than MM valid characters and all given ASCII codes represent visible characters.

Hint
Lattice’s encoding rule for test case 22:

ASCII code character lattice’s code
4949 11 00010001
5050 22 0100101001
5151 33 011011
the device takes this input in hex

1
14DB24722698
input in binary

1
0001 0100 1101 1011 0010 0100 0111 0010 0010 0110 1001 1000
formatted into 66 lines, each line contains 88 data bits and one parity bit

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.

1
00010100
2
10110110
3
10010001
4
01101001
arrange those bits by the rules informed

1
0001 01001 011 011 01001 0001 011 01001
output the result

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
题目来源
ACM-ICPC 2018 沈阳赛区网络预赛

在比赛的时候一直在搞那道模板题,但是一直做不出来,估计是网上找的模板太差了,,赛后看了一下这个题发现就是裸的字典树,然后就很尴尬了hh

#include<bits/stdc++.h>
using namespace std;
int Map[200005*4][2],nm[200005*4];
int cnt,n,m;
void add(char *s1,int num)
{
    int now=0;
    int len=strlen(s1);
    for(int i=0;i<len;i++)
    {
        int ne=s1[i]-'0';
        if(!Map[now][ne])
            Map[now][ne]=++cnt;
        if(i==len-1)
            nm[Map[now][ne]]=num;
        now=Map[now][ne];
    }
}
int query(char *s1,int &i,int len)
{
    int now=0;
    for(;i<len;i++)
    {
        now=Map[now][s1[i]-'0'];
        if(nm[now])
            return nm[now];
    }
}
char s[200005],ss[200005*4],ans[200005*4];
int main()
{
    int t;
    scanf("%d",&t);
    char z='0',o='1';
    while(t--)
    {
        cnt=0;
        memset(Map,0,sizeof(Map));
        memset(nm,0,sizeof(nm));
        scanf("%d%d",&n,&m);
        char s1[20];
        int num;
        for(int i=1;i<=m;i++)
            scanf("%d%s",&num,s1),add(s1,num);
        scanf("%s",s);
        int len=strlen(s);
        char cc;
        int ct=-1,c;
        for(int i=0;i<len;i++)
        {
            cc=s[i];
            if('0'<=cc&&cc<='9')
                c=cc-'0';
            else if('a'<=cc&&cc<='f')
                c=cc-'a'+10;
            else
                c=cc-'A'+10;
            if((c&8)==8)
                ss[++ct]=o;
            else
                ss[++ct]=z;
            if((c&4)==4)
                ss[++ct]=o;
            else
                ss[++ct]=z;
            if((c&2)==2)
                ss[++ct]=o;
            else
                ss[++ct]=z;
            if((c&1)==1)
                ss[++ct]=o;
            else
                ss[++ct]=z;
        }
        len=strlen(ss);
        ct=0;
        for(int i=0;i<len;i+=9)
        {
            int one=0;
            for(int j=0;j<8;j++)
                if(ss[i+j]=='1')
                    one++;
            if(one%2!=(ss[i+8]-'0')%2)
            {
                for(int j=0;j<8;j++)
                    ans[ct++]=ss[i+j];
            }
        }
        ct--;
        int pos=0;
        num=0;
        while(1)
        {
            int a=query(ans,pos,ct);
            pos++;
            num++;
            printf("%c",a);
            if(pos>=ct||num==n)
                break;
        }
        printf("\n");
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值