2018计蒜之道 第二场 B

题目链接

题意:一开始给个空的字典,然后对这个字典进行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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值