upc组队赛15 Lattice's basics in digital electronics【模拟】

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 second. In the next 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 0 and 1.

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 8-bit data there is 11 bit called parity bit, which should be '0' if there are odd number of '1's in the previous 8 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 9 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 3 parts:"101010101","010101010" and "101010". For the first part, there are 4 '1's in the first 8 bits, and parity bit is '1', so this part passed the check. For the second part, there are 4 '1's and parity bit is '0', so this part failed the check. For the third part, it has less than 9 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 8 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 N pairs of corresponding relations from a bit string Si to an ASCII code Ci, and the message length M, they want to peek his privacy so they come to you to write a program that decodes messages that LATTICE receives.

输入

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

Every test case starts with one line containing two integers, M(0<M≤100000), the number of original characters, and N(1≤N≤256), then N lines, every line contains an integer Ci, and a string Si(0<|Si|≤10), means that Si represents Ci, the ASCII code to a visible character and Si only contains '0' or '1' and there are no two numbers i and j that Si is prefix of Sj.

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

输出

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 M characters. Input guarantees that it will have no less than M valid characters and all given ASCII codes represent visible characters.

样例输入

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

题解

一个题目很长的模拟,但实际上真的不难

首先把输入的十六进制字符串转化成二进制,划分每9位一行

如果前8位中1的个数为奇数,并且第9位为0

或者前8位中1的个数为偶数,并且第9位为1

那么保留前8位,否则去掉这一行

将剩下的结果遍历,将二进制转化为输入的对应字符

代码

#include<iostream>
#include<cstdio> //EOF,NULL
#include<cstring> //memset
#include<cstdlib> //rand,srand,system,itoa(int),atoi(char[]),atof(),malloc
#include<limits.h> //INT_MAX
#include<bitset> // bitset<?> n
#include<cmath> //ceil,floor,exp,log(e),log10(10),hypot(sqrt(x^2+y^2)),cbrt(sqrt(x^2+y^2+z^2))
#include<algorithm> //fill,reverse,next_permutation,__gcd,
#include<iomanip> //setw(set_min_width),setfill(char),setprecision(n),fixed,
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<utility>
#include<iterator>
#include<functional>
#include<map>
#include<set>
using namespace std;
#define rep(i,a,n) for(int i=a;i<n;i++)
#define ll long long
#define LL long long
inline ll read(){ll x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;}
#define read read()
#define pb push_back
#define mp make_pair
#define P pair<int,int>
#define PLL pair<ll,ll>
#define PI acos(1.0)
#define eps 1e-6
#define inf 1e17
#define INF 0x3f3f3f3f
#define MOD 998244353
#define mod 1e9+7
#define N 1000005
const int maxn=10000;
int t;
int n,m,x;
string ss;
map<string,int> ap;
map<char,string> sl;
void Init()
{
sl['0'] = "0000";
sl['1'] = "0001";
sl['2'] = "0010";
sl['3'] = "0011";
sl['4'] = "0100";
sl['5'] = "0101";
sl['6'] = "0110";
sl['7'] = "0111";
sl['8'] = "1000";
sl['9'] = "1001";
sl['A'] = "1010";
sl['B'] = "1011";
sl['C'] = "1100";
sl['D'] = "1101";
sl['E'] = "1110";
sl['F'] = "1111";
sl['a'] = "1010";
sl['b'] = "1011";
sl['c'] = "1100";
sl['d'] = "1101";
sl['e'] = "1110";
sl['f'] = "1111";
}
int main()
{
std::ios::sync_with_stdio(false);
Init();
cin >> t;
while(t--)
{
ap.clear();
cin >> n >> m;
while(m--)
{
cin >> x;
cin >> ss;
ap[ss] = x;
}
cin >> ss;
string s;
for(int i = 0; i<ss.length();i++)
{
s+=sl[ss[i]];
}
int len = s.length();
int cnt = len/9;
string str;
for(int i = 0; i < cnt;i++)
{
int sum = 0;
string tmp;
for(int j = 9*i + 0 ; j < 9 * i + 8; j++)
{
if(s[j] =='1') sum++;
tmp+=s[j];
}
if((sum % 2 == 0 && s[9 * i + 8] == '1') ||(sum%2==1 && s[9 * i+ 8] =='0'))
{
str+= tmp;
}
}
string tmp;
string ans;
int tot = 0;
for(int i = 0; i < str.length();i++)
{
tmp+=str[i];
if(ap[tmp])
{
char c;
c = ap[tmp];
ans += c;
tmp.clear();
tot++;
if(tot == n) break;
}
}
cout << ans <<endl;
}
}
上一篇:npm run dev error


下一篇:Getting Started With Node and NPM