题解:P10564 [ICPC 2024 Xi'an I] Rubbish Sorting
Asahina_Mafuyu · · 题解
前言
别样的 phigros 大战:
一下课,AKPC 便来到我的身边 ,说到:“你敢不敢与我举行 phigros 大战?”
想了下自己
我轻蔑地笑了:“来就来,谁不来谁永远 AP 不了
AKPC 挠了挠头:“或许我们请到奥托普雷先生可以敲击桌面?”
我和他大战一百回合,从 Rrhar'il 到 Destruction 3,2,1,我们次次 AP,直到有一首歌难到了我们,就是 Rubbish Sorting SP.?。我跟他再次大战八百回合,不分上下,于是我们来到了 luogu 做这道题目,我们同时被卡常,但是由于我跑得更快,他被我击败了。
于是我发了这篇题解。
思路
由于
对于一个字符串,我们先将末位补全到 % 补全,再枚举每一位的状态(要么不改变,要么将其变成 %),作为 key 将其存入 map 中(值即为相似度,若没有就为 % 的个数,因为不同的就用 % 代替了。
同样的,我们对于每一次询问,将询问的子串进行枚举,询问 map 内满足匹配度最大的字符串对应的最小分类即可。
代码:
#include <bits/stdc++.h>
using namespace std;
int q, x, ans, p;
map<string, int> mp;
map<string, int> op;
struct node {
int p, v;
node(int p_ = -1, int v_ = 1e9) : p(p_), v(v_) {}
};
void dfs1(string s, int cur) {//枚举状态并存入
if (cur == 5) {
int cnt = 0;
for (char c : s) if (c != '%') cnt ++;//匹配度
if (!mp.count(s) || cnt > mp[s] || (cnt == mp[s] && x < op[s])) {
mp[s] = cnt;
op[s] = x;
}
return;
}
dfs1(s, cur + 1);//改变或者不改变
char tmp = s[cur];
s[cur] = '%';
dfs1(s, cur + 1);
s[cur] = tmp;
return ;
}
node dfs2(string s, int cur) {
if (cur == 5) {
if (mp.count(s)) return node(mp[s], op[s]);
return node();
}
node res = dfs2(s, cur + 1);
char tmp = s[cur];
s[cur] = '%';
node res2 = dfs2(s, cur + 1);
if (res2.p > res.p || (res2.p == res.p && res2.v < res.v)) res = res2;
return res;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> q;
while (q --) {
int o;
string s;
cin >> o >> s;
while (s.size() < 5) s += '%';//补全长度
if (o == 1) {
cin >> x;
dfs1(s, 0);
}
else {
node res = dfs2(s, 0);
cout << res.v << '\n';
}
}
return 0;
}
这段代码的做法是正确的,但是 T 掉了。
于是我们进行卡常:
A掉了!!!
后记(by AKPC)
第
第
我知道,Rubbish Sorting SP.? 的对决,正是决胜的关键。我竟然在这样的决胜局因 TLE 而战败,被迫遗憾离场,对我的打击比把我 rks 清零还大。
被击败记录。