您的位置:首页 > 其它

2013内部赛第一场D,F题 暨由此引发的思考

2013-04-21 21:29 267 查看
                                                                                                   

内部赛第一场D题

                                                 D Shut The  Box I

Their pursuit of the Collector has taken the Avengers on a long voyage through the galaxies, and they are looking for ways to pass time on their ship. They discover an old game, called shut the box, that used to be popular with sailors. Each player starts with 9 cards, numbered 1 to 9, laid face-up (i.e., the number showing) on the table, and continues playing till he or she cannot make any moves.

The game is played with a pair of dice (with a small twist that, only one die is used if the total of the open cards is ≤ 6). Initially,  all the cards are open (i.e., face-up). The player then roles the dice. Let

the total dice value be m.  The player selects any set of open cards whose face values sum to m, and closes (shuts) them by turning them face-down. For instance, say the player rolls a 6 and 2 the first time, with a total of 8. The player can shut either the card 8, or the cards 1 and 7, or the cards 2 and

6, or the three cards 1 and 2 and 5, and so on. As another example, if the current cards face up are:

1, 2, 6, and the player rolls a 4, then there are no cards that he can shut, and his turn is over.

The final score for the player is the sum of the numbers of the cards still face-up, and the aim is to get as low score as possible  (and ideally to shut the box, i.e., have no cards left facing up). In the above case where the game ends with face up cards 1, 2, and 6, the final score is: 1 + 2 + 6 = 9.



Figure 1: Illustration of Shut the Box game in two cases (in the right example, only the last few moves are shown). Note that, we only use 1 die when the total of open cards is less than or equal to 6 (in the right example)

Dr.  Banner (The Hulk)  has heard of the following strategy.  Consider all the valid alternatives  for shutting cards, given the current roll of the dice. If there is only one, do it.  If there are more than

one, take the one that maximizes the smallest face value. (For example, given the options {1,7} and

{2,6}, you would take {2,6}.)  If there are many that maximize the smallest face value, take the one

that maximizes the second smallest face value. (For example, given the options {2,4,6} and {2,3,7},

you would take  {2,4,6}.)   In general, among the valid options, select  the one that  maximizes the

lexicographically ordered sequence of face values.

Your goal is to write a program to help Dr.  Banner make this choice and, hopefully, win (you know the consequences of angering the Hulk).

Input/Output Format:

Input:  The first line in the test data file contains the number of test cases (< 100). After that, each line contains one test case:  the first entry on each line is the rolled total (called target), and the next entry is the number of open cards n.  The following n entries denote the open cards in the ascending order.

Output:   For each test case, you are to output  the best move according to the above strategy, or output that there is no legal move. The exact form is shown below.

Note:  We have provided a skeleton program that reads the input and prints the output.  All you need to do is provide the body of the following procedure:

private static void  solveShutTheBoxI(int[] values, int target)

Your code should set the variables solution and solution found appropriately.

Examples:

The output is shown with an extra blank line so that each test case input is aligned with the output;

the first blank line should not be present in the actual output.



 

题目大意:给出两个筛子点数,然后给出最大9个数,每个数最大为9,求点数能用这9个数组合出来的最大数.

思路:这道题数据量不大就是水题,因为直接枚举1,2,3,4就可以了,因为5个数的点数大于12了,所以枚举或者DFS都可以.

还有这一种方法也可行,那就是利用位运算:

        1 2 3 4 5 6 7 8 9

利用2^9次方枚举全部情况就可以了,还有这个不用从大到小枚举,因为这里刚好反过来,枚举小的二进制数相当于枚举字典序大的方案数.

数据量大的话可以考虑DP.

 

 

由此引发的思考(1)

位运算真的用得很灵活,可以唯一的表示很多种状态,是很多暴力题的重要解决方案之一.

昨天校队新队员选拔的一道题也很经典:

南华203 酒厂又生产了一批美酒,足足有一千瓶,但是,但是,坑爹的少主把老鼠药放

进某十瓶酒中准备清除203 的老鼠,却被木喜全部包装好了,当然毒酒不能给大家喝,于是

木喜决定找出这一瓶毒酒!嗯,已知老鼠喝了那种毒酒将在两个小时毒发身亡,现在他抓到

了十只老鼠,如果你是他,

请问你将怎么找那一瓶毒酒。(写出方法和估算一下时间效率,尽可能优)

 

 

思路:其实最优的方法可以只需要2个小时.

因为10只老鼠就可以表示2^10不同的种状态,所以最多都不用付出10只老鼠都死的情况就可以试出这一瓶毒酒,这个是O(1)的算法,当然也有O(logn)的二分算法.

这个就是利用二进制的想法.

最坏的的小白鼠试验情况就是1111111111,但是这道题没有1024来引发这玩家的思路.

但是这个二进制的用法真的非常之妙

 

 

 

