原題連結:https://www.luogu.com.cn/problem/P5266
題意解讀:本題考察map的應用。
解題思路:直接使用map即可解題。
100分程式碼:
#include <bits/stdc++.h>
using namespace std;
map<string, int> h;
string name;
int n, op, score;
int main()
{
cin >> n;
while(n--)
{
cin >> op;
if(op == 1)
{
cin >> name >> score;
h[name] = score;
cout << "OK" << endl;
}
else if(op == 2)
{
cin >> name;
if(h.count(name)) cout << h[name] << endl;
else cout << "Not found" << endl;
}
else if(op == 3)
{
cin >> name;
if(h.count(name))
{
h.erase(h.find(name)); //查詢name所在位置並刪除
cout << "Deleted successfully" << endl;
}
else cout << "Not found" << endl;
}
else cout << h.size() << endl;
}
return 0;
}