1007. To and Fro

2023-11-20 20:50
文章标签 1007 fro

本文主要是介绍1007. To and Fro,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

【题目要求】

1007. To and Fro

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

Mo and Larry have devised a way of encrypting messages. They first decide secretly on the number ofcolumns and write the message (letters only) down the columns, padding with extra random letters soas to make a rectangular array of letters. For example, if the message is "There's no place like home ona snowy night" and there are five columns, Mo would write downt o i o yh p k n ne l e a ir a h s ge c o n hs e m o tn l e w xNote that Mo includes only letters and writes them all in lower case. In this example, Mo used thecharacter `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 andright-to-left. So, the above would be encrypted astoioynnkpheleaigshareconhtomesnlewxYour job is to recover for Larry the original message (along with any extra padding letters) from theencrypted one.

Input

There will be multiple input sets. Input for each set will consist of two lines. The first line will containan integer in the range 2 . ..20 indicating the number of columns used. The next line is a string of upto 200 lower case letters. The last input set is followed by a line containing a single 0, indicating end ofinput.

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


【解题思路】

            本题为密文解密,使用二维矩阵

【遇到的问题】

          1、读题出现问题,然后google原题,看了题目中给的示例矩阵,才明白是明文竖着排,之前因为觉得竖着排需要数,就以为是明文横着排

          2、编程时应该先默认输入一个样例,这样就不用一遍遍输入同样的样例了。

          3、一开始对一个样例进行编程,不免会有默认的错误,就需要时刻注意和多个样例的测试。

          4、对于如何输入多个样例,while居然没调出来,不知道为什么,所以就用了for,看着挺蛋疼。

         

#include<iostream>
#include <string>
using namespace std;int main ()
{int column =1 ;string ciphertext ;cin>>column;for(;column!=0;)
{cin>>ciphertext;int row = ciphertext.size()/column;string rectangle[row][column];int cinCol = -1;for(int i=0;i<ciphertext.size();i++){if(cinCol == 2*column-1)cinCol = -1;cinCol++;if(cinCol<column)rectangle[i/column][cinCol] = ciphertext[i];else rectangle[i/column][2*column-1-cinCol] = ciphertext[i];//cout<<"rectangle[][]"<<i/column<<"  "<<cinCol<<" "<<ciphertext[i]<<endl;}for (int i=0;i<ciphertext.size();i++){cout<<rectangle[i%row][i/row];}cout<<endl;cin>>column;
}system ("pause");return 0;
}



这篇关于1007. To and Fro的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/397349

相关文章

奋战杭电ACM(DAY5)1007

1006题昨天想了整整一天一夜也没有结果……所以跳过了……过会去问一下老师,网上大神的答案都看不懂啊啊啊啊!! 今天搞定了1007,暴力果然是没有好结果的,超时了…… 正好前天刚看了递归与分治法,用上了,AC~ 不过具体怎么计算算法复杂度还没搞懂,回去再琢磨琢磨!! Quoit Design #include <iostream>#include <iomanip>#inclu

【PAT】【Advanced Level】1007. Maximum Subsequence Sum (25)

1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a sequence of K integers { N1, N2, ..., NK }. A continuous

HDU-1007 Quoit Design 最小距离点对

求平面内任意两点间距离 1.蛮力法( 时间复杂度O(n^2) 适用于点的数目比较小的情况下) 算法描述:已知集合S中有n个点,一共可以组成n(n-1)/2对点对,蛮力法就是对这n(n-1)/2对点对逐对进行距离计算,通过循环求得点集中的最近点对: 2、分治法( 时间复杂度O(nlogn) )      1)算法描述:已知集合S中有n个点,分治法的思想就是将S进行拆分,分为2部分

POJ 1007 DNA 排序

题意:分类DNA字符串(只有ACGT四个字符)。但是分类它们的方法不是字典序,而是逆序数,排序程度从好到差。所有字符串长度相同。 解题思路:第一感觉就是用结构体数组,结构体中存字符数组和这个字符数组的逆序数。然后用两个for循环求逆序数即可。刚开始编完提交WA,仔细看题目才记得是稳定排序,所以把sort改为stable_sort即可实现稳定排序。编写一个判断的函数使逆序数从小到大排序,最后从头到

Hdu 5038 Grade(2014 ACM/ICPC Asia Regional Beijing Online 1007)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5038 题目的意思, 给你公式,求出每个蘑菇的grade,求这些grade的众数。。Mode,众数,表示英语又被鄙视了。 表示,自己很弱,不知道众数的定义。。。 百度之。。。 一般来说,一组数据中,出现次数最多的数就叫这组数据的众数。 例如:1,2,3,3,4的众数是3。

1007 温度转换

#include<iostream>#include<iomanip>using namespace std;int main(){float c,f;cin>>f;c=5*(f-32)/9;cout<<setiosflags(ios::fixed)<<setprecision(2)<<c<<endl;return 0;}

(C)1007 素数对猜想

1007 素数对猜想 问题描述 输入样例: 20 输出样例: 4 解决方案: #include<stdio.h>#include<string.h>#include<math.h>int main(){int n,d;int a[100000];int flag,jishu=0;scanf("%d",&n);memset(a,-1,sizeof(a));

hdu contest day1 1007 Tricks Device

传送门:http://acm.hdu.edu.cn/contests/contest_showproblem.php?cid=589&pid=1007 思路:预处理出最短路,跑最小割 lll的代码: #include<bits/stdc++.h>#define maxn 2100#define maxm 180000#define inf 0x7fffffff#define mi

51nod 1007 正整数分组(背包/dp)

1007 正整数分组 基准时间限制:1 秒 空间限制:131072 KB 分值: 10  难度:2级算法题  收藏  关注 将一堆正整数分为2组,要求2组的和相差最小。 例如:1 2 3 4 5,将1 2 4分为1组,3 5分为1组,两组和相差1,是所有方案中相差最少的。 Input 第1行:一个数N,N为正整数的数量。第2 -

关于hdu 1007 老是 TLE的认知

经过多次修改发现,同样的代码,把cin换成scanf就AC了,然后查找相关资料发现,,scanf效率比cin好的多,cout 比print 效率好。mark