首页 > ACM题库 > HDU-杭电 > HDU 1200 To and Fro-字符串-[解题报告] C++
2013
12-04

HDU 1200 To and Fro-字符串-[解题报告] C++

To and Fro

问题描述 :

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.

输入:

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.

输出:

Each input set should generate one line of output, giving the original plaintext message, with no spaces.

样例输入:

5
toioynnkpheleaigshareconhtomesnlewx
3
ttyohhieneesiaabss
0

样例输出:

theresnoplacelikehomeonasnowynightx
thisistheeasyoneab


#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cctype>
using namespace std;
int main() {
    int n;
    while(cin>>n&&n) {
        string s; cin>>s;
        string f[1000];
        int a=0,j;
        for(j=0; a+n<=s.size(); j++,a+=n) {
            f[j].assign(s,a,n);
            if(j%2)reverse(f[j].begin(),f[j].end());
        }
        if(a<s.size()-1){
            int l=s.size()-a;
            f[j].assign(s,a,l);
            ++j;
        }
        for(int i=0,k;i<n;++i)
            for(k=0;k<j;k++)
                cout<<f[k][i];
        cout<<endl;
    }
    return 0;
}




  1. 题本身没错,但是HDOJ放题目的时候,前面有个题目解释了什么是XXX定律。
    这里直接放了这个题目,肯定没几个人明白是干啥

  2. 你的理解应该是:即使主持人拿走一个箱子对结果没有影响。这样想,主持人拿走的箱子只是没有影响到你初始选择的那个箱子中有奖品的概率,但是改变了其余两个箱子的概率分布。由 1/3,1/3 变成了 0, 2/3

  3. 博主您好,这是一个内容十分优秀的博客,而且界面也非常漂亮。但是为什么博客的响应速度这么慢,虽然博客的主机在国外,但是我开启VPN还是经常响应很久,再者打开某些页面经常会出现数据库连接出错的提示