Hdu1200 (string up and down)

Source: Internet
Author: User

Problem DescriptionMo and Larry have devised a way of encrypting messages. they first decide secretly on the number of columns and write the message (letters only) down the columns, padding with extra random letters so as to make a rectangular array of letters. for example, if the message is "There's no place like home on a snowy night" and there are five columns, mo wocould write down t o I o yh p k n ne l e a ir a h s ge c o n hs e m o tn l e w x Note that Mo has des only letters and writes them all in lower case. in this example, Mo used the character 'x' to pad the message out to make a rectangle, although he cocould have used any letter. mo then sends the message to Larry by writing the letters in each row, alternating left-to-right and right-to-left. so, the above wocould be encrypted as toioynnkpheleaig?conhtomesnlewx Your job is to recover for Larry the original message (along with any extra padding letters) from the encrypted one. inputThere will be multiple input sets. input for each set will consist of two lines. the first line will contain an integer in the range 2... 20 indicating the number of columns used. the next line is a string of up to 200 lower case letters. the last input set is followed by a line containing a single 0, indicating end of input. outputEach input set shoshould generate one line of output, giving the original plaintext message, with no spaces. sample input5toioynnkpheleaig?conhtomesnlewx3ttyohhieneesiaabss0 Sample Outputtheresnoplacelikehomeonasnowynightxthisistheeasyoneab

# Include <stdio. h> # include <string. h> int main () {int n, I, len, m, e, j; char str [10000], ch [1000] [25]; while (scanf ("% d", & n) = 1 & n) {getchar (); scanf ("% s", str ); len = strlen (str); m = len/n; // split the str string into m small strings if (len % n) m ++; for (I = 0; I <m; I ++) if (I % 2 = 0) // when it can be divisible by 2, it is saved in the forward direction {e = 0; for (j = I * n; j <len & j <(I + 1) * n; j ++) ch [I] [e ++] = str [j];} else // otherwise, {e = 0; for (j = (I + 1) * n-1; j> = I * n; j --) if (str [j]! = '\ 0') ch [I] [e ++] = str [j];} e = len % n; // note that some cannot be wiped out, the last small string is less than n for (j = 0; j <n; j ++) for (I = 0; I <m; I ++) if (I = s-1) // output of the last small string {if (j <e | e = 0) printf ("% c ", ch [I] [j]);} else printf ("% c", ch [I] [j]); printf ("\ n ");}}

 


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.