扩大
缩小

TOJ1547. To and Fro

1547.   To and Fro
Time Limit: 1.0 Seconds   Memory Limit: 65536K
Total Runs: 878   Accepted Runs: 688



Mo 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 would write down

t o i o y
h p k n n
e l e a i
r a h s g
e c o n h
s e m o t
n l e w x

Note that Mo includes 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 could 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 would be encrypted as

toioynnkpheleaigshareconhtomesnlewx

Your job is to recover for Larry the original message (along with any extra padding letters) from the encrypted one.

Input

There 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.

Output

Each input set should generate one line of output, giving the original plaintext message, with no spaces.

Sample Input

5
toioynnkpheleaigshareconhtomesnlewx
3
ttyohhieneesiaabss
0

Sample Output

theresnoplacelikehomeonasnowynightx
thisistheeasyoneab
View Code
 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<queue>
 5 using namespace std;
 6 char str[210];
 7 queue<char>q;
 8 int main()
 9 {
10     int n,i,len,lie1,lie2,ok,k;
11     while(cin>>n&&n)
12     {
13      getchar();
14       cin>>str;
15       len=strlen(str);
16       for(i=0;i<len;i++)
17         q.push(str[i]);
18         ok=1;
19         lie1=len/n;
20         //cout<<len<<lie1<<endl;
21       
22     while(!q.empty())
23     {   
24       while(1)                               //依 次 输 出   N 列 
25       {     ok=1;
26             lie2=lie1;
27             while(lie2--)                  //第一次 完成 输出 第一列 
28             { 
29                k=n-1;
30               if(ok==1)               //先输出后进栈 
31               {
32                 cout<<q.front();
33                 q.pop();
34                 ok=0;
35                 while(k--)
36                 {
37                   q.push(q.front());
38                   q.pop();
39                 }
40                 continue;
41               }
42               if(ok==0)               //先进栈后输出 
43               {   k=n-1;
44                   while(k--)
45                     {
46                       q.push(q.front());
47                       q.pop();
48                     }
49                 cout<<q.front();
50                 q.pop();
51                 ok=1;
52                 continue;
53               }
54            } 
55        n--;
56        //printf("n==%d**",n);//
57            if(n==0)break;
58       }
59     }
60     cout<<endl;         
61     }
62     return 0;
63 }
64        

 



posted on 2012-08-02 16:04  LinuxPanda  阅读(207)  评论(2编辑  收藏  举报

导航