程序控

IPPP (Institute of Penniless Peasent-Programmer) Fellow

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: :: 管理 ::

Time limit: 3.000 seconds
限时:3.000秒

 

Problem
问题

You work for a company which uses lots of personal computers. Your boss, Dr Penny Pincher, has wanted to link the computers together for some time but has been unwilling to spend any money on the Ethernet boards you have recommended. You, unwittingly, have pointed out that each of the PCs has come from the vendor with an asynchronous serial port at no extra cost. Dr Pincher, of course, recognizes her opportunity and assigns you the task of writing the software necessary to allow communication between PCs.
你在一家有很多PC机的公司工作。你的老板是Penny Pincher博士,想把所有电脑都连接起来。你提出的建议是购买网卡,但她觉得太贵了。此时,你无意间想到这些计算机都来自同一个供货商,并且都有一个异步通信的串口可以直接使用而无需花钱。Pincher博士采纳了这个主意,并让你编写用于连通计算机(通过串口的异步通信方式)的软件。

You've read a bit about communications and know that every transmission is subject to error and that the typical solution to this problem is to append some error checking information to the end of each message. This information allows the receiving program to detect when a transmission error has occurred (in most cases). So, off you go to the library, borrow the biggest book on communications you can find and spend your weekend (unpaid overtime) reading about error checking.
后来,你在某次通信过程中读取了一些数据位,结果发现传输中出现了错误。解决这个问题的传统办法就是在每条报文的后面都附加一段错误校验信息。这样软件就可以利用这些校验信息来检测传输过程中是否出现了错误(在绝大多数情况下都有效)。在此后的一周里你天天去泡图书馆(在没有加班费的空闲时间),借了最厚的通信书籍查阅有关错误校验的知识。

Finally you decide that CRC (cyclic redundancy check) is the best error checking for your situation and write a note to Dr Pincher detailing the proposed error checking mechanism noted below.
最后你确定使用CRC(循环冗余校验)是解决当前问题的最佳方案,并给Pincher博士递交了一分关于错误校验机制的说明,如下:

CRC Generation
CRC的生成

The message to be transmitted is viewed as a long positive binary number. The first byte of the message is treated as the most significant byte of the binary number. The second byte is the next most significant, etc. This binary number will be called "m" (for message). Instead of transmitting "m" you will transmit a message, "m2", consisting of "m" followed by a two-byte CRC value.
传输的报文可以视为一个很长的二进制数。报文的1个字节是整个二进制数的最高位,第2个字节其次,等等。将此二进制数称为“m”(message, 报文),在传输m时要附加两个字节的CRC编码,称为“m2”。

The CRC value is chosen so that "m2" when divided by a certain 16-bit value "g" leaves a remainder of 0. This makes it easy for the receiving program to determine whether the message has been corrupted by transmission errors. It simply divides any message received by "g". If the remainder of the division is zero, it is assumed that no error has occurred.
选择的CRC码应该能使m2能被一个16位的数“g”整除,这样的话接收方软件就可以非常容易的确定消息在传输过程中是否出错。只需要将收到的每一条消息除以“g”,如果余数为0就表示没有错误发生。

You notice that most of the suggested values of "g" in the book are odd, but don't see any other similarities, so you select the value 34943 for "g" (the generator value).
你除了注意到书中建议的大多数“g”值都是奇数外没发现其它的规律,因此你选择“34943”作为“g”的值。

 

Input and Output
输入与输出

You are to devise an algorithm for calculating the CRC value corresponding to any message that might be sent. To test this algorithm you will write a program which reads lines (each line being all characters up to, but not including the end of line character) as input, and for each line calculates the CRC value for the message contained in the line, and writes the numeric value of the CRC bytes (in hexadecimal notation) on an output line. Each input line will contain no more than 1024 ASCII characters. The input is terminated by a line that contains a # in column 1. Note that each CRC printed should be in the range 0 to 34942 (decimal).
你要设计一种算法,为要发送的所有 报文计算出CRC码。为了测试你的算法,你还要写一个程序来读取每行输入的字串(每行都从开始一直到(但不包括)换行符结束),并为每一行字串表示的消息计算出CRC值,然后对应的输出一行,打印出CRC字节的数值。每行输入都不会超过1024个ASCII字符。首字符为#号的一行表示输入结束。注意每个CRC都应该在0到34942(十进制)的范围内。

 

