PIN Codes CodeForces - 1263B

A PIN code is a string that consists of exactly 4 digits. Examples of possible PIN codes: 7013, 0000 and 0990. Please note that the PIN code can begin with any digit, even with 0.

Polycarp has n (2≤n≤10) bank cards, the PIN code of the i-th card is pi.

Polycarp has recently read a recommendation that it is better to set different PIN codes on different cards. Thus he wants to change the minimal number of digits in the PIN codes of his cards so that all n codes would become different.

Formally, in one step, Polycarp picks i-th card (1≤i≤n), then in its PIN code pi selects one position (from 1 to 4), and changes the digit in this position to any other. He needs to change the minimum number of digits so that all PIN codes become different.

Polycarp quickly solved this problem. Can you solve it?

Input
The first line contains integer t (1≤t≤100) — the number of test cases in the input. Then test cases follow.

The first line of each of t test sets contains a single integer n (2≤n≤10) — the number of Polycarp’s bank cards. The next n lines contain the PIN codes p1,p2,…,pn — one per line. The length of each of them is 4. All PIN codes consist of digits only.

Output
Print the answers to t test sets. The answer to each set should consist of a n+1 lines

In the first line print k — the least number of changes to make all PIN codes different. In the next n lines output the changed PIN codes in the order corresponding to their appearance in the input. If there are several optimal answers, print any of them.

Example
Input
3
2
1234
0600
2
1337
1337
4
3139
3139
3139
3139
Output
0
1234
0600
1
1337
1237
3
3139
3138
3939
6139

题意:
PIN码指的是一个4位数字的编码。你有N个PIN码,每次你可以操作改变一个PIN码的一位数字,要求最少的操作次数使得所有PIN码都不同。
思路:
枚举修改哪一位改成什么数字,改成的那个数字没有出现过就可以了。

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

int p[10] = {0,1,10,100,1000};

int b[10005],vis[10005];
int n;

int modify(int x,int y,int z)//x的第y位改为z.
{
    int num = x / p[y] % 10;
    x -= p[y] * num;
    x += p[y] * z;
    return x;
}

int solve()
{
    int ans = 0;
    for(int i = 1;i <= n;i++)
    {
        int flag = 0;
        if(vis[b[i]] > 1)
        {
            for(int j = 1;j <= 4;j++)
            {
                for(int k = 0;k <= 9;k++)
                {
                    int tmp = modify(b[i],j,k);
                    if(vis[tmp]==0)
                    {
                        ans++;
                        vis[tmp]++;
                        vis[b[i]]--;
                        b[i] = tmp;
                        flag = 1;
                        break;
                    }
                }
                if(flag)break;
            }
        }
    }
    return ans;
}

int main()
{
    int T;scanf("%d",&T);
    while(T--)
    {
        memset(vis,0,sizeof(vis));
        scanf("%d",&n);
        for(int i = 1;i <= n;i++)
        {
            scanf("%d",&b[i]);
            vis[b[i]]++;
        }
        int ans = solve();
        printf("%d\n",ans);
        for(int i = 1;i <= n;i++)printf("%04d\n",b[i]);
    }
    return 0;
}
发布了676 篇原创文章 · 获赞 18 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/tomjobs/article/details/104124827