题意:一开始给个空的字典,然后对这个字典进行4种操作:
1. 添加操作
insert barty 8
给barty这个单词次数增加8(PS. 所有单词初始次数默认为0)
2. 删除操作
delete barty
从字典中把barty的数据删除,如果当前字典中不存在这个单词输出 Empty
3. 查询操作
query ty
查询以ty结尾的单词总次数
思路:因为n比较小,所以直接用map模拟字典就好了
PS:之前没有处理到查询的后缀可能比map中某些key的长度要长错了好几次,手测的时候才发现这个问题会导致程序崩溃
#include<algorithm>
#include<typeinfo>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<iomanip>
#include<stdio.h>
#include<math.h>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
#define pi acos(-1)
ll gcd(ll x, ll y) { return x ? gcd(y%x, x) : y; }
ll lcm(ll x, ll y) { return x * y / gcd(x, y); }
int t, n, xx;
int main() {
ios::sync_with_stdio(false);
//cin.tie(0);
cout << fixed << setprecision(6);
while(cin>>t)
while (t--) {
map<string, int>mp;
cin >> n;
while (n--) {
string x, y;
cin >> x >> y;
if (x == "insert")
cin >> xx, mp[y] += xx;
else if (x == "delete") {
if (mp[y])
mp[y] = 0;
else
cout << "Empty" << endl;
}
else if (x == "update") {
vector<string>ve;
string z;
cin >> z;
int ans = 1, ji = 0;
for (map<string, int>::iterator a = mp.begin(); a != mp.end(); a++) {
string w;
if (a->first.size() >= z.size()) {
w = a->first.substr(a->first.size() - z.size(), z.size());
if (w == z)
ji = 1;
}
if (a->first.size() < y.size())
continue;
w = a->first.substr(a->first.size() - y.size(), y.size());
if (w == y)
ve.push_back(a->first), ans=0;
}
if (ans)
cout << "Empty" << endl;
else if (ji)
cout << "Conflict" << endl;
else {
for (int a = 0; a < ve.size(); a++) {
string w = ve[a].substr(0, ve[a].size() - y.size());
w += z;
mp[w] = mp[ve[a]];
mp[ve[a]] = 0;
}
}
}
else {
int ans = 0;
for (map<string, int>::iterator a = mp.begin(); a != mp.end(); a++) {
if (a->first.size() < y.size())
continue;
string z = a->first.substr(a->first.size() - y.size(), y.size());
if (z == y)
ans += a->second;
}
cout << ans << endl;
}
}
}
return 0;
}