​ACM-ICPC 2018 沈阳赛区网络预赛 I. Lattice’s basics in digital electronics​

题目

给一个十六进制的数。先算出对应的二进制序列,再做奇偶校验(每9位一组,看最后一位与前 8 位 1 的数量奇偶关系来进行取舍)。最后根据题目给出的编码方式进行解码。

分析

题目太长了。。。。。

读完发现是个大水题。完全按照题目里描述的步骤直接模拟即可。。

注意题目给出的 16 进制序列有可能有大小写混合的情况。

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define INF 0x3f3f3f3f
#define fuck(x) cout<<x<<endl
const int N = 1e2 + 5;
const ll mod = 1e9 + 7;

int t, n, m;
set<string> s;
map<string, int> mp;
string str;
string dir[N] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};

void init(){
s.clear();
mp.clear();
}

void solve(){
string ans = "", ss = "";
for(auto x : str){ // 16 进制转 2 进制
if(x >= 'a' && x <= 'z'){
x -= ('z' - 'Z');
}
if(x >= 'A' && x <= 'F'){
x = char('0' + x - 'A' + 10);
}
ans += dir[char(x - '0')];
}
int cnt = 1, num = 0;
string tmp = ""; // 根据题目要求转换
for(int i = 0; i < ans.size(); i++, cnt++){
if(cnt < 9){
num += (ans[i] == '1' ? 1 : 0);
tmp += ans[i];
}
if(cnt == 9){
if(ans[i]-'0' == !(num&1)){
ss += tmp;
}
cnt = 0;
tmp = "";
num = 0;
}
}
ans = tmp = "";
cnt = 0; // 解码
for(int i = 0; i < ss.size() && cnt < n; i++){
ans += ss[i];
if(s.count(ans)){
tmp += (char)mp[ans];
cnt++;
ans = "";
}
}
cout << tmp << "\n";
}

int main(){
std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> t;
while(t--){
cin >> n >> m;
init();
for (int i = 0, x; i < m; i++){
string y;
cin >> x >> y;
s.insert(y);
mp[y] = x;
}
cin >> str;
solve();
}
return 0;
}

/*
32 0100
33 11
100 1011
101 0110
104 1010
108 00
111 100
114 0111
119 0101

1010 0110 1110 0000 1011 1000 0101 1110

1010 0110 1 1110 0000 0 1000 0100 0 1011 1000 1 `0101 0110 0` 0101 1110 0 01

*/
// 1010011011100000100001001011100001011110