题目链接: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. 软工网络15团队作业4——Alpha阶段敏捷冲刺-1

    各个成员在 Alpha 阶段认领的任务 成员 Alpha 阶段认领的任务 肖世松 编写界面设计代码 杨泽斌 服务器连接与配置 叶文柠 数据库连接与配置 谢庆圆 编写功能板块代码 林伟航 编写功能板块代 ...

  2. Centos安装TFTP/NFS/PXE服务器网络引导安装系统

    客户端网卡要求支持以PXE启动,配置都在服务端进行,通过PXE网络启动安装系统流程: 客户端以PXE启动发送DHCP请求: 服务器DHCP应答,包括客户端的IP地址,引导文件所在TFTP服务器: 客户 ...

  3. canvas drawImage 不显示

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. 第61天:json遍历和封装运动框架(多个属性)

    一.json 遍历  for in  关键字  for ( 变量 in  对象)  { 执行语句;  } 例如: var json = {width:200,height:300,left:50}co ...

  5. CodeForces 632E Thief in a Shop

    题意:给你n种物品,每种无限个,问恰好取k个物品能组成哪些重量.n<=1000,k<=1000,每种物品的重量<=1000. 我们搞出选取一种物品时的生成函数,那么只要对这个生成函数 ...

  6. 【bzoj2060】[Usaco2010 Nov]Visiting Cows拜访奶牛 树形dp

    题目描述 经过了几周的辛苦工作,贝茜终于迎来了一个假期.作为奶牛群中最会社交的牛,她希望去拜访N(1<=N<=50000)个朋友.这些朋友被标号为1..N.这些奶牛有一个不同寻常的交通系统 ...

  7. [洛谷P4847]银河英雄传说V2

    题目大意:有$n(n\leqslant2\times10^5)$个序列,有$m(m\leqslant2\times10^5)$个操作,分三种: 1. $M\;x\;y:$把$x$所在的序列放在$y$所 ...

  8. [SHOI2012]回家的路 最短路

    ---题面--- 题解: 吐槽:找了好久的错,换了n种方法,重构一次代码,,,, 最后发现,,, 数组开小了,其实一开始尝试开大了数组,但唯独没有尝试开大手写队列的数组.... 思路: 有两种方法,这 ...

  9. Android <Android应用开发实战> 资源类型<二>

    1.菜单资源菜单不仅可以在onCreateContextMenu或onCreateOptionsMenu方法中通过代码创建,还可以在res/menu目录中建立相应的菜单资源文件,并在上面两个方法中加载 ...

  10. POJ3243:Clever Y——题解

    http://poj.org/problem?id=3243 求最小的非负整数y满足x^y=k(mod z) 写完板子之后等待了半个小时poj才终于进入…… poj不行啊.jpg 以前一直觉得BSGS ...