Present
Mier_Samuelle · · 题解
官方题解。
根据题意,
故可以直接枚举
容易发现统计时有大量重复。考虑将所有
:::info[Bonus]{open}
若允许使用提高级算法,则本题可使用状压 dp 做到
:::
:::success[实现]{open}
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 10;
int cnt[MAXN][MAXN], p[MAXN], n, ans;
bool vis[MAXN];
string s, t;
void dfs(int step){
if (step > 9){
int tot = 0;
for (int i = 0; i <= 9; i++){
tot += cnt[i][p[i]];
}
ans = max(ans, tot);
return;
}
for (int i = 0; i <= 9; i++){
if (vis[i]){
continue;
}
vis[i] = true;
p[step] = i;
dfs(step + 1);
vis[i] = false;
}
return;
}
void solve(){
memset(cnt, 0, sizeof(cnt));
cin >> n >> s >> t;
for (int i = 0; i < n; i++){
cnt[s[i] - '0'][t[i] - '0']++;
}
ans = 0;
dfs(0);
cout << ans << endl;
return;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T;
cin >> T;
while (T--){
solve();
}
return 0;
}
:::