001:Set

天涯浪子 提交于 2020-02-24 04:26:26

总时间限制: 5000ms 内存限制: 100000kB
描述
现有一整数集(允许有重复元素),初始为空。我们定义如下操作:
add x 把x加入集合
del x 把集合中所有与x相等的元素删除
ask x 对集合中元素x的情况询问
对每种操作,我们要求进行如下输出。
add 输出操作后集合中x的个数
del 输出操作前集合中x的个数
ask 先输出0或1表示x是否曾被加入集合(0表示不曾加入),再输出当前集合中x的个数,中间用空格格开。
输入
第一行是一个整数n,表示命令数。0<=n<=100000。
后面n行命令,如Description中所述。
输出
共n行,每行按要求输出。
样例输入
7
add 1
add 1
ask 1
ask 2
del 2
del 1
ask 1
样例输出
1
2
1 2
0 0
0
2
1 0

#include<iostream>
#include<set>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
    int n;
    multiset<int> s;
    set<int> s1; //还要在新建一个set用来存所有存过的,来判断他是不是出现过
    string st; //终于记得用了
    cin >> n;
    int ii;
    for(ii = 0; ii < n; ii++)
    {
        cin >> st;
        if(st == "add")
        {
            int x;
            cin >> x;
            s.insert(x);
            s1.insert(x);
            cout << s.count(x) << endl;
        }
        else if(st == "del")
        {
            int x;
            cin >> x;
            cout << s.count(x) << endl;
            multiset<int>::iterator i;
            i = s.lower_bound(x);
            while(*i == x && i != s.end())
            {
                s.erase(i++);   //这里用了好长时间,有个疑问,删除函数的返回值不就是下一个的迭代器吗,为啥不能直接给i啊???
            }
        }
        else if(st == "ask")
        {
            int x;
            cin >> x;
            set<int>::iterator i1 = s1.find(x);
            if(i1 != s1.end())
            {
                cout << '1' << " " << s.count(x);
            }
            else cout << '0' << " " << s.count(x);
            cout << endl;
        }
    }
    return 0;
}

终于可算A了,睡觉了

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!