设为首页 收藏本站
查看: 416|回复: 0

[经验分享] Python解决codeforces ---- 3

[复制链接]

尚未签到

发表于 2017-5-1 07:55:55 | 显示全部楼层 |阅读模式
  

  第一题 7A
  


A. Kalevitch and Chess

time limit per test

2 seconds

memory limit per test

64 megabytes

input

standard input

output

standard output



[size=1em]
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.
[size=1em]
As before, the chessboard is a square-checkered board with the squares arranged in a8 × 8grid, 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.
[size=1em]
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.
[size=1em]
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

[size=1em]
The input file contains 8 lines, each of the lines contains 8 characters. The given matrix describes the client's requirements,Wcharacter stands for a white
square, andBcharacter — for a square painted black.
[size=1em]
It is guaranteed that client's requirments can be fulfilled with a sequence of allowed strokes (vertical/column or horizontal/row).



Output

[size=1em]
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[0] == 'B':
for j in range(8):
if matrix[j] == 'W':
break
if j == 7 and matrix[j] == 'B':
ans += 1        
# col
for i in range(8):
if matrix[0] == 'B':
for j in range(8):
if matrix[j] == 'W':
break
if j == 7 and matrix[j] == '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



[size=1em]
Peter likes to travel by train. He likes it so much that on the train he falls asleep.
[size=1em]
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.
[size=1em]
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.
[size=1em]
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.
[size=1em]
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.
[size=1em]
Peter's parents put lowercase Latin letters for colours. The same letter stands for the same colour, different letters — for different colours.



Input

[size=1em]
The input data contains three lines. The first line contains a non-empty string, whose length does not exceed105,
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.
[size=1em]
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

[size=1em]
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.

[size=1em]




Sample test(s)

input

atob
a
b



output

forward



input

aaacaaa
aca
aa



output

both







Note

[size=1em]
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 += 1
i += 1
return 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



[size=1em]
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.
[size=1em]
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.
[size=1em]
Yakko thrown a die and gotYpoints, Wakko —Wpoints. It was
Dot's turn. But she didn't hurry. Dot wanted to know for sure what were her chances to visit Transylvania.
[size=1em]
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

[size=1em]
The only line of the input file contains two natural numbersYandW
the results of Yakko's and Wakko's die rolls.



Output

[size=1em]
Output the required probability in the form of irreducible fraction in format «A/B», whereA
the numerator, andB— 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

[size=1em]
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+1
b = 6
print "%d/%d" % (a/gcd(a,b) , b/gcd(a,b))

  

  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-371435-1-1.html 上篇帖子: Python解决codeforces ---- 4 下篇帖子: python守护进程编写
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表