签到完看到C没什么人过就溜乐。
A.淘宝的推荐系统
直接DP,时间复杂度$O(∑nd)$
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i(a); i <= (b); ++i) #define dec(i, a, b) for (int i(a); i >= (b); --i) #define MP make_pair #define fi first #define se second typedef long long LL; const int N = 2e5 + 10; int f[N]; int T; int ans; int n, d; int main(){ scanf("%d", &T); while (T--){ scanf("%d%d", &n, &d); memset(f, 0, sizeof f); ans = 0; rep(i, 1, n){ int x; scanf("%d", &x); int mx = 0; rep(j, x - d, x + d){ if (j > 0) mx = max(mx, f[j]); } f[x] = max(f[x], mx + 1); ans = max(ans, f[x]); } printf("%d\n", ans); } return 0; }
B.阿里巴巴的手机代理商(简单)
map直接暴力。
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i(a); i <= (b); ++i) #define dec(i, a, b) for (int i(a); i >= (b); --i) #define MP make_pair #define fi first #define se second typedef long long LL; map <string, int> mp; int T; int n; string op, ss, s; int main(){ std::ios::sync_with_stdio(false); cin >> T; while (T--){ cin >> n; mp.clear(); rep(i, 1, n){ cin >> op; if (op[0] == ‘i‘){ int y; cin >> ss >> y; mp[ss] += y; } else if (op[0] == ‘q‘){ cin >> ss; int len = ss.length(); int ans = 0; for (auto u : mp){ string s = u.fi; int sz = s.length(); if (sz < len) continue; int fg = 1; int p = -1; rep(i, sz - len, sz - 1){ ++p; if (s[i] != ss[p]){ fg = 0; break; } } if (fg) ans += u.se; } cout << ans << endl; } else if (op[0] == ‘d‘){ cin >> ss; if (mp[ss] == 0) cout << "Empty" << endl; mp[ss] = 0; } } } return 0; }
C.阿里巴巴的手机代理商(中等)
看起来好像是个字典树上的模拟……待补
D. 阿里巴巴的手机代理商(困难)
留坑。听zlc1114说是……暴力?
原文地址:https://www.cnblogs.com/cxhscst2/p/9034044.html
时间: 2024-11-09 05:28:33