CF 5A. Chat Server's Outgoing TrafficClick to open the link
The meaning of the question: simulate a chat system +name means someone enters the chat system, -name means go out, name: means send a message. Whenever someone sends a message, the message will be sent to everyone currently in the chat system, including himself, to find the total number of bytes.
Idea: record the total number of people in the current chat system, and then multiply it by the number of bytes of information and add them up
#include <iostream> #include<cstring> #include<cstdio> using namespace std; char ch[105]; intmain() { int ans=0; int sum=0; while(gets(ch)){ int byte=0,flag=0; if(ch[0]=='+') sum++; else if(ch[0]=='-') sum--; else{ int len = strlen (ch); for(int i=0;i<len;i++){ if(flag) byte++; if(ch[i]==':') flag=1; } } ans+=byte*sum; } cout<<ans<<endl; return 0; }