HDOJ/HDU 1200 To and Fro(加密解密字符串)

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

水题。
译码还原问题

题目保证输入的数据一定是一个矩形的。

import java.util.Scanner;

public class Main{

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int n=sc.nextInt();
            if(n==0){
                return;
            }
            String str=sc.next();
            String s[] = new String[str.length()/n];
            for(int i=0;i<str.length()/n;i++){
                s[i]="";//不要忘记初始化
                if(i%2==0){
                    for(int k=i*n;k<n+n*i;k++){
                        s[i]=s[i]+str.charAt(k);
                    }
                }else{
                    for(int k=i*n;k<n+n*i;k++){
                        s[i]=str.charAt(k)+s[i];
                    }
                }
                //System.out.println(s[i]);
            }

            for(int i=0;i<n;i++){
                for(int k=0;k<s.length;k++){
                    System.out.print(s[k].charAt(i));
                }
            }
            System.out.println();

        }

    }

}
时间: 2024-07-31 13:52:46

HDOJ/HDU 1200 To and Fro(加密解密字符串)的相关文章

详解 PHP加密解密字符串函数附源码下载_php实例

项目中有时我们需要使用PHP将特定的信息进行加密,也就是通过加密算法生成一个加密字符串,这个加密后的字符串可以通过解密算法进行解密,便于程序对解密后的信息进行处理. 下面先给大家展示下效果图,感兴趣的朋友继续阅读全文. 效果演示     源码下载 笔者收录了一些比较经典的PHP加密解密函数代码,分享给大家.加密解密原理一般都是通过一定的加密解密算法,将密钥加入到算法中,最终得到加密解密结果. 1.非常给力的authcode加密函数,Discuz!经典代码(带详解): function authc

C#加密解密字符串方法

C#加密解密字符串方法 首先在web.config | app.config 文件下增加如下代码: <?xml version="1.0"?>   <configuration>     <appSettings>       <add key="IV" value="SuFjcEmp/TE="/>       <add key="Key" value="KIPS

HDOJ 1048 The Hardest Problem Ever(加密解密类)

Problem Description Julius Caesar lived in a time of danger and intrigue. The hardest situation Caesar ever faced was keeping himself alive. In order for him to survive, he decided to create one of the first ciphers. This cipher was so incredibly sou

PHP加密解密字符串汇总

  PHP加密解密函数分享,一个是Discuz!的authcode加密函数(带详细分解),一个是encrypt()函数.需要使用PHP将特定的信息进行加密,也就是通过加密算法生成一个加密字符串,这个加密后的字符串可以通过解密算法进行解密,便于程序对解密后的信息进行处理. 项目中有时我们需要使用PHP将特定的信息进行加密,也就是通过加密算法生成一个加密字符串,这个加密后的字符串可以通过解密算法进行解密,便于程序对解密后的信息进行处理. 最常见的应用在用户登录以及一些API数据交换的场景. 笔者收录

PHP加密解密字符串汇总_php技巧

项目中有时我们需要使用PHP将特定的信息进行加密,也就是通过加密算法生成一个加密字符串,这个加密后的字符串可以通过解密算法进行解密,便于程序对解密后的信息进行处理. 最常见的应用在用户登录以及一些API数据交换的场景. 笔者收录了一些比较经典的PHP加密解密函数代码,分享给大家.加密解密原理一般都是通过一定的加密解密算法,将密钥加入到算法中,最终得到加密解密结果. 1.非常给力的authcode加密函数,Discuz!经典代码(带详解):  function authcode($string,

php加密解密字符串示例_php实例

收录了一些比较经典的PHP加密解密函数代码,分享给大家.加密解密原理一般都是通过一定的加密解密算法,将密钥加入到算法中,最终得到加密解密结果.     function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) { // 动态密匙长度,相同的明文会生成不同密文就是依靠动态密匙 $ckey_length = 4; // 密匙 $key = md5($key ? $key : $GLOBALS['discuz_a

php加密解密字符串示例

收录了一些比较经典的PHP加密解密函数代码,分享给大家.加密解密原理一般都是通过一定的加密解密算法,将密钥加入到算法中,最终得到加密解密结果. function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) { // 动态密匙长度,相同的明文会生成不同密文就是依靠动态密匙 $ckey_length = 4; // 密匙 $key = md5($key ? $key : $GLOBALS['discuz_auth_

使用Base64加密/解密字符串

Imports System.Text.UnicodeEncoding Dim str As String = "我要被加密了!" '将字符串转换为 ascii 码数组 Dim strb1() As Byte = Unicode.GetBytes(str) '加密转换后的字符串 Dim newstr As String = System.Convert.ToBase64String(strb1) Label1.Text = "加密后是:" + newstr '将加密

js加密解密字符串可自定义密码因子_javascript技巧

复制代码 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv=&qu