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

upc组队赛15 Lattice's basics in digital electronics【模拟】的更多相关文章

  1. ACM-ICPC2018沈阳网络赛 Lattice's basics in digital electronics(模拟)

    Lattice's basics in digital electronics 44.08% 1000ms 131072K   LATTICE is learning Digital Electron ...

  2. ACM-ICPC 2018 沈阳赛区网络预赛 I. Lattice's basics in digital electronics 阅读题加模拟题

    题意:https://nanti.jisuanke.com/t/31450 题解:题目很长的模拟,有点uva的感觉 分成四步 part1 16进制转为二进制string 用bitset的to_stri ...

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

    原题链接:https://nanti.jisuanke.com/t/31450 附上队友代码:(感谢队友带飞) #include <bits/stdc++.h> using namespa ...

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

    https://nanti.jisuanke.com/t/31450 题意 给出一个映射(左为ascll值),然后给出一个16进制的数,要求先将16进制转化为2进制然后每9位判断,若前8位有奇数个1且 ...

  5. 【ACM-ICPC 2018 沈阳赛区网络预赛 I】Lattice's basics in digital electronics

    [链接] 我是链接,点我呀:) [题意] [题解] 每个单词的前缀都不同. 不能更明示了... 裸的字典树. 模拟一下.输出一下就ojbk了. [代码] #include <bits/stdc+ ...

  6. upc组队赛15 Supreme Number【打表】

    Supreme Number 题目链接 题目描述 A prime number (or a prime) is a natural number greater than 1 that cannot ...

  7. upc组队赛15 Made In Heaven【第K短路 A*】

    Made In Heaven 题目描述 One day in the jail, F·F invites Jolyne Kujo (JOJO in brief) to play tennis with ...

  8. 沈阳网络赛I-Lattice's basics in digital electronics【模拟】

    42.93% 1000ms 131072K LATTICE is learning Digital Electronic Technology. He is talented, so he under ...

  9. upc组队赛3 Chaarshanbegaan at Cafebazaar

    Chaarshanbegaan at Cafebazaar 题目链接 http://icpc.upc.edu.cn/problem.php?cid=1618&pid=1 题目描述 Chaars ...

随机推荐

  1. hive 排序

    1.全局排序(order by) Order by:全局排序,只有一个reducer ASC(ascend):升序(默认) DESC(descend):降序 2.每个MR内部排序(sort by) s ...

  2. aspnet core in docker

    1 创建一个文件夹(app), 将项目发布后的文件放入该文件夹中 并且创建Dockerfile文件 2 打开Dockerfile文件,编辑一下内容 #基于 `microsoft/dotnet:-cor ...

  3. 常用的AJAX弹出层代码

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. 【五一qbxt】day4 数论知识

    这些东西大部分之前都学过了啊qwq zhx大概也知道我们之前跟着他学过这些了qwq,所以: 先讲新的东西qwq:(意思就是先讲我们没有学过的东西) 进制转换 10=23+21=1010(2) =32+ ...

  5. python依赖包整体迁移方法(pip)

    做个记录 python依赖包整体迁移方法

  6. <一> idea+gradle+springboot创建项目

    转载自https://windcoder.com/springbootchutan-chuangjianxiangmu 前言 一边学习公司用到的技术,一边重构小程序后端,从而更好的理解公司搭建的框架. ...

  7. 常见前端HTML5面试题

    1.H5新标签新特性 新标签:header,nav,footer,aside,article,section,Canvas,audio,video 新特性:localStorag, sessionSt ...

  8. Tomcat的用途

    总结: 这篇文章主要反思了Tomcat的作用.本文主要是自己的一个思考过程,不是严谨地介绍和详细总结Tomcat使用方法的文章.最后尝试利用tomcat的知识,以URL的形式来访问文件夹(在浏览器的地 ...

  9. Mac版Navicat Premium激活教程

    工具: Navicat Premium12.0.20 安装包 下载注册机工具包 链接:https://pan.baidu.com/s/1NS8gk780ds1Xn-zHrSIzIw  密码:dvke ...

  10. 微信小程序(11)--购物车

    今天记录一下购物车案例,实现购物车的全选,单选,数量加一减一,金额总数,以及清空购物车. <view class="main"> <!-- hasList 列表是 ...