杰拉斯的博客

[整理]ACM详解(8)——加密

杰拉斯 杰拉斯 | 时间:2012-02-17, Fri | 6,762 views
编程算法 
比赛的时候告诉你简单的加密算法,让你完成加密或者解密操作,下面通过几个简单例子介绍。
1Message Decowding
Description
The cows are thrilled because they've just learned about encrypting messages. They think they will be able to use secret messages to plot meetings with cows on other farms.
Cows are not known for their intelligence. Their encryption method is nothing like DES or BlowFish or any of those really good secret coding methods. No, they are using a simple substitution cipher.
The cows have a decryption key and a secret message. Help them decode it. The key looks like this:
yrwhsoujgcxqbativndfezmlpk
Which means that an 'a' in the secret message really means 'y'; a 'b' in the secret message really means 'r'; a 'c' decrypts to 'w'; and so on. Blanks are not encrypted; they are simply kept in place.
Input text is in upper or lower case, both decrypt using the same decryption key, keeping the appropriate case, of course.
Input
* Line 1: 26 lower case characters representing the decryption key
* Line 2: As many as 80 characters that are the message to be decoded
Output
* Line 1: A single line that is the decoded message. It should have the same length as the second line of input.

(阅读全文…)

[整理]ACM详解(7)——压缩与编码

杰拉斯 杰拉斯 | 时间:2012-02-17, Fri | 6,720 views
编程算法 

有些题目会给出一些简单的压缩方法或者编码方法,让你实现具体的算法。下面通过题目分析。

1Parencodings
Description
Let S = s1 s2...s2n be a well-formed string of parentheses. S can be encoded in two different ways:
By an integer sequence P = p1 p2...pn where pi is the number of left parentheses before the ith right parenthesis in S (P-sequence).
By an integer sequence W = w1 w2...wn where for each right parenthesis, say a in S, we associate an integer which is the number of right parentheses counting from the matched left parenthesis of a up to a. (W-sequence).
Following is an example of the above encodings:
S                 (((()()())))
P-sequence          4 5 6666
W-sequence         1 1 1456
Write a program to convert P-sequence of a well-formed string to the W-sequence of the same string.
Input
The first line of the input contains a single integer t (1 <= t <= 10), the number of test cases, followed by the input data for each test case. The first line of each test case is an integer n (1 <= n <= 20), and the second line is the P-sequence of a well-formed string. It contains n positive integers, separated with blanks, representing the P-sequence.
Output
The output file consists of exactly t lines corresponding to test cases. For each test case, the output line should contain n integers describing the W-sequence of the string corresponding to its given P-sequence.

(阅读全文…)

[整理]ACM详解(6)——栈

杰拉斯 杰拉斯 | 时间:2012-02-17, Fri | 6,934 views
编程算法 
堆栈是一种特殊的线性结构,后进先出,只能对栈顶元素操作,典型的操作入栈和出站。下面通过例子介绍基本用法。
题目:

Train Problem

Problem Description
As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of student want to get back to school by train(because the trains in the Ignatius Train Station is the fastest all over the world ^v^). But here comes a problem, there is only one railway where all the trains stop. So all the trains come in from one side and get out from the other side. For this problem, if train A gets into the railway first, and then train B gets into the railway before train A leaves, train A can't leave until train B leaves. The pictures below figure out the problem. Now the problem for you is, there are at most 9 trains in the station, all the trains has an ID(numbered from 1 to n), the trains get into the railway in an order O1, your task is to determine whether the trains can get out in an order O2.
Input
The input contains several test cases. Each test case consists of an integer, the number of trains, and two strings, the order of the trains come in:O1, and the order of the trains leave:O2. The input is terminated by the end of file. More details in the Sample Input.
Output
The output contains a string "No." if you can't exchange O2 to O1, or you should output a line contains "Yes.", and then output your way in exchanging the order(you should output "in" for a train getting into the railway, and "out" for a train getting out of the railway). Print a line contains "FINISH" after each test case. More details in the Sample Output.

(阅读全文…)

[整理]ACM详解(5)——排序

杰拉斯 杰拉斯 | 时间:2012-02-17, Fri | 5,314 views
编程算法 
有些ACM题需要使用一些基本的数据结构,下面首先介绍与排序相关的内容。
1、基本排序
Problem Description
These days, I am thinking about a question, how can I get a problem as easy as A+B? It is fairly difficulty to do such a thing. Of course, I got it after many waking nights.
Give you some integers, your task is to sort these number ascending (升序).
You should know how easy the problem is now!
Good luck!
Input
Input contains multiple test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow. Each test case contains an integer N (1<=N<=1000 the number of integers to be sorted) and then N integers follow in the same line.
It is guarantied that all integers are in the range of 32-int.
Output
For each case, print the sorting result, and one line one case.

(阅读全文…)

[整理]ACM详解(4)——递归

杰拉斯 杰拉斯 | 时间:2012-02-17, Fri | 5,091 views
编程算法 

递归在解决一些问题的时候非常直观,但是在是使用递归的时候要注意递归的深度,如果深度太深,可能会造成堆栈溢出。下面通过实例介绍如何使用。

题目:超级楼梯
Problem Description
有一楼梯共M级,刚开始时你在第一级,若每次只能跨上一级或二级,要走上第M级,共有多少种走法?
Input
输入数据首先包含一个整数N,表示测试实例的个数,然后是N行数据,每行包含一个整数M(1<=M<=40),表示楼梯的级数。
Output
对于每个测试实例,请输出不同走法的数量

(阅读全文…)