STL练习板子题,c++11出没

第一题 词典

总时间限制: 3000ms 内存限制: 65536kB

描述

你旅游到了一个国外的城市。那里的人们说的外国语言你不能理解。不过幸运的是,你有一本词典可以帮助你。

输入

首先输入一个词典,词典中包含不超过100000个词条,每个词条占据一行。每一个词条包括一个英文单词和一个外语单词,两个单词之间用一个空格隔开。而且在词典中不会有某个外语单词出现超过两次。词典之后是一个空行,然后给出一个由外语单词组成的文档,文档不超过100000行,而且每行只包括一个外语单词。输入中出现单词只包括小写字母,而且长度不会超过10。

输出

在输出中,你需要把输入文档翻译成英文,每行输出一个英文单词。如果某个外语单词不在词典中,就把这个单词翻译成“eh”。

样例输入

dog ogday

cat atcay

pig igpay

froot ootfray

loops oopslay

atcay

ittenkay

oopslay

样例输出

cat

eh

loops

#include<iostream>
#include<cstdio>
#include<fstream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<map>
#include<string>
#include<list>
using namespace std;
int read(){
    char ch;
    int res=0,f=1;
    ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        res=res*10+(ch-'0');
        ch=getchar();
    }
    return res*f;
}
map<string,string>u;
string s,k,g;
int y;
int main(){
    while(1){
        getline(cin,s);
        y=s.find(' ');
        if(y!=s.npos){
            k=s.substr(0,y+1);
            g=s.substr(y+1);
            u.insert(make_pair(g,k));
        }
        else break;
    }
    while(cin>>s){
        if(u.count(s))cout<<u[s]<<endl;
        else cout<<"eh"<<endl;
    }
    return 0;
}

第二题 list

总时间限制: 4000ms 内存限制: 65536kB

描述

写一个程序完成以下命令:

new id ——新建一个指定编号为id的序列(id<10000)

add id num——向编号为id的序列加入整数num

merge id1 id2——合并序列id1和id2中的数,并将id2清空

unique id——去掉序列id中重复的元素

out id ——从小到大输出编号为id的序列中的元素,以空格隔开

输入

第一行一个数n,表示有多少个命令( n<=200000)。以后n行每行一个命令。

输出

按题目要求输出。

样例输入

16

new 1

new 2

add 1 1

add 1 2

add 1 3

add 2 1

add 2 2

add 2 3

add 2 4

out 1

out 2

merge 1 2

out 1

out 2

unique 1

out 1

样例输出

1 2 3

1 2 3 4

1 1 2 2 3 3 4

1 2 3 4

#include<iostream>
#include<cstdio>
#include<fstream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<map>
#include<string>
#include<list>
using namespace std;
int read(){
    char ch;
    int res=0,f=1;
    ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        res=res*10+(ch-'0');
        ch=getchar();
    }
    return res*f;
}
list<int>u[10005];
string order;
int n,a,b;
int main(){
    n=read();
    for(int i=1;i<=n;++i){
        cin>>order;
        if(order=="new"){
            a=read();
        }
        else if(order=="add"){
            a=read();b=read();
            u[a].push_back(b);
        }
        else if(order=="merge"){
            a=read();b=read();
            u[a].splice(end(u[a]),u[b]);
        }
        else if(order=="unique"){
            a=read();
            u[a].sort();
            u[a].unique();
        }
        else{
            a=read();
            u[a].sort();
            for(auto iter=begin(u[a]);iter!=end(u[a]);++iter){
                printf("%d ",*iter);
            }
            printf("\n");
        }
    }
    return 0;
}

第三题 set

现有一整数集(允许有重复元素),初始为空。我们定义如下操作:

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<cstdio>
#include<fstream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<map>
#include<string>
#include<list>
#include<set>
using namespace std;
int read(){
    char ch;
    int res=0,f=1;
    ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        res=res*10+(ch-'0');
        ch=getchar();
    }
    return res*f;
}
int n,a;
multiset<int>u;
string order;
bool t[1000005];
int main(){
    n=read();
    for(int i=1;i<=n;++i){
        cin>>order;
        if(order=="add"){
            a=read();
            t[a]=1;
            u.insert(a);
            printf("%d\n",u.count(a));
        }
        else if(order=="del"){
            a=read();
            printf("%d\n",u.count(a));
            u.erase(a);
        }
        else{
            a=read();
            printf("%d %d\n",t[a],u.count(a));
        }
    }
    return 0;
}