UVA740 UVALive5355 Baudot Data Communication Code【编码】
Data are communicated between digital computers as sequences of bits. To provide meaning to a sequence of bits, the bits are grouped to form a data character and an encoding scheme, or translation table, is provided to allow a computer system to translate each group of bits into a character. The ideal encoding scheme will provide a unique code for every possible character to be communicated and stored in the computer, but this requires that each group have a sufficient number of bits for each data character.
A code used early in the data communications industry is the Baudot code. Baudot uses five bits per character, thus allowing up to 32 distinct characters. As a technique used to extend this limitation, the code uses up-shift and down-shift modes as is used on a typewriter. In the Baudot code, each five bits transmitted must be interpreted according to whether they are up-shifted (figures) or down-shifted (letters). The complete BAUDOT code (modified for this problem) is shown in the table below.

For example, the bit pattern 11111 represents up-shift and the bit pattern 11011 represents downshift characters. All characters transmitted after the sequence 11111 but before the shifted sequence 11011 are treated as up-shift characters. All characters transmitted after the sequence 11011 are treated as down-shift characters until the pattern 11111 is recognized.
Input
The input consists of two parts. The first part is the Baudot character set: line one contains the 32 down-shift characters and line two contains the 32 up-shift characters. (Note: spaces are inserted for the shift characters.) The remainder of the input file consists of one or more messages encoded using the Baudot code. Each message will be on a line in the input file. Each line will consist of 1’s and 0’s, with no characters between the bits. There can be up to 80 bits per message.
The input file will be terminated by end-of-file. The initial state of each message should be assumed to be in the down-shift state.
Output
The output should consist of one line of text for each message. The output should contain the character representation, as translated using BAUDOT, of each of the messages.
Sample Input
<T*O HNM=LRGIPCVEZDBSYFXAWJ UQK
>5@9 %,.+)4&80:;3"$?#6!/-2' 71(
100100110011000010011111101110000111110111101
001100001101111001001111100001001100010001100110111100000111
Sample Output
DIAL:911
NOV 5, 8AM
Regionals 1988 >> North America - East Central NA
问题链接:UVA740 UVALive5355 Baudot Data Communication Code
问题简述:(略)
问题分析:
这是一个编解码问题,题目比较长,读懂应该就没有问题了。看代码吧!
程序说明:(略)
参考链接:(略)
题记:(略)
AC的C++语言程序如下:
/* UVA740 UVALive5355 Baudot Data Communication Code */
#include <bits/stdc++.h>
using namespace std;
int convert(string& s)
{
int ans = 0;
for (int i = 0; i < s[i]; i++)
ans = ans * 2 + s[i] - '0';
return ans;
}
int main()
{
string s[2], message;
getline(cin, s[0]);
getline(cin, s[1]);
while(getline(cin, message)) {
string ans;
int state = 0;
if(message.length() % 5) {
cout << endl;
continue;
}
for(int i = 0; message[i]; i+= 5) {
string t = message.substr(i, 5);
if ((state == 1 && t == "11011") || (state == 0 && t == "11111"))
state = 1 - state;
else if (t == "11011" || t == "11111")
;
else
ans += s[state][convert(t)];
}
cout << ans << endl;
}
return 0;
}
浙公网安备 33010602011771号