PIN Codes
题目描述:
A PIN code is a string that consists of exactly 44 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 nn (2≤n≤102≤n≤10) bank cards, the PIN code of the ii-th card is pipi.
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 nn codes would become different.
Formally, in one step, Polycarp picks ii-th card (1≤i≤n1≤i≤n), then in its PIN code pipi selects one position (from 11 to 44), 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 tt (1≤t≤1001≤t≤100) — the number of test cases in the input. Then test cases follow.
The first line of each of tt test sets contains a single integer nn (2≤n≤102≤n≤10) — the number of Polycarp’s bank cards. The next nn lines contain the PIN codes p1,p2,…,pnp1,p2,…,pn — one per line. The length of each of them is 44. All PIN codes consist of digits only.
Output:
Print the answers to tt test sets. The answer to each set should consist of a n+1n+1 lines
In the first line print kk — the least number of changes to make all PIN codes different. In the next nn 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.
Sample Input:
3
2
1234
0600
2
1337
1337
4
3139
3139
3139
3139
Sample Output:
0
1234
0600
1
1337
1237
3
3139
3138
3939
6139
题目大意:
这道题要求输入不同的四位pin码(任何数字,包括开头为0也能接受),要
求通过利用改变(可以通过增减以及替换来改变数字)最少次数达到pin码
每一个都不同就行了,并且输入最少改变数以及改变后的pin码。
思路分析:
一开始因为看这道题和八数码很像,就往哈希去考虑了,但是发现这道题的
输入居然不超过10个,所以这道题只要在他们个位做一些处理就行了,完全
不用队列和哈希去做,并且判断最小次数,只需要判断有多少个相同就行了
。
代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<map>
#include<string>
#include<queue>
using namespace std;
int main()
{
string s1[15];//输入的字符串
int t,b;
cin >> t;//样例
while(t--)
{
map<string,int> visit;//这里用映射代替,觉得不理解可以用二维数组,
scanf("%d",&b);
for(int i=0;i<b;i++)
{
cin >> s1[i];
visit[s1[i]]++;//数目多少
}
int num=0;
for(int i=0;i<b;i++)
{
if(visit[s1[i]]==1)//如果数目为1,说明没有相同
{
continue;
}
else
{
num++;//改变次数
visit[s1[i]]--;//需要
for(int j=0;j<=9;j++)
{
s1[i][0]='0'+j;
if(visit[s1[i]]==0)//这里是改变后的位置是否被先前所输入字符串所占
{
visit[s1[i]]++;
break;
}
}
}
}
cout << num << endl;//输出改变次数与改变后字符串
for(int i=0;i<b;i++)
{
cout << s1[i] <<endl;
}
}
}