您的位置:首页 > 编程语言 > Python开发

Python解决codeforces ---- 3

2013-10-06 22:00 288 查看
 第一题 7A

A. Kalevitch and Chess

time limit per test
2 seconds

memory limit per test
64 megabytes

input
standard input

output
standard output

A famous Berland's painter Kalevitch likes to shock the public. One of his last obsessions is chess. For more than a thousand years people have been playing this old game on uninteresting, monotonous boards. Kalevitch decided to put an end to this tradition
and to introduce a new attitude to chessboards.

As before, the chessboard is a square-checkered board with the squares arranged in a 8 × 8 grid, each square is painted black or white. Kalevitch suggests that chessboards
should be painted in the following manner: there should be chosen a horizontal or a vertical line of 8 squares (i.e. a row or a column), and painted black. Initially the whole chessboard is white, and it can be painted in the above described way one or more
times. It is allowed to paint a square many times, but after the first time it does not change its colour any more and remains black. Kalevitch paints chessboards neatly, and it is impossible to judge by an individual square if it was painted with a vertical
or a horizontal stroke.

Kalevitch hopes that such chessboards will gain popularity, and he will be commissioned to paint chessboards, which will help him ensure a comfortable old age. The clients will inform him what chessboard they want to have, and the painter will paint a white
chessboard meeting the client's requirements.

It goes without saying that in such business one should economize on everything — for each commission he wants to know the minimum amount of strokes that he has to paint to fulfill the client's needs. You are asked to help Kalevitch with this task.

Input

The input file contains 8 lines, each of the lines contains 8 characters. The given matrix describes the client's requirements, W character stands for a white
square, and B character — for a square painted black.

It is guaranteed that client's requirments can be fulfilled with a sequence of allowed strokes (vertical/column or horizontal/row).

Output

Output the only number — the minimum amount of rows and columns that Kalevitch has to paint on the white chessboard to meet the client's requirements.

Sample test(s)

input
WWWBWWBW
BBBBBBBB
WWWBWWBW
WWWBWWBW
WWWBWWBW
WWWBWWBW
WWWBWWBW
WWWBWWBW


output
3


input
WWWWWWWW
BBBBBBBB
WWWWWWWW
WWWWWWWW
WWWWWWWW
WWWWWWWW
WWWWWWWW
WWWWWWWW


output
1


  

  题意:一个8*8的平板初始化每个点的颜色为白色,现在可以给平板涂色,但是每次只能够涂一行或者一列,现在给定已经涂好的平板,问最少需要涂几次

 思路:直接暴力枚举第一行和第一列判断即可,但是要注意如果整个都是平板都是黑色只要8次

 代码:

# input
matrix = []
for i in range(8):
matrix.append(raw_input())

# getAns
ans = 0
# row
for i in range(8):
if matrix[i][0] == 'B':
for j in range(8):
if matrix[i][j] == 'W':
break
if j == 7 and matrix[i][j] == 'B':
ans += 1
# col
for i in range(8):
if matrix[0][i] == 'B':
for j in range(8):
if matrix[j][i] == 'W':
break
if j == 7 and matrix[j][i] == 'B':
ans += 1
if ans == 16:
ans = 8
print ans


 第二题 8A

A. Train and Peter

time limit per test
1 second

memory limit per test
64 megabytes

input
standard input

output
standard output

Peter likes to travel by train. He likes it so much that on the train he falls asleep.

Once in summer Peter was going by train from city A to city B, and as usual, was sleeping. Then he woke up, started to look through the window and noticed that every railway station has a flag of a particular colour.

The boy started to memorize the order of the flags' colours that he had seen. But soon he fell asleep again. Unfortunately, he didn't sleep long, he woke up and went on memorizing the colours. Then he fell asleep again, and that time he slept till the end of
the journey.

At the station he told his parents about what he was doing, and wrote two sequences of the colours that he had seen before and after his sleep, respectively.

Peter's parents know that their son likes to fantasize. They give you the list of the flags' colours at the stations that the train passes sequentially on the way from A to B, and ask you to find out if Peter could see those sequences on the way from A to B,
or from B to A. Remember, please, that Peter had two periods of wakefulness.

