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

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

[复制链接]

尚未签到

发表于 2017-4-29 14:11:02 | 显示全部楼层 |阅读模式
  

  第一题 20A


A. BerOS file system

time limit per test

2 seconds

memory limit per test

64 megabytes

input

standard input

output

standard output



[size=1em]
The new operating system BerOS has a nice feature. It is possible to use any number of characters'/'as a delimiter in path instead of one traditional'/'.
For example, strings//usr///local//nginx/sbin//and/usr/local/nginx///sbinare
equivalent. The character'/'(or some sequence of such characters) at the end of the path is required only in case of the path to the root directory, which
can be represented as single character'/'.
[size=1em]
A path called normalized if it contains the smallest possible number of characters'/'.
[size=1em]
Your task is to transform a given path to the normalized form.



Input

[size=1em]
The first line of the input contains only lowercase Latin letters and character'/'— the path to some directory. All paths start with at least one character'/'.
The length of the given line is no more than 100 characters, it is not empty.



Output

[size=1em]
The path in normalized form.



Sample test(s)

input

//usr///local//nginx/sbin



output

/usr/local/nginx/sbin

















  题意:给定一个字符串路径,但是这个路径可能由多个/来分割,现在要求我们把这个路径简化为Linux下的路径格式,要求末尾不能有/,根目录是一个/
  思路:我们利用split()函数把字符串利用/来分割,然后我们利用join()函数来连接非空的值,但是要注意开头一定要有一个/(根目录)
  代码:

# solve
def solve():
str = raw_input()
print "/"+"/".join(s for s in str.split("/") if s)
if __name__ == "__main__":
solve()


  

  第二题 19A

A. World Football Cup

time limit per test

2 seconds

memory limit per test

64 megabytes

input

standard input

output

standard output



