杰拉斯的博客

[ACM_ZOJ_1003]Crashing Balloon

杰拉斯 杰拉斯 | 时间:2012-05-22, Tue | 23,029 views
编程算法 

Crashing Balloon

Time Limit: 2 Seconds Memory Limit: 65536 KB

Description

On every June 1st, the Children's Day, there will be a game named "crashing balloon" on TV. The rule is very simple. On the ground there are 100 labeled balloons, with the numbers 1 to 100. After the referee shouts "Let's go!" the two players, who each starts with a score of "1", race to crash the balloons by their feet and, at the same time, multiply their scores by the numbers written on the balloons they crash. After a minute, the little audiences are allowed to take the remaining balloons away, and each contestant reports his\her score, the product of the numbers on the balloons he\she's crashed. The unofficial winner is the player who announced the highest score.

Inevitably, though, disputes arise, and so the official winner is not determined until the disputes are resolved. The player who claims the lower score is entitled to challenge his\her opponent's score. The player with the lower score is presumed to have told the truth, because if he\she were to lie about his\her score, he\she would surely come up with a bigger better lie. The challenge is upheld if the player with the higher score has a score that cannot be achieved with balloons not crashed by the challenging player. So, if the challenge is successful, the player claiming the lower score wins.

So, for example, if one player claims 343 points and the other claims 49, then clearly the first player is lying; the only way to score 343 is by crashing balloons labeled 7 and 49, and the only way to score 49 is by crashing a balloon labeled 49. Since each of two scores requires crashing the balloon labeled 49, the one claiming 343 points is presumed to be lying.

On the other hand, if one player claims 162 points and the other claims 81, it is possible for both to be telling the truth (e.g. one crashes balloons 2, 3 and 27, while the other crashes balloon 81), so the challenge would not be upheld.

By the way, if the challenger made a mistake on calculating his/her score, then the challenge would not be upheld. For example, if one player claims 10001 points and the other claims 10003, then clearly none of them are telling the truth. In this case, the challenge would not be upheld.

Unfortunately, anyone who is willing to referee a game of crashing balloon is likely to get over-excited in the hot atmosphere that he\she could not reasonably be expected to perform the intricate calculations that refereeing requires. Hence the need for you, sober programmer, to provide a software solution.

Input

Pairs of unequal, positive numbers, with each pair on a single line, that are claimed scores from a game of crashing balloon.

Output

Numbers, one to a line, that are the winning scores, assuming that the player with the lower score always challenges the outcome.

(阅读全文…)

[ACM实验五]ACM程序设计基础(3)

杰拉斯 杰拉斯 | 时间:2012-05-09, Wed | 8,853 views
编程算法 

实验项目:ACM程序设计基础(3)
实验目的:掌握C++程序设计基础。
实验要求:使用VC++6.0实现实验要求。
实验内容:

1.为了对信件保密,需要对信件进行加密,加密方法是每个字母加5,如A写成F,B写成G。输入一行加密的英文句子,输出其解密英文句子,例如:
输入:NS BFW, JAJSYX TK NRUTWYFSHJ FWJ YMJ WJXZQY TK YWNANFQ HFZXJX
输出:IN WAR, EVENTS OF IMPORTANCE ARE THE RESULT OF TRIVIAL CAUSES
(提示:getline 是一个函数,它可以接受用户的输入的字符,直到已达指定个数,或者用户输入了特定的字符。它的函数声明形式(函数原型)如下:
istream& getline(char line[], int size, char endchar = '\n');
char line[]: 就是一个字符数组,用户输入的内容将存入在该数组内。
int size : 最多接受几个字符?用户超过size的输入都将不被接受。
char endchar :当用户输入endchar指定的字符时,自动结束。默认是回车符。
例如用string buf;来保存:getline( cin , buf ); 如果用char buf[ 255 ]; 来保存:cin.getline( buf, 255 )。)

2. 某售货员要到若干城市去推销商品,一直各城市之间的路程,他要选定一条从驻地出发,经过每个城市一遍,最后回到住地的路线,使总的路程最短。

最短路径

3. Anagrams by Stack问题,见Anagrams by Stack

(阅读全文…)

[ACM_HDU_1515]Anagrams by Stack

杰拉斯 杰拉斯 | 时间:2012-05-09, Wed | 9,776 views
编程算法 

Anagrams by Stack

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 614 Accepted Submission(s): 311

Description

How can anagrams result from sequences of stack operations? There are two sequences of stack operators which can convert TROT to TORT:

[
i i i i o o o o
i o i i o o i o
]

where i stands for Push and o stands for Pop. Your program should, given pairs of words produce sequences of stack operations which convert the first word to the second.

A stack is a data storage and retrieval structure permitting two operations:

