P5266 【深基17.例6】学籍管理(set+map
题目描述
传送门
您要设计一个学籍管理系统,最开始学籍数据是空的,然后该系统能够支持下面的操作(不超过 100000条):
插入与修改,格式1 NAME SCORE:在系统中插入姓名为 NAME(由字母和数字组成不超过 20 个字符的字符串,区分大小写) ,分数为 SCORE(0<SCORE<10000) 的学生。如果已经有同名的学生则更新这名学生的成绩为 SCORE。如果成功插入或者修改则输出OK。
查询,格式2 NAME:在系统中查询姓名为 NAME 的学生的成绩。如果没能找到这名学生则输出Not found,否则输出该生成绩。
删除,格式3 NAME:在系统中删除姓名为 NAME 的学生信息。如果没能找到这名学生则输出Not found,否则输出Deleted successfully。
汇总,格式4:输出系统中学生数量。
1
2
3
4
输入格式
无
输出格式
无
输入输出样例
输入 #1
5
1 lxl 10
2 lxl
3 lxl
2 lxl
4
输出 #1
OK
10
Deleted successfully
Not found
0
又到了快乐的水博客 题解时间。
数据范围10的5次方,有删除,修改,查询操作,想到用set,但是题目又有字符串和数字组成的整体,可以用结构体把它们封装起来,但是题目又说已知字符串要求查找出分数,那么就用map了!
下面是喜闻乐见的代码
#include <iostream>
#include <algorithm>
#include <math.h>
#include <string>
#include <vector>
#include <map>
#include <stack>
#include <cstdio>
#include <list>
#include <queue>
#include <set>
using namespace std;
#define ll long long
set<string> sst;
map<string,int> mmp;
set<string>::iterator p;
int main(){
int n,a,i,x,m;
string s1;
cin>>n;
for(i=0;i<n;i++){
cin>>m;
if(m==1){
cin>>s1>>a;
mmp[s1]=a;
sst.insert(s1);
cout<<"OK"<<endl;
}
if(m==2){
cin>>s1;
if(sst.empty()==0&&*sst.lower_bound(s1)==s1)
cout<<mmp[s1]<<endl;
else cout<<"Not found"<<endl;
}
if(m==3){
cin>>s1;
if(sst.empty()==0&&*sst.lower_bound(s1)==s1){
sst.erase(sst.lower_bound(s1));
cout<<"Deleted successfully"<<endl;
}
else cout<<"Not found"<<endl;
}
if(m==4){
cout<<sst.size()<<endl;
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
2020/7/19
补充:
头文件#include<unordered_map>
unordered_map 的定义与用法都与 map 差不多,只不过是用Hash来存储的,判断是O(1)的。
而map判断是O(logn)的。
如果没有找到,则mmp[i]=0。
例题传送门:
传送门
题目描述
给定 n 个数,要求把其中重复的去掉,只保留第一次出现的数。
输入格式
本题有多组数据。
第一行一个整数 T,表示数据组数。
对于每组数据:
第一行一个整数 n。
第二行 n 个数,表示给定的数。
输出格式
对于每组数据,输出一行,为去重后剩下的数,两个数之间用一个空格隔开。
输入输出样例
输入 #1
2
11
1 2 18 3 3 19 2 3 6 5 4
6
1 2 3 4 5 6
输出 #1
1 2 18 3 19 6 5 4
1 2 3 4 5 6
说明/提示
对于 30% 的数据,n≤100,给出的数 ∈[0,100]。
对于 60% 的数据,n≤10^4,给出的数 ∈[0,104]。
对于 100%的数据,1≤T≤50,1≤n≤5×10^4,给出的数在 32 位有符号整数范围内。
这题本意是hash但是俺偷懒水水水 ,用unordered_map能水过。。
快读:
inline int read()
{
char c=getchar();int x=0,f=1;
for(;!isdigit(c);c=getchar())if(c=='-')f=-1;
for(;isdigit(c);c=getchar())x=x*10+c-48;
return x*f;
}
1
2
3
4
5
6
7
(不用快读会TLE,用scanf输入不会T
#include <iostream>
#include <algorithm>
#include <cstring>
#include <math.h>
#include <string>
#include <vector>
#include <map>
#include <stack>
#include <cstdio>
#include <list>
#include <queue>
#include <set>
#include <unordered_map>
using namespace std;
#define ll long long
unordered_map<int,int > mmp;
inline int read()
{
char c=getchar();int x=0,f=1;
for(;!isdigit(c);c=getchar())if(c=='-')f=-1;
for(;isdigit(c);c=getchar())x=x*10+c-48;
return x*f;
}
int main(){
int t,n,i,x;
t=read();
while(t--){
cin>>n;
for(i=1;i<=n;i++){
x=read();
if(mmp[x]==0) {
mmp[x]=i;
cout<<x<<' ';
}
}
mmp.clear();
cout<<endl;
}
}