题目链接: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. mysql指定编码格式创建数据库

    CREATE DATABASE `dev` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

  2. C# Parsing 类实现的 PDF 文件分析器

    下载示例 下载源代码 1. 介绍 这个项目让你可以去读取并解析一个PDF文件,并将其内部结构展示出来. PDF文件的格式标准文档可以从Adobe那儿获取到. 这个项目基于“PDF指南,第六版,Adob ...

  3. Matlab 之 im2col 【转】

    函数原型: B=im2col(A,[m n],block_type) 功能:将矩阵A分为m×n的子矩阵,再将每个子矩阵作为B的一列 (1)当block_type为distinct时,将A分解为互不重叠 ...

  4. RT-thread 设备驱动组件之IIC总线设备

    本文主要介绍RT-thread中IIC总线设备驱动,涉及到的主要文件有:驱动框架文件(i2c_core.c,i2c_dev.c,i2c-bit-ops.c,i2c_dev.h,i2c.h):底层硬件驱 ...

  5. 求熵 python 代码

    #coding=gbk import nltk import math def entropy(labels): freqdist = nltk.FreqDist(labels) #Frequency ...

  6. 【转】C# typeof()实例详解

    转自:http://www.cnblogs.com/lm3515/archive/2010/09/02/1815725.html typeof(C# 参考) 用于获取类型的 System.Type 对 ...

  7. [洛谷P5075][JSOI2012]分零食

    题目大意:有$m(m\leqslant10^8)$个人站成一排,有$n(n\leqslant10^4)$个糖果,若第$i$个人没有糖果,那么第$i+1$个人也没有糖果.一个人有$x$个糖果会获得快乐值 ...

  8. [NOIP2016 TG D2T3]愤怒的小鸟

    题目大意:有一架弹弓位于(0,0)处,每次可以用它向第一象限发射一只小鸟,飞行轨迹均为形如y=ax2+bxy=ax+bx2 y=ax2+bx的曲线,且必须满足a<0(即是下开口的) 平面的第一象 ...

  9. BZOJ3709 [PA2014]Bohater 【贪心】

    题目链接 BZOJ3709 题解 贪心很显然 我们先干掉能回血的怪,当然按照\(d\)升序顺序,因为打得越多血越多,\(d\)大的尽量往后打 然后再干掉会扣血的怪,当然按照\(a\)降序顺序,因为最后 ...

  10. (ex)BSGS题表

    学了一下BSGS大概知道他是什么了,但是并没有做什么难题,所以也就会个板子.普通的BSGS,我还是比较理解的,然而exBSGS我却只理解个大概,也许还会个板子......(这个东西好像都会有一群恶心的 ...