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. 推荐一个 Java 里面比较牛逼的公众号!

    今天给大家推荐一个牛逼的纯 Java 技术公众号:Java技术栈,作者:栈长. Java程序员.Java爱好者扫码关注吧! 确实牛逼,几十万人关注了,原创文章350+,好友都 3000+ 关注了. 栈 ...

  2. 洛谷 P1972 [SDOI2009]HH的项链(树状数组,离线)

    传送门 解题思路 因为是求区间的不同种类数,所以我们用树状数组(貌似并没有什么直接联系) (...表示到) 还是和原来一样,用s[i]来表示a[i-lowbit(i)]...a[i]的种类数. 因为有 ...

  3. #python# 使用代理和不使用代理对比

    import urllib.request url='http://httpbin.org/ip' #不使用代理 response1=urllib.request.urlopen(url) #设置代理 ...

  4. Electron 无边框窗口最大化最小化关闭功能

    Electron 无边框窗口最大化最小化关闭功能 目的 实现无边框窗口,并添加最大化最小化和关闭功能 前提 了解Electron 主进程和渲染进程的通讯 了解 BrowserWindow相关功能 操作 ...

  5. CSS样式表能否控制文字禁止选择,复制, 焦点

    div中禁止文字被选择 在做div的点击计数事件时,遇到一个小问题. 因为div里面有文字,所以当点击多次时,特别是鼠标点的比较快的时候,文字会被选中. 查了下,用css和javascript可以实现 ...

  6. java.net.ProtocolException: Exceeded stated content-length of: '13824' bytes

    转自:https://blog.csdn.net/z69183787/article/details/18967927 1. 原因: 因为weblogic会向response中写东西造成的,解决方式是 ...

  7. PyInstaller库的使用

    PyInstaller库的使用 PyInstaller库用于将已经写好的py程序,转换成可以跨平台的可执行文件 使用方式 发布主要借助cmd命令行来实现.在当前目录的powershell下,输入 py ...

  8. geometry_msgs的ros message 类型赋值

    test_custom_particles.cpp // // Created by gary on 2019/8/27. // #include <ros/ros.h> #include ...

  9. 一、简单的图片上传并预览功能input[file]

    一.简单的图片上传并预览功能input[file] <!DOCTYPE html> <html lang="en"> <head> <me ...

  10. nutz包的学习

    参考资料: 1.http://www.nutzam.com/core/nutz_preface.html