字符串处理问题系列之 To and Fro hdoj 1200
2014-08-18 19:27
357 查看
/*
To and Fro
http://acm.hdu.edu.cn/showproblem.php?pid=1200
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4664 Accepted Submission(s): 3255
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
Source
East Central North America 2004
*/
方法一(本人)AC:
#include<cstdio>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#define M 210
char a[M][M];
int main()
{
int n;
char s[210];
while(scanf("%d",&n),n)
{
getchar();
gets(s);
int i,j,l,k,len,t=0;
l=strlen(s);
k=(double)l/n;//此段至关重要,否则当就会出现你难以发现的WA
if(k>l/n)
len=(int)k+1;
else
len=(int)k;
for(i=1;i<=len;i++)
{
if(i%2==0)
for(j=n;j>0;j--)
a[i][j]=s[t++];
else
for(j=1;j<=n;j++)
a[i][j]=s[t++];
}
/*
printf("数组:\n");
for(i=1;i<=len;i++)
{
for(j=1;j<=n;j++)
printf("%c",a[i][j]);
printf("\n");
}
printf("\n");
*/
for(j=1;j<=n;j++)
for(i=1;i<=len;i++)
printf("%c",a[i][j]);
printf("\n");
}
return 0;
}
方法二(人家)AC:
/*
#include<cstdio>
#include<string.h>
#include<stdlib.h>
int main()
{
int n;
char a[210];
while(scanf("%d",&n),n)
{
getchar();
gets(a);
int i,j,len;
len=strlen(a);
for(j=0;j<n;j++)
{
for(i=0;i<len/n;i++)
if(i&1)
printf("%c",a[(i+1)*n-(j+1)]);
else
printf("%c",a[i*n+j]);
}
printf("\n");
}
return 0;
}
*/
To and Fro
http://acm.hdu.edu.cn/showproblem.php?pid=1200
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4664 Accepted Submission(s): 3255
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
Source
East Central North America 2004
*/
方法一(本人)AC:
#include<cstdio>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#define M 210
char a[M][M];
int main()
{
int n;
char s[210];
while(scanf("%d",&n),n)
{
getchar();
gets(s);
int i,j,l,k,len,t=0;
l=strlen(s);
k=(double)l/n;//此段至关重要,否则当就会出现你难以发现的WA
if(k>l/n)
len=(int)k+1;
else
len=(int)k;
for(i=1;i<=len;i++)
{
if(i%2==0)
for(j=n;j>0;j--)
a[i][j]=s[t++];
else
for(j=1;j<=n;j++)
a[i][j]=s[t++];
}
/*
printf("数组:\n");
for(i=1;i<=len;i++)
{
for(j=1;j<=n;j++)
printf("%c",a[i][j]);
printf("\n");
}
printf("\n");
*/
for(j=1;j<=n;j++)
for(i=1;i<=len;i++)
printf("%c",a[i][j]);
printf("\n");
}
return 0;
}
方法二(人家)AC:
/*
#include<cstdio>
#include<string.h>
#include<stdlib.h>
int main()
{
int n;
char a[210];
while(scanf("%d",&n),n)
{
getchar();
gets(a);
int i,j,len;
len=strlen(a);
for(j=0;j<n;j++)
{
for(i=0;i<len/n;i++)
if(i&1)
printf("%c",a[(i+1)*n-(j+1)]);
else
printf("%c",a[i*n+j]);
}
printf("\n");
}
return 0;
}
*/
相关文章推荐
- hdoj 2203 亲和串(字符串处理问题(strstr函数的巧用))
- hdoj 1004 Let the balloon rise (字符串处理问题)
- 急急急!!!有关asp字符串处理的问题,请大虾指点???
- 字符串处理:中英文混排固定长度截取问题
- 字段里字符串的处理问题
- 有关字符串处理问题的原则
- 切记在JAVA中处理字符串的时候的问题 == equals
- 字段里字符串的处理问题
- 字段里字符串的处理问题
- 字段里字符串的处理问题
- 数学之美 系列十八 - 矩阵运算和文本处理中的分类问题
- 字段里字符串的处理问题
- 字符串及处理之三: 使用TCHAR系列方案
- CString的几个字符串处理函数的index问题
- 学之美 系列十八 - 矩阵运算和文本处理中的分类问题
- 字段里字符串的处理问题
- 字符串处理路径问题
- [转载]数学之美 系列十八 - 矩阵运算和文本处理中的分类问题
- 字段里字符串的处理问题
- 字段里字符串的处理问题