• 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 ("
    ");
    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("
    ");
    32     }
    33     return 0;
    34 }
  • 相关阅读:
    目录:数据挖掘
    目录:工具的安装与激活
    目录:Django框架开发Web系统的功能实现过程
    Python3.5在jupyter中使用graphviz画决策树(包括graphviz-2.38.msi的安装)
    2020年全国高校计算机能力挑战赛Python程序设计初赛
    离群异常值与重复数据检测
    数据描述性统计度量方法
    数据挖掘的方法
    Nginx+uWSGI+Django部署方案的详细实现过程
    VMware的联网配置及虚拟机IP配置
  • 原文地址:https://www.cnblogs.com/qq-star/p/4340527.html
Copyright © 2020-2023  润新知