1.P5266 【深基17.例6】学籍管理 - 洛谷
#include<iostream>
#include<unordered_map>
using namespace std;
int n;
unordered_map<string, int> mp;
int main()
{
cin >> n;
while (n--)
{
int x; cin >> x;
string name;
if (x == 1)//插入修改
{
cin >> name;
int sco; cin >> sco;
mp[name] = sco;
cout << "OK" << endl;
}
else if (x == 2)
{
cin >> name;
if (mp.count(name))
{
cout << mp[name] << endl;
}
else
{
cout << "Not found" << endl;
}
}
else if(x == 3)
{
cin >> name;
if (mp.count(name))
{
mp.erase(name);
cout << "Deleted successfully" << endl;
}
else
{
cout << "Not found" << endl;
}
}
else
{
cout << mp.size() << endl;
}
}
return 0;
}
2.P4305 [JLOI2011] 不重复数字 - 洛谷
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<unordered_set>
using namespace std;
int n;
int main()
{
unordered_set<int> mp;
scanf("%d", &n);
while (n--)
{
int num; scanf("%d", &num);
while (num--)
{
int x; scanf("%d", &x);
if (!mp.count(x))
{
printf("%d ", x);//鉴于是无序的,所以插入顺序也就不会依照原本排序的顺序进行
mp.insert(x);
}
}
mp.clear();
cout << endl;
}
return 0;
}
3.P3879 [TJOI2010] 阅读理解 - 洛谷
#include<iostream>
#include<unordered_map>
#include<set>
using namespace std;
int n;
int l;
int al_n;
unordered_map<string, set<int>> mp;//前面存名字,后面用set存放在那篇文章里面,正好set里面有排序功能
int main()
{
cin >> n;
for(int i = 1;i <= n;i++)
{
cin >> l;
for (int j = 1; j <= l; j++)
{
string voc; cin >> voc;
mp[voc].insert(i);
}
}
cin >> al_n;
while (al_n--)
{
string x; cin >> x;
for (auto v : mp[x])
{
cout << v << " ";
}
cout << endl;
}
return 0;
}
4.记录详情 - 洛谷 | 计算机科学教育新生态
#include<iostream>
#include<unordered_map>
typedef long long LL;
using namespace std;
int n, c;
unordered_map<int, int> mp;
const int N = 2e5 + 10;
int a[N];
int main()
{
cin >> n >> c;
//把每个数出现的次数都记录一遍
for (int i = 1; i <= n; i++)
{
int x; cin >> x;
a[i] = x;
mp[x]++;
}
LL ret = 0;//可能会超出
//找目标mp[c+a[i]]的数字出现的次数
for (int i = 1; i <= n; i++)
{
ret += mp[c + a[i]];
}
cout << ret << endl;
return 0;
}
5.P3405 [USACO16DEC] Cities and States S - 洛谷
#include<iostream>
#include<unordered_map>
using namespace std;
int n;
//该城市的前两个字母和该城市所在州的简写拼成一个字符串,记录出现的次数
//然后相反的次数
unordered_map<string, int> mp;//拼接组合的字符串,出现的次数
int main()
{
cin >> n;
int ret = 0;
while (n--)
{
string a, b; cin >> a >> b;
string a_2 = a.substr(0, 2);//取前面两个字母
if (a_2 == b)continue;//排除处在同一个州的城市
ret += mp[b + a_2];//a_2+b 找 b+ a_2有多少个
mp[a_2 + b]++;//把a_2+b字母推入mp中
}
cout << ret << endl;
}