Python解决codeforces ---- 7

简介:  第一题 20A A. BerOS file system time limit per test 2 seconds memory limit per test 64 megabytes ...


 第一题 20A

A. BerOS file system
time limit per test
2 seconds
memory limit per test
64 megabytes
input
standard input
output
standard output

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///sbin are 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 '/'.

A path called normalized if it contains the smallest possible number of characters '/'.

Your task is to transform a given path to the normalized form.

Input

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

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

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:

  • the final tournament features n teams (n is always even)
  • the first n / 2 teams (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.

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

The first input line contains the only integer n (1 ≤ n ≤ 50) — amount of the teams, taking part in the final tournament of World Cup. The following n lines 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 following n·(n - 1) / 2 lines describe the held matches in the format name1-name2 num1:num2, wherename1name2 — names of the teams; num1num2 (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

Output n / 2 lines — 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[i][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

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.

There are the samples of correct Jabber IDs: mike@codeforces.com007@en.codeforces.com/contest.

Your task is to write program which checks if given string is a correct Jabber ID.

Input

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

Print YES or NO.

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[i]):
           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[i]):
              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()



目录
打赏
0
0
0
0
15
分享
相关文章
Python解决codeforces ---- 6
 第一题 16A A. Flag time limit per test 2 seconds memory limit per test 64 megabytes input stan...
920 0
Python解决codeforces ---- 4
 第一题 10A A. Power Consumption Calculation time limit per test 1 second memory limit per test 256...
1178 0
Python解决codeforces ---- 5
 第一题 13A A. Numbers time limit per test 1 second memory limit per test 64 megabytes input st...
1261 0
Python解决codeforces ---- 2
 第一题 4A A. Watermelon time limit per test 1 second memory limit per test 64 megabytes in...
1319 0
Python解决codeforces ---- 3
 第一题 7A A. Kalevitch and Chess time limit per test 2 seconds memory limit per test 64 megabytes ...
1146 0
Python解决codeforces ---- 1
 第一题 1A A. Theatre Square time limit per test 2 seconds memory limit per test 64 megabytes ...
1045 0
探索Python编程:从基础到高级
在这篇文章中,我们将一起深入探索Python编程的世界。无论你是初学者还是有经验的程序员,都可以从中获得新的知识和技能。我们将从Python的基础语法开始,然后逐步过渡到更复杂的主题,如面向对象编程、异常处理和模块使用。最后,我们将通过一些实际的代码示例,来展示如何应用这些知识解决实际问题。让我们一起开启Python编程的旅程吧!
Python编程入门:从零基础到实战应用
本文是一篇面向初学者的Python编程教程,旨在帮助读者从零开始学习Python编程语言。文章首先介绍了Python的基本概念和特点,然后通过一个简单的例子展示了如何编写Python代码。接下来,文章详细介绍了Python的数据类型、变量、运算符、控制结构、函数等基本语法知识。最后,文章通过一个实战项目——制作一个简单的计算器程序,帮助读者巩固所学知识并提高编程技能。
[oeasy]python053_学编程为什么从hello_world_开始
视频介绍了“Hello World”程序的由来及其在编程中的重要性。从贝尔实验室诞生的Unix系统和C语言说起,讲述了“Hello World”作为经典示例的起源和流传过程。文章还探讨了C语言对其他编程语言的影响,以及它在系统编程中的地位。最后总结了“Hello World”、print、小括号和双引号等编程概念的来源。
128 80
|
2月前
|
Python高性能编程:五种核心优化技术的原理与Python代码
Python在高性能应用场景中常因执行速度不及C、C++等编译型语言而受质疑,但通过合理利用标准库的优化特性,如`__slots__`机制、列表推导式、`@lru_cache`装饰器和生成器等,可以显著提升代码效率。本文详细介绍了这些实用的性能优化技术,帮助开发者在不牺牲代码质量的前提下提高程序性能。实验数据表明,这些优化方法能在内存使用和计算效率方面带来显著改进,适用于大规模数据处理、递归计算等场景。
76 5
Python高性能编程:五种核心优化技术的原理与Python代码

热门文章

最新文章

AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等