A - Chat Server's Outgoing Traffic
CodeForces - 5A
Polycarp is working on a new project called "Polychat". Following modern tendencies in IT, he decided, that this project should contain chat as well. To achieve this goal, Polycarp has spent several hours in front of his laptop and implemented a chat server that can process three types of commands:
- Include a person to the chat ('Add' command).
- Remove a person from the chat ('Remove' command).
- Send a message from a person to all people, who are currently in the chat, including the one, who sends the message ('Send' command).
Now Polycarp wants to find out the amount of outgoing traffic that the server will produce while processing a particular set of commands.
Polycarp knows that chat server sends no traffic for 'Add' and 'Remove' commands. When 'Send' command is processed, server sends l bytes to each participant of the chat, where l is the length of the message.
As Polycarp has no time, he is asking for your help in solving this problem.
Input
Input file will contain not more than 100 commands, each in its own line. No line will exceed 100 characters. Formats of the commands will be the following:
- +<name> for 'Add' command.
- -<name> for 'Remove' command.
- <sender_name>:<message_text> for 'Send' command.
<name> and <sender_name> is a non-empty sequence of Latin letters and digits. <message_text> can contain letters, digits and spaces, but can't start or end with a space. <message_text> can be an empty line.
It is guaranteed, that input data are correct, i.e. there will be no 'Add' command if person with such a name is already in the chat, there will be no 'Remove' command if there is no person with such a name in the chat etc.
All names are case-sensitive.
Output
Print a single number — answer to the problem.
Examples
Input
+Mike
Mike:hello
+Kate
+Dmitry
-Dmitry
Kate:hi
-Kate
Output
9
Input
+Mike
-Mike
+Mike
Mike:Hi I am here
-Mike
+Kate
-Kate
Output
14
在训练赛上第一次遇到这种题,死磕了近两个小时没过。
题意:
输入:输入字符串
1、前面带“+”号的,代表加入一个聊天的人
2、前面带“-”号的,代表删除一个聊天的人
3、第三种操作,发送操作
输出:坑点就在输出上,看完题意和样例后会发现,我要在哪里加“print”输出“sum”???
我们需要不断的输入字符串,然后如果有“发送”命令就计算产生的流量= 发送内容的长度+正在聊天的人数。
解决:解决的过程经历三个阶段
1、一开始我找不到"print"要放在哪,但是我可以根据题意把三个操作完成,然后看到题目中有这么一句话“When 'Send' command is processed, server sends l bytes to each participant of the chat”(当“发送”命令被执行时,服务器就像所有参与聊天的人发送产生的流量),所以我就以为只要有“发送”命令就发送一次,结果。。。第一组样例都没过。。
2、那就重新改变思路,看着样例写,发现只要有执行过“发送”命令的人被删除,就会发送流量,结果。。
3、最后,在秒AC了这道题的大佬指点下,我才知道,最大的坑点就在于输出语句只要放在循环输入外边就行了,也就是不用管它什么时候输出。电脑上手动推出循环输入时“Ctrl+z”,判题系统应该也是这么处理这道题的。
最后,贴上代码
#include <bits/stdc++.h>
using namespace std;
struct Person{
char name[100];
int temp;
}P[105];
int main(){
char s[105];
int k = 0;
int sum = 0;
for (int i = 0;i < 105;i++){
P[i].temp = 0;
}
while (gets(s)){
int sss = 0;
if (s[0] == '+'){
strcpy(P[k++].name,s+1);
}
else if (s[0] == '-'){
for (int i = 0;i < k;i++){
if (strcmp(P[i].name,s+1) == 0){
for (int j = i;j < k-1;j++){
strcpy(P[j].name,P[j+1].name);
}
k--;
break;
}
}
}
else {
int c = 0;
int l = strlen(s);
char t[105];
for (int i = 0;s[i] != ':';i++){
t[i] = s[i];
}
for (int i = 0;i < k;i++){
if (strcmp(P[i].name,t) == 0){
P[i].temp = 1;
}
}
for (int i = l-1;s[i] != ':';i--){
c++;
}
sum += c*k;
}
} printf("%d\n",sum);
}