The Problem is from codeforces: http://www.codeforces.com/problemset/problem/5/A
In Python, there is no explicit EOF, therefore, to read until the end of input stream, one can check the input string, and check if it is zero-length.
Knowing this, the rest would be easy. To keep a counter of the current people in the chat server, and once a message is sent, the length would be updated with the length of the message multiplied with the number of people.
It is for sure the inputs are correct, therefore, there is no need to store the names of the people, i.e. there is no need to use the set structure/hash to check if the names have been in the server. We just need to use a counter, fairly simple.
#!/usr/bin/env python from sys import stdin cnt = 0 p = 0 while True: s = stdin.readline().strip() if len(s) <= 1: break if s[0] == '+': p += 1 elif s[0] == '-': p -= 1 else: x = s.find(':') cnt += len(s[x+1:]) * p print cnt
–EOF (The Ultimate Computing & Technology Blog) —
GD Star Rating
loading...
259 wordsloading...
Last Post: Arbitrary Base Number Converter
Next Post: Preloading Images using Javascript, CSS or Pure HTML