Peter's parents put lowercase Latin letters for colours. The same letter stands for the same colour, different letters — for different colours.

Input

The input data contains three lines. The first line contains a non-empty string, whose length does not exceed 105,
the string consists of lowercase Latin letters — the flags' colours at the stations on the way from A to B. On the way from B to A the train passes the same stations, but in reverse order.

The second line contains the sequence, written by Peter during the first period of wakefulness. The third line contains the sequence, written during the second period of wakefulness. Both sequences are non-empty, consist of lowercase Latin letters, and the
length of each does not exceed 100 letters. Each of the sequences is written in chronological order.

Output

Output one of the four words without inverted commas:

«forward» — if Peter could see such sequences only on the way from A to B;

«backward» — if Peter could see such sequences on the way from B to A;

«both» — if Peter could see such sequences both on the way from A to B, and on the way from B to A;

«fantasy» — if Peter could not see such sequences.

Sample test(s)

input
atob
a
b


output
forward


input
aaacaaa
aca
aa


output
both


Note

It is assumed that the train moves all the time, so one flag cannot be seen twice. There are no flags at stations A and B.

 题意:给定一个字符串,在给定两个子串,问两个子串存在于给定字符串是4种情况中的哪一种

 思路:暴力判断

 代码:

# input
normal_str = raw_input()
reverse_str = normal_str[::-1]
seq_one = raw_input()
seq_two = raw_input()

length = len(normal_str)
len_seq_one = len(seq_one)
len_seq_two = len(seq_two)

# judge
def isOk(str):

global seq_one
global seq_two
length = len(normal_str)
len_seq_one = len(seq_one)
len_seq_two = len(seq_two)

i = 0
while i < length:
if i+len_seq_one < length and str[i:i+len_seq_one:] == seq_one:
j = i+len_seq_one
while j < length:
if j+len_seq_two <= length and str[j:j+len_seq_two:] == seq_two:
return True
j += 1i += 1return False

# solve
forward = isOk(normal_str)
backward = isOk(reverse_str)

# output
if forward and backward:
print "both"
elif forward and backward == False:
print "forward"
elif forward == False and backward:
print "backward"
else:
print "fantasy"



 第三题 9A

A. Die Roll

time limit per test
1 second

memory limit per test
64 megabytes

input
standard input

output
standard output

Yakko, Wakko and Dot, world-famous animaniacs, decided to rest from acting in cartoons, and take a leave to travel a bit. Yakko dreamt to go to Pennsylvania, his Motherland and the Motherland of his ancestors. Wakko thought about Tasmania, its beaches, sun
and sea. Dot chose Transylvania as the most mysterious and unpredictable place.

But to their great regret, the leave turned to be very short, so it will be enough to visit one of the three above named places. That's why Yakko, as the cleverest, came up with a truly genius idea: let each of the three roll an ordinary six-sided die, and
the one with the highest amount of points will be the winner, and will take the other two to the place of his/her dreams.

Yakko thrown a die and got Y points, Wakko — W points. It was
Dot's turn. But she didn't hurry. Dot wanted to know for sure what were her chances to visit Transylvania.

It is known that Yakko and Wakko are true gentlemen, that's why if they have the same amount of points with Dot, they will let Dot win.

Input

The only line of the input file contains two natural numbers Y and W —
the results of Yakko's and Wakko's die rolls.

Output

Output the required probability in the form of irreducible fraction in format «A/B», where A —
the numerator, and B — the denominator. If the required probability equals to zero, output «0/1».
If the required probability equals to 1, output «1/1».

Sample test(s)

input
4 2


output
1/2


Note

Dot will go to Transylvania, if she is lucky to roll 4, 5 or 6 points.

 题意:三个人掷筛子,前面两个人扔出了y和w,问第三个人仍的分数x是最大的概率,输出的格式一定要按照A/B

 思路:求出x的可能值,然后直接输出,注意要最简形式

 代码:

# input
list = raw_input().split()
x = max(int(list[0]) , int(list[1]))

def gcd(a , b):
if b == 0:
return a
return gcd(b , a%b)

a = 6-x+1b = 6

print "%d/%d" % (a/gcd(a,b) , b/gcd(a,b))

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