Push - to insert an item and
Pop - to retrieve the most recently pushed item
We will use the symbol i (in) for push and o (out) for pop operations for an initially empty stack of characters. Given an input word, some sequences of push and pop operations are valid in that every character of the word is both pushed and popped, and furthermore, no attempt is ever made to pop the empty stack. For example, if the word FOO is input, then the sequence:

i i o i o o is valid, but
i i o is not (it's too short), neither is
i i o o o i (there's an illegal pop of an empty stack)

Valid sequences yield rearrangements of the letters in an input word. For example, the input word FOO and the sequence i i o i o o produce the anagram OOF. So also would the sequence i i i o o o. You are to write a program to input pairs of words and output all the valid sequences of i and o which will produce the second member of each pair from the first.

Input

The input will consist of several lines of input. The first line of each pair of input lines is to be considered as a source word (which does not include the end-of-line character). The second line (again, not including the end-of-line character) of each pair is a target word. The end of input is marked by end of file.

Output

For each input pair, your program should produce a sorted list of valid sequences of i and o which produce the target word from the source word. Each list should be delimited by

[
]

and the sequences should be printed in "dictionary order". Within each sequence, each i and o is followed by a single space and each sequence is terminated by a new line.

(阅读全文…)

[朝花夕拾]一个菜鸟从高一到大二的作品整理

杰拉斯 杰拉斯 | 时间:2012-05-06, Sun | 62,773 views
心路历程, 编程算法 

写在前面

虽然不是什么文艺青年,但对着过去的事物总会有一种说不清道不明的感觉,听着《三个人的时光》这首抒情的音乐,决定也跟着文艺一把。

曾经,我也是一个什么都不懂的菜鸟,第一次与网络亲密接触的时候,我就在想:“我们一直以来上的都是别人网站,我能不能自己制作一个呢?”于是稀里糊涂的就开始学起了网页……

这么多年走过,也算是小有所成,捣鼓过不少作品,只是想即便放出来也只是贻笑于大方之家罢了,于是这些在我手里诞生的作品们便在硬盘里尘封了多年,直到最近,突然觉得让他们只能在自己的硬盘里待着也实在没什么意义,要是有一天硬盘坏了那就再也找不出来了,于是收拾心情把这些旧事物整理出来,或许有一天我真正成熟,乃至老去,我可以指着这些东西对别人说,你看,这些幼稚的作品,却是我一步一步走过来的痕迹。。

朝花夕拾,为时未晚。。

(阅读全文…)

[ACM_HDU_1052]Tian Ji -- The Horse Racing

杰拉斯 杰拉斯 | 时间:2012-05-03, Thu | 18,310 views
编程算法 

Tian Ji -- The Horse Racing

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9786 Accepted Submission(s): 2718

Description

Here is a famous story in Chinese history.

"That was about 2300 years ago. General Tian Ji was a high official in the country Qi. He likes to play horse racing with the king and others."

"Both of Tian and the king have three horses in different classes, namely, regular, plus, and super. The rule is to have three rounds in a match; each of the horses must be used in one round. The winner of a single round takes two hundred silver dollars from the loser."

"Being the most powerful man in the country, the king has so nice horses that in each class his horse is better than Tian's. As a result, each time the king takes six hundred silver dollars from Tian."

"Tian Ji was not happy about that, until he met Sun Bin, one of the most famous generals in Chinese history. Using a little trick due to Sun, Tian Ji brought home two hundred silver dollars and such a grace in the next match."

"It was a rather simple trick. Using his regular class horse race against the super class from the king, they will certainly lose that round. But then his plus beat the king's regular, and his super beat the king's plus. What a simple trick. And how do you think of Tian Ji, the high ranked official in China?"

[ACM_HDU_1052]Tian Ji -- The Horse Racing

Were Tian Ji lives in nowadays, he will certainly laugh at himself. Even more, were he sitting in the ACM contest right now, he may discover that the horse racing problem can be simply viewed as finding the maximum matching in a bipartite graph. Draw Tian's horses on one side, and the king's horses on the other. Whenever one of Tian's horses can beat one from the king, we draw an edge between them, meaning we wish to establish this pair. Then, the problem of winning as many rounds as possible is just to find the maximum matching in this graph. If there are ties, the problem becomes more complicated, he needs to assign weights 0, 1, or -1 to all the possible edges, and find a maximum weighted perfect matching...

However, the horse racing problem is a very special case of bipartite matching. The graph is decided by the speed of the horses --- a vertex of higher speed always beat a vertex of lower speed. In this case, the weighted bipartite matching algorithm is a too advanced tool to deal with the problem.

In this problem, you are asked to write a program to solve this special case of matching problem.

(阅读全文…)