Getting started with algorithm competitions classic (version 2nd) Exercise 4

Source: Internet
Author: User
Tags define local
(This question is not difficult, but the bad habits of procrastination and fear have eaten me for more than 10 days.) Related Concepts: difference or: ^ bitwise OR 0 if the two binary digits involved in the calculation are the same; otherwise, the value is 1 parity: parity is a method to verify the correctness of code transmission. The number of "1" in the number of transmitted binary code is odd or even

(This question is not difficult, but the bad habits of procrastination and fear have eaten me for more than 10 days.) Related Concepts: difference or: ^ bitwise OR 0 if the two binary digits involved in the calculation are the same; otherwise, the value is 1 parity: parity is a method to verify the correctness of code transmission. The number of "1" in the number of transmitted binary code is odd or even

(This question is not difficult, but the bad habit of procrastination and fear has eaten me for more than 10 days. I am ashamed)


Related Concepts:

Exclusive or: ^ bitwise OR 0 if the two binary values involved in the operation are the same; otherwise, 1

Parity: parity is a method for verifying the correctness of code transmission. The verification is performed based on the number of "1" in the number of transmitted binary codes being an odd or even number. Odd-number parity is used. What kind of verification is required in advance. It is usually used to set a parity bit so that the number of "1" in this set of code is an odd or even number. If the odd check is used, when the receiving end receives this set of code, check whether the number of "1" is an odd number to determine the correctness of the transfer code.


// # Define LOCAL // # define TESTING # include
 
  
# Include
  
   
Char data [8] [102] [66], disk [8] [6401], data2 [32001], pdata [6402]; const char * hex [] = {"0", "1", "2", "3", "4", "5", "6", "7 ", "8", "9", "A", "B", "C", "D", "E", "F"}; int main () {# ifdef LOCAL freopen ("xt4-7.in", "r", stdin); # endif int d, s, B, kase = 0; char parity [2]; for (;) {kase ++; scanf ("% d", & d); if (! D) break; scanf ("% d % s", & s, & B, parity); memset (disk, 0, sizeof (disk); memset (data2, 0, sizeof (data2); memset (pdata, 0, sizeof (pdata); for (int I = 1; I <= d; I ++) scanf ("% s", disk [I]); // verify that the data is stored in pdata, the general code is stored in data2, the total number of data blocks on each disk of B, and the number of d disks, s data block size int byte = 0, pbyte = 0; for (int I = 1; I <= B; I ++) // process the I th data block {for (int j = 1; j <= d; j ++) // process disk j {if (I % d = j | (I % d = 0 & j = d )) {for (int k = 0; k <s; k ++) {Pdata [pbyte] = disk [j] [s * I-s + k]; pbyte ++ ;}} else {for (int k = 0; k <s; k ++) {data2 [byte] = disk [j] [s * I-s + k]; byte ++ ;}}}} // for (int I = 1; I <= d; I ++) printf ("% d % s \ n", I, disk [I]); // printf ("read data: % s | % s \ n", data2, pdata); // check and restore Data bool valid = true; int check; if (parity [0] = 'E') check = 0; else check = 1; int datas = D-1; for (int I = 1; I <= B; I ++) // process the I-th data block {for (int j = 0; j <s; j ++ )// The j-bit data for processing data blocks {int xn = 0, pxn = 0, presult =-1, xind =-1; if (pdata [I * s-s + j] = 'X') pxn ++; // verify the j-bit data of the I-th data block if x, pxn = 1 for (int k = 0; k <= datas-1; k ++) // {if (data2 [I * s * datas-datas * s + j + k * s] = 'X') {xn ++; if (pxn | xn> = 2) valid = false; else xind = k; if (! Valid) break;} else if (presult <0) presult = data2 [I * s * datas-datas * s + j + k * s]-'0 '; else {presult ^ = data2 [I * s * datas-datas * s + j + k * s]-'0' ;}} if (! Valid) break; if (pxn + xn = 0) & presult ^ check! = Pdata [I * s-s + j]-'0') {valid = false; break;} else if (xn &&! Pxn) {if (presult> = 0) data2 [I * s * datas-datas * s + j + xind * s] = (presult ^ check) ^ (pdata [I * s-s + j]-'0') + '0 '; else data2 [I * s * datas-datas * s + j + xind * s] = check ^ (pdata [I * s-s + j]-'0 ') + '0'; // printf ("-% d: % d \ n ", data2 [I * s * datas-datas * s + j + xind * s], I * s * datas-datas * s + j + xind * s, presult, check, pdata [I * s-s + j]) ;}} if (! Valid) break;} // check and restore Data end // printf ("check and restore results: % s | % s \ n", data2, pdata ); while (strlen (data2) % 4) data2 [strlen (data2)] = '0'; // verify that the data is stored in pdata, and the normal encoding is stored in data2 (! Valid) printf ("Disk set % d is invalid. \ n ", kase); else {printf (" Disk set % d is valid, contents are: ", kase); for (int I = 0; I <= strlen (data2); I + = 4) {int result = 0; for (int k = 0; k <4; k ++) {result = result * 2 + data2 [I + k]-'0';} if (result <16 & result> = 0) printf ("% s ", hex [result]);} printf ("\ n") ;}# ifdef TESTING printf ("% s \ n", data2); printf ("% s \ n ", pdata); printf ("\ n"); # endif} return 0 ;}
  
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.