hdu 1200 To and Fro(简单模拟或DP)

题目链接: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;

详见代码。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4
 5 using namespace std;
 6
 7 char ch[210];
 8 char dp[110][20];
 9
10 int main ()
11 {
12     int n;
13     while (~scanf("%d",&n),n)
14     {
15         scanf("%s",ch);
16         int x=0,y=0;
17         int len=strlen(ch);
18         for (int i=0; i<len; i++)
19         {
20             dp[x][y]=ch[i];
21             if (x%2==0)
22             {
23                 y++;
24                 if (y==n)
25                 {
26                     x++;
27                     y--;
28                 }
29             }
30             else
31             {
32                 y--;
33                 if (y==-1)
34                 {
35                     x++;
36                     y++;
37                 }
38             }
39
40         }
41         for (int i=0; i<n; i++)
42         {
43             for (int j=0; j<x; j++)
44             {
45                 printf ("%c",dp[j][i]);
46             }
47         }
48         printf ("\n");
49     }
50     return 0;
51 }

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

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4
 5 using namespace std;
 6
 7 int main()
 8 {
 9     int n;
10     char ch[250];
11     while(scanf("%d",&n),n)
12     {
13         scanf("%s",ch);
14         int l=strlen(ch);
15         for(int i=0;i<n;i++)            //i表示第几列
16         {
17             for(int j=0;j<l;j++)        //j表示第几个数
18             {
19                 if((j/n)%2==0)          //判断奇偶行
20                 {
21                     if((j-i)%n==0)      //满足第一组的条件
22                         printf("%c",ch[j]);
23                 }
24                 else
25                 {
26                     if((j+i+1)%n==0)     //满足第二组的条件
27                         printf("%c",ch[j]);
28                 }
29             }
30         }
31         printf("\n");
32     }
33     return 0;
34 }
时间: 2024-10-11 01:06:43

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

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

题意:计算Bestcoder四题的得分. 思路:直接模拟,4项分数直接计算后输出.注意不要低于百分之40的分. 1 //#include <bits/stdc++.h> 2 #include <iostream> 3 #include <cstdio> 4 #define LL long long 5 using namespace std; 6 const int N=10; 7 int a[N]; 8 int b[N]; 9 10 int cal() 11 { 12

HDU 5059 Help him(简单模拟题)

http://acm.hdu.edu.cn/showproblem.php?pid=5059 题目大意: 给定一个字符串,如果这个字符串是一个整数,并且这个整数在[a,b]的范围之内(包括a,b),那就输出YES,其它的都是NO. 这个字符串是整数的条件: 1.如果它是正整数,它只包含前导不是0的数(这个数前面没有零). 2.如果它是负整数,只包含一个'-'符号,任然没有前导0. 3.除此之外都不是非法的 解题思路: http://bestcoder.hdu.edu.cn/ 这里有 要注意: 0

HDU 1200 To and Fro

To and Fro Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5493    Accepted Submission(s): 3799 Problem Description Mo and Larry have devised a way of encrypting messages. They first decide sec

HDU ACM 1035 Robot Motion 简单模拟题

分析:一步步的走,走出矩阵则说明没有环,若走到已经走过的地方,说明有环,按格式输出结果,OK. #include<iostream> using namespace std; #define N 15 int dir[4][2]={-1,0,1,0,0,-1,0,1}; char map[N][N]; int vis[N][N]; char ch[]="NSWE"; int n,m; int id(char c) { int i; for(i=0;i<4;i++) i

HDU 5119 Happy Matt Friends(简单二维dp)

题意不再说了,就是一个二维的dp,维持取最大值是多少. Happy Matt Friends Time Limit: 6000/6000 MS (Java/Others)    Memory Limit: 510000/510000 K (Java/Others) Total Submission(s): 608    Accepted Submission(s): 229 Problem Description Matt has N friends. They are playing a ga

HDU 1048 What Is Your Grade? (简单模拟)

 What Is Your Grade? Problem Description "Point, point, life of student!" This is a ballad(歌谣)well known in colleges, and you must care about your score in this exam too. How many points can you get? Now, I told you the rules which are used in

HDU 4891 简单模拟

The Great Pan Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1035    Accepted Submission(s): 355 Problem Description As a programming contest addict, Waybl is always happy to take part in vario

HDU 3623 Best Cow Line, Gold(模拟,注意思路,简单)

题目 POJ 3617 和 这道题题目一样,只是范围稍稍再小一点. //模拟试试 #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; char s[30010][2]; bool bijiao(int st,int ed) { if(st==ed) return true; if(s[st][0]<s[ed][0]) return true; else if(s

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4772 题面: Zhuge Liang's Password Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1404    Accepted Submission(s): 926 Problem Description In the anc