Text Encryption
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65535KB
Total submit users: 28, Accepted users: 28
Problem 12346 : No special judgement
Problem description
To keep privacy of messages and prevent the aliens from reading them, we may use various encryption algorithms. These algorithms encode a message into the so-called ciphertext that is difficult (or impossible) to decode for anyone else than the intended recipient. Transposition ciphers are a type of encryption that do not change the letters of the message but only change their order (&Prime shuffle&Prime the letters). Of course, the shuffling must be reversible to allow later decryption.
In this problem, we will consider a simple transposition cipher which shuffles the letters in such a way that the decryption algorithm always takes every n-th letter. More specifically: when decrypting, the first letter of the ciphertext is taken first, then the next n − 1 letters are (repeatedly) skipped and the next letter taken, and so on until we reach the end of the ciphertext. After that, we repeat the procedure starting with the second letter of the ciphertext, and so on until all letters are used.
Your task is to implement the encryption algorithm for this cipher. For a given message, produce the encrypted text (ciphertext). To make the cipher a little bit stronger, you should convert all letters to uppercase and leave out all spaces between words.

Input
The input contains several messages. Each message is described by two lines. The first line contains one integer number N (1 <=N <= 1000). The second line contains the message. The message will be at most 10 000 characters long, it will only contain letters and spaces, and there will be at least one letter in each message. The last message is followed by a line containing zero.

Output
For each message, output the ciphertext that, after using the described decryption algorithm, will result in the original message (with all spaces removed and all letters in uppercase).

Sample Input
2
CTU Open Programming Contest
7
This is a secret message that noone should ever see Lets encrypt it
15
text too short
0
Sample Output
CMTMUIONPGECNOPNRTOEGSRTA
TESNUECHCAOLERIRGODLYSEENEEPITTEVTTSMHSESIAEAHRETSSTOSN
TEXTTOOSHORT
Problem Source
CTU Open Contest 2011

 

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<ctype.h>
 4 
 5 int main()
 6 {
 7     int t,len,p,i,j,k,l;
 8     char s[10002],b[10002];
 9     while(scanf("%d",&t)!=EOF&&t)
10     { 
11         getchar();
12         gets(s);
13         len=strlen(s);
14         for(i=j=0;i<len;i++)
15         {    
16             if(isalpha(s[i])) s[i]=toupper(s[i]);
17             if(s[i]!=' ') 
18             {
19                 b[j++]=s[i];
20             }    
21         }
22         if(j%t!=0)  p=j/t+1;
23         else p=j/t;
24         if(j<=t)  
25         {
26             for(i=0;i<j;i++)
27             printf("%c",b[i]);
28             printf("\n");
29         }
30         else
31         {
32         for(i=l=0;i<t;i++)
33         for(k=0;k<p;k++)
34         {
35             if(i+k*t<j)
36             {
37                 s[i+k*t]=b[l];
38                 l++;
39             }
40         }    
41         for(i=0;i<j;i++)
42         {
43            printf("%c",s[i]);
44         }
45         printf("\n");
46     }
47     }
48     return 0;
49 }
posted on 2012-07-19 21:14  黑色的铅笔  阅读(204)  评论(0编辑  收藏  举报