Review of Codeforces 5A and 5B(Python)

5A. Chat Server's Outgoing Traffic

Seems like a simple task. However, you may find it is difficult to tell the determination of the input. There are two method: first one is using sys.stdin. you may need to learn how to use sys model. The second method is using exeption method(try...catch)

source code as follows:

v, m = 0, 0
while True:
    try:
        temp = raw_input()
    except :
        break
    if temp[0] == '+':
        m += 1
    elif temp[0] == '-':
        m -= 1
    else:
        i = temp.index(':')
        v += len(temp[i+1::]) * m

print v

5B Center Alignment

if you have the exps of UI, this problem is easy to solve. also use try...catch to deal with the input, source code as follows:

v = []

while True:
    try:
        s = raw_input()
    except :
        break
    l = len(s)
    v.append([s, l])

vm = v[:]
vm.sort(key = lambda x : x[1])
width = vm[-1][1] + 2
cc = 0
print '*' * width
for i in xrange(len(v)):
    temp = v[i]
    if (width - temp[1]) % 2:
        if cc % 2 == 0:
            temp[0] += ' '
            temp[1] += 1
            cc += 1
        else :
            temp[0] = ' ' + temp[0]
            temp[1] += 1
            cc += 1
    sw = (width - temp[1]) / 2
    lw = '*' + ' '*(sw-1)
    rw = ' '*(sw-1) + '*'
    temp[0] = lw + temp[0] + rw
    print temp[0]
print '*' * width



你可能感兴趣的:(codeforces)