2371. Show Me The Fax
Constraints
Time Limit: 1 secs, Memory Limit: 256 MB
Description
Input
The first line of input contains a single integer P, (1P1000), which is the number of data sets that follow. Each data set consists of multiple lines. The first line contains two (2) decimal integer values: the problem number, followed by a space, followed by the number of bytes B, (1B5000), to decode. The remaining line(s) contain(s) the data to be decoded. Each line of data to decode contains 80 hexadecimal digits (except the last line, which may contain less). 2 hexadecimal digits are used to represent each byte. Hexadecimal digits are: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
Output
For each data set, there are multiple lines of output. The first line contains a decimal integer giving the data set number followed by a single space, followed by a decimal integer giving the total number of decoded bytes. The remaining lines contain the decoded data, 80 hexadecimal digits per line, except the last line which may contain less.
Sample Input
4 1 2 0007 2 4 00F481A5 3 32 850080FF016666825A0A717273747580080110111384550301020399807700CC 4 2 A568
Sample Output
1 1 07 2 5 F4A5A5A5A5 3 44 0000000000000000FFFFFF66665A5A5A5A5A71727374758008011011135555555555555501020399 777777CC 4 40 68686868686868686868686868686868686868686868686868686868686868686868686868686868
很水,题目意思是编码有重复和不重复的两种,对于前者,考虑两个byte,也就是4个16进制数字,前两个是次数,后两个是内容,对于不重复,算出不重复的次数n,然后后面的n个内容都不重复,也就是2n个数字,判断重复与否就看换成2进制之后的第一位。
输出比较坑爹,没多想,用两次调用,不过效率就不高了
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
string code;
int b_counter;
int p_num;
int b_num;
bool is_cal;
inline int char_to_int(char temp) {
if ('0' <= temp && temp <= '9') {
return temp - '0';
} else {
return temp - 'A' + 10;
}
}
void decode() {
int line_counter = 0;
int hex[8];
for (int i = 0; code[i] != '\0'; ) {
int h = char_to_int(code[i]), l = char_to_int(code[i + 1]);
for (int j = 3; j >= 0; j--) {
hex[j] = h % 2;
hex[j + 4] = l % 2;
h /= 2;
l /= 2;
}
if (hex[0] == 0) { //non-run
int sum = 0;
for (int k = 7; k >= 1; k--) {
sum = sum + hex[k] * (int)pow((double)2, (double)(7 - k));
}
sum = sum + 1;
if (is_cal) {
b_counter += sum;
} else {
for (int k = i + 2; k < i + 2 + 2 * sum; k++) {
cout << code[k];
line_counter++;
if (line_counter == 80) {
line_counter = 0;
cout << endl;
}
}
}
i = i + 2 + 2 * sum;
} else {
int sum = 0;
for (int k = 7; k >= 1; k--) {
sum = sum + hex[k] * (int)pow((double)2, (double)(7 - k));
}
sum = sum + 3;
if (is_cal) {
b_counter += sum;
} else {
for (int k = 0; k < sum; k++) {
cout << code[i + 2] << code[i + 3];
line_counter += 2;
if (line_counter == 80) {
line_counter = 0;
cout << endl;
}
}
}
i = i + 4;
}
}
if (is_cal) {
cout << p_num << " " << b_counter << endl;
} else if (line_counter) {
cout << endl;
}
}
int main() {
int case_num;
cin >> case_num;
for (int case_counter = 1; case_counter <= case_num; case_counter++) {
is_cal = true;
b_counter = 0;
cin >> p_num >> b_num;
int lines = b_num % 40 ? b_num / 40 + 1 : b_num / 40;
string temp;
code.clear();
for (int lines_counter = 0; lines_counter < lines; lines_counter++) {
cin >> temp;
code += temp;
}
decode();
is_cal = false;
decode();
}
return 0;
}