[ICPC2024 Xi'an I] Rubbish Sorting
2024年12月24日小于 1 分钟
#include <bits/stdc++.h>
using namespace std;
// max hash
const int MAXN = 27 * 27 * 27 * 27 * 27;
int q;
string s; // 当前字符串
int x; // 当前垃圾类型
int typ[MAXN + 5]; // 每种字符串的最小类型值
// 当前匹配下标、当前 hash 值
void dfs1(int now, int hsh)
{
if (now == 5)
{
if (!typ[hsh] || x < typ[hsh])
typ[hsh] = x;
return;
}
dfs1(now + 1, hsh * 27);
if (now < s.size())
dfs1(now + 1, hsh * 27 + (s[now] - 'a' + 1));
}
int ans, ansLen; // 当前匹配的类型、对应的匹配的位数
// 当前匹配下标、当前 hash 值,相同位数
void dfs2(int now, int hsh, int cnt)
{
if (now == 5)
{
if (!typ[hsh])
return;
if (cnt > ansLen ||
cnt == ansLen && typ[hsh] < ans)
{
ans = typ[hsh];
ansLen = cnt;
}
return;
}
dfs2(now + 1, hsh * 27, cnt);
if (now < s.size())
dfs2(now + 1, hsh * 27 + (s[now] - 'a' + 1), cnt + 1);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> q;
while (q--)
{
int op;
cin >> op;
if (op == 1)
{
cin >> s >> x;
// s 的类型为 x
dfs1(0, 0);
}
if (op == 2)
{
cin >> s;
// 输出 s 最接近的类型
ansLen = -1; // 接近程度
dfs2(0, 0, 0);
cout << ans << "\n";
}
}
return 0;
}