Codeforces Round #603 (Div. 2) B. PIN Codes(字符串)

题目链接:https://codeforces.com/contest/1263/problem/B

题意

给出 $n$ 个四位数字,每次操作可以改变一个数的一个数位,问至少要操作多少次使得所有数不同。($2 \le n \le 10$)

题解

$n$ 较小,记录重复的数,枚举每一位的每一个数字,变为没有出现过的数即可。

代码

#include <bits/stdc++.h>
using namespace std;

void solve() {
    int n; cin >> n;
    string ss[n];
    map<string, int> cnt;
    for (auto &s : ss) cin >> s, ++cnt[s];
    int ans = 0;
    for (auto &s : ss) {
        if (cnt[s] >= 2) {
            for (int i = 0; i < 4; i++) {
                for (char c = '0'; c <= '9'; c++) {
                    string t = s;
                    t[i] = c;
                    if (cnt.count(t) == 0) {
                        --cnt[s];
                        ++cnt[t];
                        s = t;
                        ++ans;
                        goto ok;
                    }
                }
            }
            ok:;
        }
    }
    cout << ans << "\n";
    for (auto s : ss) cout << s << "\n";
}

int main() {
    int t; cin >> t;
    while (t--) solve();
}

 

posted @ 2020-06-30 23:20  Kanoon  阅读(188)  评论(0)    收藏  举报