[size=1em]
Everyone knows that 2010 FIFA World Cup is being held in South Africa now. By the decision of BFA (Berland's Football Association) next World Cup will be held in Berland. BFA took the decision to change some World Cup regulations:
[size=1em]




  • the final tournament featuresnteams (nis always
    even)

  • the firstn / 2teams (according to the standings) come through to the knockout stage

  • the standings are made on the following principle: for a victory a team gets 3 points, for a draw — 1 point, for a defeat — 0 points. In the first place, teams are ordered in the standings in decreasing order of their points; in the second place — in decreasing
    order of the difference between scored and missed goals; in the third place — in the decreasing order of scored goals

  • it's written in Berland's Constitution that the previous regulation helps to order the teams without ambiguity.

[size=1em]

[size=1em]
You are asked to write a program that, by the given list of the competing teams and the results of all the matches, will find the list of teams that managed to get through to the knockout stage.



Input

[size=1em]
The first input line contains the only integern(1 ≤ n ≤ 50)
— amount of the teams, taking part in the final tournament of World Cup. The followingnlines contain the names of these teams, a name is a string of lower-case
and upper-case Latin letters, its length doesn't exceed 30 characters. The followingn·(n - 1) / 2lines describe the held matches in the formatname1-name2
num1:num2
, wherename1,name2— names of the
teams;num1,num2(0 ≤ num1, num2 ≤ 100)
— amount of the goals, scored by the corresponding teams. Accuracy of the descriptions is guaranteed: there are no two team names coinciding accurate to the letters' case; there is no match, where a team plays with itself; each match is met in the descriptions
only once.



Output

[size=1em]
Outputn / 2lines — names of the teams, which managed to get through to the knockout stage in lexicographical order. Output each name in a separate line.
No odd characters (including spaces) are allowed. It's guaranteed that the described regulations help to order the teams without ambiguity.



Sample test(s)

input

4
A
B
C
D
A-B 1:1
A-C 2:2
A-D 1:0
B-C 1:0
B-D 0:3
C-D 0:3



output

A
D



input

2
a
A
a-A 2:1



output

a





  题意:足球世界杯有n支足球队,现在第一轮两两之间较量,赢球的人得3分,平局得1分,输的0分。现在的排名按照得分高低,如果得分一样按照总进球分数和总输球分数的差,如果在一样按照总进球分数高底。要求进入第二轮的n/2支球队,按照字典序输出
  思路:利用Python的字典,字典的value是一个列表保存球队得分,总输球分,总进球分
  代码:
  

#coding=utf-8  
import os
import sys
# solve
def solve():
n = int(raw_input())
dic = {}
for i in range(n):
str = raw_input()
dic[str] = [0,0,0]
i = n*(n-1)/2
while i:
list = raw_input().split()
tmp1 = list[0].split("-")
tmp2 = list[1].split(":")
i -= 1
if(int(tmp2[0]) > int(tmp2[1])):
dic[tmp1[0]][0] += 3
dic[tmp1[0]][1] += int(tmp2[1])
dic[tmp1[0]][2] += int(tmp2[0])
dic[tmp1[1]][1] += int(tmp2[0])
dic[tmp1[1]][2] += int(tmp2[1])
elif(int(tmp2[0]) == int(tmp2[1])):
dic[tmp1[0]][0] += 1
dic[tmp1[0]][1] += int(tmp2[1])
dic[tmp1[0]][2] += int(tmp2[0])
dic[tmp1[1]][0] += 1
dic[tmp1[1]][1] += int(tmp2[0])
dic[tmp1[1]][2] += int(tmp2[1])
else:
dic[tmp1[0]][1] += int(tmp2[1])
dic[tmp1[0]][2] += int(tmp2[0])
dic[tmp1[1]][0] += 3
dic[tmp1[1]][1] += int(tmp2[0])
dic[tmp1[1]][2] += int(tmp2[1])
# out
ans = sorted(dic.items() , key = lambda value:(value[1][0],value[1][2]-value[1][1],value[1][2]) , reverse = True)
out = []
for i in range(n/2):
out.append(ans[0])
out.sort()
for s in out:
print s
# main
if __name__ == "__main__":
solve()

  

  第三题 21A


A. Jabber ID

time limit per test

2 seconds

memory limit per test

64 megabytes

input

standard input

output

standard output



[size=1em]
Jabber ID on the national Berland service «Babber» has a form<username>@<hostname>[/resource], where



  • <username>— is a sequence of Latin letters (lowercase or uppercase), digits or underscores characters «_»,
    the length of<username>is between 1 and 16, inclusive.

  • <hostname>— is a sequence of word separated by periods (characters «.»),
    where each word should contain only characters allowed for<username>, the length of each word is between 1 and 16, inclusive. The length of<hostname>is
    between 1 and 32, inclusive.

  • <resource>— is a sequence of Latin letters (lowercase or uppercase), digits or underscores characters «_»,
    the length of<resource>is between 1 and 16, inclusive.

[size=1em]

[size=1em]
There are the samples of correct Jabber IDs:mike@codeforces.com,007@en.codeforces.com/contest.
[size=1em]
Your task is to write program which checks if given string is a correct Jabber ID.



Input

[size=1em]
The input contains of a single line. The line has the length between 1 and 100 characters, inclusive. Each characters has ASCII-code between 33 and 127, inclusive.



Output

[size=1em]
PrintYESorNO.



Sample test(s)

input

mike@codeforces.com



output

YES



input

john.smith@codeforces.ru/contest.icpc/12



output

NO

















  题目:测试给定的字符串是否满足给定的格式
  思路:不解释,题目数据很变态
  代码:
  


#coding=utf-8  
import os
import sys
def isOk(ch):
if (ch.isalpha() or ch.isdigit() or ch == '_'):
return True
return False
# solve
def solve():
str = raw_input()
# judge username
pos = str.find('@')
if pos <= 0 or str.count('@') != 1:
return "NO"
for i in range(pos):
if not isOk(str):
return "NO"
# judge hostname
str = str[pos+1:]
if len(str) == 0:
return "NO"
while True:
pos = str.find('.')
if pos == -1:
if str.find('/') != -1:
break
for i in str:
if not isOk(i):
return "NO"
break
if pos == 0 or pos == len(str)-1:
return "NO"
for i in range(pos):
if not isOk(str):
return "NO"
str = str[pos+1:]
# judge resource
pos = str.find('/')
if pos != -1:
if pos == 0:
return "NO"
str = str[pos+1:]
if len(str) == 0:
return "NO"
for i in str:
if not isOk(i):
return "NO"
return "YES"
# main
if __name__ == "__main__":
print solve()


  

运维网声明 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-370863-1-1.html 上篇帖子: python数组的使用 (转) 下篇帖子: 一位大牛整理的Python资料 【转】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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