To and Fro(字符串水题)

To and Fro

点我

Problem Description

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

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 int main()
 6 {
 7     int i,n;
 8     char ch;
 9     char a[11][21];
10     char b[1000];
11     bool flag=0;
12     int p=0,j=0;
13     while(cin>>n&&n)
14     {
15         int k=0;
16         memset(b,0,sizeof(b));
17         memset(a,0,sizeof(a));
18         getchar();
19         gets(b);
20         int len=strlen(b);
21         while(k<=len)
22         {
23             if(!flag)
24             {
25                 for(i=0;i<n;i++)
26                     if(k<=len)
27                         a[p][i]=b[k++];
28                 flag=1;
29                 p++;
30                 continue;
31
32             }
33             if(flag)
34             {
35                 for(i=n-1;i>=0;i--)
36                     if(k<=len)
37                         a[p][i]=b[k++];
38                 flag=0;
39                 p++;
40                 continue;
41             }
42         }
43         for(i=0;i<n;i++)
44         {
45             for(j=0;j<p-1;j++)
46             {
47                 printf("%c",a[j][i]);
48             }
49         }
50         cout<<endl;
51         p=0;
52     }
53 }
 1 #include <stdio.h>
 2 #include <string.h>
 3 int main()
 4 {
 5     char c,s[120][30];
 6     int i,j,k,n,f,count;
 7     while(scanf("%d",&n),n){
 8         f=count=0;
 9         for(k=1;f==0;k++)
10             for(i=1;i<=n&&f==0;i++){
11                 scanf("%c",&c);
12                 if(c==‘\n‘) f=1;
13                 else{
14                     count++;
15                     if(k%2==1) s[k][i]=c;
16                     else s[k][n-i+1]=c;
17                 }
18             }
19         for(i=1;i<=n;i++)
20             for(j=1;j<=count/n;j++)
21                 putchar(s[j][i]);
22         putchar(‘\n‘);
23     }
24     return 0;
25 }
时间: 2024-10-06 18:17:38

To and Fro(字符串水题)的相关文章

1001 字符串“水”题

1001: 字符串“水”题 时间限制: 1 Sec  内存限制: 128 MB提交: 210  解决: 39[提交][状态][讨论版] 题目描述 给出一个长度为 n 的字符串(1<=n<=100000),求有多少个连续字串中所有的字母都出现了偶数次. 输入 第一行一个正整数 T,表示数据组数(1 <= T <= 10). 接下来 T 行,每行有一个只包含小写字母的字符串. 输出 每个答案输出满足要求字符串个数.每个答案占一行. 样例输入 3 a aabbcc abcabc 样例输出

HDU4891_The Great Pan_字符串水题

2014多校第五题,当时题面上的10^5写成105,我们大家都wa了几发,改正后我和一血就差几秒…不能忍 题目:http://acm.hdu.edu.cn/showproblem.php?pid=4891 The Great Pan Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 966    Accepted Submission(s

hdu 5007(字符串水题)

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5007 Post Robot Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 327    Accepted Submission(s): 253 Problem Description DT is a big fan of digital

第十一届“蓝狐网络杯”湖南省大学生计算机程序设计竞赛 B - 大还是小? 字符串水题

B - 大还是小? Time Limit:5000MS     Memory Limit:65535KB     64bit IO Format: Description 输入两个实数,判断第一个数大,第二个数大还是一样大.每个数的格式为: [整数部分].[小数部分] 简单起见,整数部分和小数部分都保证非空,且整数部分不会有前导 0.不过,小数部分的最 后可以有 0,因此 0.0 和 0.000 是一样大的. Input 输入包含不超过 20 组数据.每组数据包含一行,有两个实数(格式如前所述)

一道字符串水题LSS(ACdream)

一道水题但是我错了好惨 罚时罚到最后一名 Problem Description Time flies, four years passed, colleage is over. When I am about to leave, a xuemei ask me an ACM  problem, but I can't solve it, I am 功力尽失.  Please help me so that I won't lose face in front of xuemei! Give y

HDU ACM 1073 Online Judge -&gt;字符串水题

分析:水题. #include<iostream> using namespace std; #define N 5050 char a[N],b[N],tmp[N]; void Read(char p[]) { getchar(); gets(tmp); while(gets(tmp)) { if(strcmp(tmp,"END")==0) break; if(strlen(tmp)!=0) strcat(p,tmp); strcat(p,"\n");

POJ 2039 To and Fro(水题)

[题目简述]:字符串的简单处理,看懂题意,特别是他给的那个例子就好,很简单 见代码: #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> using namespace std; char str[211][211]; int main() { int colum; char str1[211]; int tmp; while(cin>>colum,c

CF554A 字符串水题

给出一个字符串,问再加入一个字母,最多能形成多少种字符串 inputaoutput51inputhioutput76 1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm> 5 # include <cmath> 6 # define LL long long 7 using namespace std ; 8 9 int

字符串水题(hdoj1049)

Problem Description Password security is a tricky thing. Users prefer simple passwords that are easy to remember (like buddy), but such passwords are often insecure. Some sites use random computer-generated passwords (like xvtpzyo), but users have a