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
#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;//把str串分成m个小串
if(len%n)
m++;
for(i=0;i<m;i++)
if(i%2==0)//当能被2整除的就正向保存
{
e=0;
for(j=i*n;j<len&&j<(i+1)*n;j++)
ch[i][e++]=str[j];
}
else//否则就倒着保存
{
e=0;
for(j=(i+1)*n-1;j>=i*n;j--)
if(str[j]!='\0')
ch[i][e++]=str[j];
}
e=len%n;//注意,有的不能除尽,最后一个小串长度小于n
for(j=0;j<n;j++)
for(i=0;i<m;i++)
if(i==m-1)//最后一个小串输出
{
if(j<e||e==0)
printf("%c",ch[i][j]);
}
else
printf("%c",ch[i][j]); printf("\n");
}
}

hdu1200(来来回回串起来)的更多相关文章

  1. ASP.NET MVC5+EF6+EasyUI 后台管理系统(62)-EF链接串加密

    系列目录 前言: 这一节提供一个简单的功能,这个功能看似简单,找了一下没找到EF链接数据库串的加密帮助文档,只能自己写了,这样也更加符合自己的加密要求 有时候我们发布程序为了避免程序外的SQL链接串明 ...

  2. JQuery使用deferreds串行多个ajax请求

    使用JQuery对多个ajax请求串行执行. HTML代码: <a href="#">Click me!</a> <div></div&g ...

  3. iOS 字典或者数组和JSON串的转换

    在和服务器交互过程中,会iOS 字典或者数组和JSON串的转换,具体互换如下: // 将字典或者数组转化为JSON串 + (NSData *)toJSONData:(id)theData { NSEr ...

  4. iOS:GCD理解1(同步-异步、串行-并行)

    1.并行-异步(ST1与ST2抢占资源) 1-1).获取 并行(全局)队列 ,DISPATCH_QUEUE_PRIORITY_DEFAULT 为默认优先级. dispatch_queue_t queu ...

  5. 关于用sql语句实现一串数字位数不足在左侧补0的技巧

    在日常使用sql做查询插入操作时,我们通常会用到用sql查询一串编号,这串编号由数字组成.为了统一美观,我们记录编号时,统一指定位数,不足的位数我们在其左侧补0.如编号66,我们指定位数为5,则保存数 ...

  6. [LeetCode] Longest Palindrome 最长回文串

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

  7. [LeetCode] Shortest Palindrome 最短回文串

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  8. [LeetCode] Palindrome Partitioning II 拆分回文串之二

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  9. [LeetCode] Palindrome Partitioning 拆分回文串

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

随机推荐

  1. Archive for required library: ‘WebContent/WEB-INF/lib/xxx.jar cannot&n

    今天导入一个项目到eclipse,出现感叹号,而且报1. Archive for required library: ‘WebContent/WEB-INF/lib/xxxxx.jar cannot ...

  2. 异步和同步http请求超时机制

    异步超时设置: 例子: Example: do a simple HTTP GET request for http://www.nethype.de/ and print the response ...

  3. poj 1056 IMMEDIATE DECODABILITY(KMP)

    题目链接:http://poj.org/problem?id=1056 思路分析:检测某字符串是否为另一字符串的前缀,数据很弱,可以使用暴力解法.这里为了练习KMP算法使用了KMP算法. 代码如下: ...

  4. 九度 和为S的连续正数序列

    题目1354:和为S的连续正数序列 时间限制:2 秒 内存限制:32 兆 特殊判题:否 提交:2008 解决:622 题目描述: 小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上 ...

  5. “易信”今日正式更新至V1.1版

    热门移动通讯社交应用“易信”今日正式更新至V1.1版,目前用户已可在苹果AppStore和各大Android商店下载.新版本主要包括三大变化:开通公众平台.提供外部分享.强化社交安全,此外包含好友关系 ...

  6. ceph之Placement Group

    预定义PG_NUM 新建一个存储池命令:ceph osd pool set {pool-name}  pg_num 选择一个pg_num的值是强制性的,这是因为该值不能被自动计算出来,以下是一些常用值 ...

  7. java反射机制入门01

    java反射机制入门是我从极客学院的视频中学习的. 1.反射机制背景概述 反射(Reflection)是java被视为动态(或准动态)语言的一个关键性质.反射机制指的是程序在运行时能够获取任何类的内部 ...

  8. [Swust OJ 1125]--又见GCD(数论,素数表存贮因子)

    题目链接:http://acm.swust.edu.cn/problem/1125/ Time limit(ms): 1000 Memory limit(kb): 65535   Descriptio ...

  9. ZOJ 3778 Talented Chef 模拟 [ 祝愿明天省赛一帆风顺, ZJSU_Bloom WILL WIN : )

    这题的意思是给你 n 道菜,第 i 道菜需要 Ai 步才能完成 每次你能对 m 道菜分别完成一步,请问最少需要几次? 这题暴力写肯定是不行的,去年省赛的时候就是没写出来这题,今天再把思路理一理吧. 首 ...

  10. windows服务怎么向应用程序发消息(部署在同一台机,非SCOKET)

    命名管道:NamedPipeClientStream & NamedPipeClientStream 参考实例:http://msdn.microsoft.com/zh-cn/library/ ...