题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1200

To and Fro

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5129    Accepted Submission(s): 3550

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
 
题目大意:输入按照题目中列举的矩阵输入,假使从第0行开始,也就是偶数行按序输入,奇数行反序输入。最后输出的时候是按列输出~
 
有两种方法:第一种是DP,用dp这个数组来按照原有矩阵进行存放。最后直接输出dp即可。第二种就是直接模拟,比如第一列的就是,0,9,10,19,20,29,30;将其分成两组,第一组由0,10,20,30组成,第二组由9,19,29组成;进一步解释就是第一组的(位置数-i)%n==0;第二组的(位置数+i+1)%n==0;
 
 
详见代码。
 #include <iostream>
#include <cstdio>
#include <cstring> using namespace std; char ch[];
char dp[][]; int main ()
{
int n;
while (~scanf("%d",&n),n)
{
scanf("%s",ch);
int x=,y=;
int len=strlen(ch);
for (int i=; i<len; i++)
{
dp[x][y]=ch[i];
if (x%==)
{
y++;
if (y==n)
{
x++;
y--;
}
}
else
{
y--;
if (y==-)
{
x++;
y++;
}
} }
for (int i=; i<n; i++)
{
for (int j=; j<x; j++)
{
printf ("%c",dp[j][i]);
}
}
printf ("\n");
}
return ;
}

第二种模拟代码,详见注释

 #include<iostream>
#include<cstdio>
#include<cstring> using namespace std; int main()
{
int n;
char ch[];
while(scanf("%d",&n),n)
{
scanf("%s",ch);
int l=strlen(ch);
for(int i=;i<n;i++) //i表示第几列
{
for(int j=;j<l;j++) //j表示第几个数
{
if((j/n)%==) //判断奇偶行
{
if((j-i)%n==) //满足第一组的条件
printf("%c",ch[j]);
}
else
{
if((j+i+)%n==) //满足第二组的条件
printf("%c",ch[j]);
}
}
}
printf("\n");
}
return ;
}

hdu 1200 To and Fro(简单模拟或DP)的更多相关文章

  1. 【HDU 4452 Running Rabbits】简单模拟

    两只兔子Tom和Jerry在一个n*n的格子区域跑,分别起始于(1,1)和(n,n),有各自的速度speed(格/小时).初始方向dir(E.N.W.S)和左转周期turn(小时/次). 各自每小时往 ...

  2. hdu 4414 Finding crosses【简单模拟】

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=4414 CSUST:点击打开链接 Finding crosses Time Limit: 2000/1000 ...

  3. HDU 5268 ZYB loves Score (简单模拟,水)

    题意:计算Bestcoder四题的得分. 思路:直接模拟,4项分数直接计算后输出.注意不要低于百分之40的分. //#include <bits/stdc++.h> #include &l ...

  4. HDU 5059 Help him(简单模拟题)

    http://acm.hdu.edu.cn/showproblem.php?pid=5059 题目大意: 给定一个字符串,如果这个字符串是一个整数,并且这个整数在[a,b]的范围之内(包括a,b),那 ...

  5. HDOJ/HDU 1200 To and Fro(加密解密字符串)

    Problem Description Mo and Larry have devised a way of encrypting messages. They first decide secret ...

  6. HDU - 1200 To and Fro

    题意:给定一个,其实是由一个图按蛇形输出而成的字符串,要求按从左到右,从上到下的顺序输出这个图. 分析: 1.把字符串转化成图 2.按要求输出图= = #include<cstdio> # ...

  7. HDU 4772 Zhuge Liang&#39;s Password (简单模拟题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4772 题面: Zhuge Liang's Password Time Limit: 2000/1000 ...

  8. (hdu step 8.1.6)士兵队列训练问题(数据结构,简单模拟——第一次每2个去掉1个,第二次每3个去掉1个.知道队伍中的人数&lt;=3,输出剩下的人 )

    题目: 士兵队列训练问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...

  9. HDU 4509 湫湫系列故事——减肥记II (简单模拟)

    题意:一天一共有1440分钟,主人公每天有n件事要做,给出这n件事开始跟结束的时间,然后让你求出,空闲的时间的总分钟数是多少. 解题报告:简单模拟,只要开个一维数组标记那个每个分钟是否是有事的就可以了 ...

随机推荐

  1. delphi怎样单步调试

    在delphi的IDE编辑窗口里,主菜单->Run->Step Over或者主菜单->Run->Trace Into单步调试有两种方式:一种是Step Over,快捷键是F8, ...

  2. Visual Studio 2013中使用Ribbon For WPF

    1.首先需要 下载Ribbon For WPF.目前最新的版本是Microsoft Ribbon for WPF October 2010. 下载 链接: https://www.microsoft. ...

  3. 【Json】C#格式化JSON字符串

    很多时候我们需要将json字符串以 {     "status": 1,     "sum": 9 }这种方式显示,而从服务端取回来的时候往往是这样 {&quo ...

  4. RT-thread内核之邮箱

    一.邮箱控制块:在include/rtdef.h中 #ifdef RT_USING_MAILBOX /** * mailbox structure */ struct rt_mailbox { str ...

  5. SocketServer-实现并发处理3

    用socketserver创建一个服务的步骤: 1  创建一个request handler class(请求处理类),合理选择StreamRequestHandler和DatagramRequest ...

  6. NetScaler SNIPs Bound To An Interface Without A VLAN

    NetScaler SNIPs Bound To An Interface Without A VLAN https://www.citrix.com/blogs/2014/04/09/work-yo ...

  7. 【题解】SDOI2017树点涂色

    LCT强强!以前总是觉得LCT非常的难懂(当然现在也是的),但实际上它真的是很厉害的一种东西.它是一种动态的链剖分结构,其实就是对于剖分出来的重链使用LCT去进行维护.cut 与 link 两个操作让 ...

  8. BZOJ4868:[SHOI2017]期末考试——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=4868 题目复制于洛谷:https://www.luogu.org/problemnew/show/P ...

  9. BZOJ1257:[CQOI2007]余数之和——题解+证明

    http://www.lydsy.com/JudgeOnline/problem.php?id=1257 Description 给出正整数n和k,计算j(n, k)=k mod 1 + k mod ...

  10. HDU5115:Dire Wolf——题解+翻译

    http://acm.hdu.edu.cn/showproblem.php?pid=5115 题目大意:给n匹狼,每一次攻击可以秒杀一匹狼,但同时会受到这匹狼的a攻击和它相邻两只狼的b攻击. 给定a, ...