Sample Input
输入示例

this is a test

A
#

 

Sample Output
输出示例

77 FD
00 00
0C 86

 

Analysis
分析

实际在通迅中使用的CRC校验码要比题目中描述的复杂得多,涉及的知识点也很广,这里仅介绍与题目相关的算法,了解其它内容请参见“循环冗余校验”(维基百科)。

本题最主要的算法就是将字符串看作一个很长的整数,然后执行除法取余。简单来讲,就是要实现大数的除法。我们先来看10进制的例子,请用笔计算:2357 ÷ 6。我们只关心余数,第一次在3上面试商3,下面得余数5;用5 × 10 + 5 = 55作为第二次被除数,商9得余数1;用1 × 10 + 7 = 17作第三次被除数,商2余5。注意到每次被除数的每一位去除以除数6得到的余数都要累计到下一位(乘以10再加下一位)。只要能保证被除数中每一位都大于除数,就可以避免出现借位的情况。将笔算除法推广到n进制,即得到大数除法。设p为一个n进制整数作为被除数,q为小于n的除数(即选择的校验码生成数):

p = a0n0 + a1n1 + ... + aknk

现在要求的是p ÷ q的余数rk,即r0 = p mod q,先从rk开始向前计算:

rk = aknk mod q
rk-1 = (rkn + ak-1nk-1) mod q
...
r0 = (r1n + a0n0) mod q

根据此递推公式就很容易处理大数的除法了。为了方便实现,在程序中可以使用216=65535进制或232=4294967295进制。这样在计算余数时,无需将上一个r乘以进制,只要左移16位或32位即可,最大限度的利用了寄存器和CPU的特性。

求得余数后,下一步就是要将余数转为校验码。设校验码为c,那么应满足:

(a0n0 + a1n1 + ... + aknk + c) mod q = 0

前面已求得:

(a0n0 + a1n1 + ... + aknk) mod q = r

显然,c的值不止一个,但最小的正值c可用如下公式来计算:

c = q - (nr mod q)

上式很容易理解,就不赘述了。至此,CRC码计算完成。这里还有一个要注意的地方,我们一般写程序和编译的机器环境(包括OJ系统运行的环境)都是x86架构的,也就意味着字节序是little-endian, 即在存储器中的所有整型数值都是高位在前低位在后。比如32位16进制数:AF045Bh,在内存中的顺序应该是:

5B 04 AF 00

如果我们直接将字符串的指针转为int型指针进行计算就会算错。必须先对每一组字节(整型变量大小的一组)先进行反转再作运算。

 

Solution
解答

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
int main(void) {
	typedef unsigned short word;
	char Bits[1032]; //存储输入的字符串
	cin.sync_with_stdio(false); //输入的数据量很大,关闭同步以加速
	cout << setbase(16) << setiosflags(ios::uppercase) << setfill('0');
	//循环处理输入的每一行字符串
	for (string Line; getline(cin, Line) && Line[0] != '#'; cout << endl) {
		word nGen = 34943, nLen = Line.length(), *pBit = (word*)Bits;
		//将字符串转存到静态数组中。x86的CPU字节序为little-endian,
		reverse_copy(Line.begin(), Line.end(), Bits); //反转为正字节序
		*(word*)(&Bits[nLen]) = 0; //将结尾后面的一些位都清零
		nLen = nLen / 2 + (nLen % 2 != 0); //计算作为int数组的长度
		long nRem = 0;//nRem表示余数
		//循环除所有的位,累加进位的余数,生成CRC码
		for (int i = nLen - 1; i >= 0; --i) {
			nRem = ((nRem << 16) + pBit[i]) % nGen;
		}
		if (nRem != 0) { //如果余数不为0,则需构造CRC码,算法见文档
			nRem = nGen - (nRem << 16) % nGen;
		} //下面按要求的格式输出CRC码
		unsigned char* pByte = (unsigned char*)&nRem;
		cout << setw(2) << (int)pByte[1] << ' ' << setw(2) << (int)pByte[0];
	}
	return 0;
}
posted on 2010-08-28 04:50  Devymex  阅读(2231)  评论(1编辑  收藏  举报