[b]内部赛第一场F题:[/b]

                                                                                             F,Breaking  Vigen`ere Cipher

Nick Fury has gotten wind of the secret communications  between Black Widow and Hawkeye (who are using a Vigen`ere Cipher), and is worried about a possible rebellion. He started reading the description of the Vigen`ere  cipher on Wikipedia, and was discouraged  to find in the first paragraph that  it is considered le chiffre inchiffrable (’the indecipherable cipher’). He now wants your help to break it, in return promising not to tell The Hulk about how you ate his lunch.

You are however smarter than Nick, and continue reading the description of Vigen`ere  cipher, and discover that it is considered quite insecure and easy to break. In fact, Charles Babbage, considered a “father of the computer”, decrypted a sample of encrypted ciphertext in 1846, using a technique later published by Kasiski, and called “Kasiski examination”.

Without getting into too many specific details, Kasiski examination relies on guessing the length of the keyword by looking for repeated groups of letters in the ciphertext. For example, consider the following plaintext, keyword (repeated sufficiently), and ciphertext.

Keyword: ABCDABCDABCDABCDABCDABCDABCD Plaintext: CRYPTOISSHORTFORCRYPTOGRAPHY Ciphertext:   CSASTPKVSIQUTGQUCSASTPIUAQJB

The plaintext contains a repeated text CRYPTO, and because of the fortuitous (for you) alignment, both the occurrences were encrypted to the same ciphertext CSASTP. We will denote such a pair of repeated

text in the ciphertext by a triple:  (text, position1, position2).  The above repetition is denoted by:

(CSASTP, 1, 17).

For each such triple, we can compute the distance between the two occurences (in this case 17 - 1 =

16) and guess the keyword to be of a factor of that distance (in this case, the possibilities are: 1, 2, 4,

8, or 16). Since 1 and 2 are too small, best guesses are 4, 8, or 16.

The repeated group of letters may also be a coincidence,  so a keyword length-guessing algorithm needs to be aware of that possibility.

Your goal is to try to find possible key lengths given a ciphertext. For that purpose, you have to find every repeated group of letters of length exactly  3 in the ciphertext.  Say you find n such pairs of repeated groups:

(text1, pos11, pos21), (text2, pos12, pos22), · · · , (textn, pos1n, pos2n)

Let x1  = pos21 − pos11, · · · , xn  = pos2n − pos1n be the distances between the repeated occurences. The possible  guesses are found as follows: a number k is a guess for the length of the keyword if and

only if it is a factor of (i.e., it divides) at least 90% of the numbers in {x1, · · · , xn}.  Further, we will

assume that the key length is between 4 and 20 (inclusive). For example, consider the ciphertext:

Ciphertext:   VHVSSPQUCEMRVBVBBBVHVSURQGIBDUGRNICJQUCERVUAXSSR

There are two  repeated group of length 4, giving us a total  of 4 groups of length 3:  VHV,  HVS, QUC, and UCE. We will  count  these VHV  and HVS as separate  groups even though they overlap, naturally giving higher weight to longer repeated groups. The sequence of lengths is 18, 18, 30 and 30, respectively, and the only possible guess that satisfies the conditions above is 6.

Input/Output Format:

Input:  The first line in the test data file contains the number of test cases (< 100). After that, each line contains one test case with  a single string, the ciphertext. You can assume that the ciphertext contains only capital letters (from A to Z) – all numbers or punctuation marks (including white spaces) are stripped out.

Output:   For each test case,  you are to output  the guesses for keyword length between  4 and 20 (inclusive). The exact form of the output is shown below.

Note:  We have provided a skeleton program that reads the input and prints the output.  All you need to do is provide the body of the following procedure:

private static Vector<Integer> solveVigenereII(String cipherText)

The procedure should return a vector that contains the possible key lengths in increasing order.

Examples:

The output is shown with an extra blank line so that each test case input is aligned with the output;

the first blank line should not be present in the actual output.

 

题目大意:其实也是水题一道.但是英文不过关就是悲剧了,其实就是枚举4~20如果是x1~xn的公约数就可以了.法克.

但是在找的过程要注意:因为要确定以三个字符长度的字符串,如果暴力的话,就是O(n^2)的算法,字符串过长的话就over了.但是可以有更优的算法就是,利用哈希表,就是把出现过的字符串标记好,不用再回头去看当前的字符串有没有出现过.

当然可以好好的利用map

 

[b]由此引发的思考(2)[/b]

map比较好用,在比赛的时候对STL的灵活运用真的很重要,这可以省很多代码:

map在这道题可以记录某个字符串的序号,如果要记录他们的序号的话,那么就可以再额外开对应map相应序号的结构体就可以大大的扩展map的灵活性了.

 

sprintf对于调整字符串的格式还有将整数变成字符串是个很有用的方法.

还有

S.c_str();

这对于char 类型的字符串转化为string是很有用的

 

今天的队内赛,我们队伍表现还是蛮不错的,第一次试一下这样的配合,感觉还很不错,给Achilles加油一下!

 

 

 

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: