CF 5 A. Chat Server's Outgoing Traffic

题目:Chat Servers Outgoing Traffic

思路:水,map模拟

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <queue>
#include <map>
using namespace std;
map<string,int>m;
int main()
{
    m.clear();
    char s[110];
    string str;
    int ans=0;
    while(gets(s))
    {
        str="";
        int l=strlen(s);
        if(s[0]=='+')
        {
            for(int i=1;i<l;i++)
                str+=s[i];
            m[str]++;
        }
        else if(s[0]=='-')
        {
            for(int i=1;i<l;i++)
                str+=s[i];
            map<string,int>::iterator it=m.find(str);
            m.erase(it);
        }
        else
        {
            int cnt=0;
            for(int i=0;i<l;i++)
                if(s[i]==':')
                {
                    cnt=i;
                    break;
                }
            ans+=m.size()*(l-1-cnt);
            //cout<<l<<":"<<cnt<<endl;
        }
    }
    cout<<ans<<endl;
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/overflow/p/3187967.html