题目

A PIN code is a string that consists of exactly 444 digits. Examples of possible PIN codes: 701370137013, 000000000000 and 099009900990. Please note that the PIN code can begin with any digit, even with 000.

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

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 nnn codes would become different.

Formally, in one step, Polycarp picks iii-th card (1≤i≤n)(1≤i≤n)(1≤i≤n), then in its PIN code pip_ipi​ selects one position (from 111 to 444), 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?

输入

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

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

输出

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

In the first line print kkk — the least number of changes to make all PIN codes different. In the next nnn 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.

题目大意

ttt组数据,每组给定nnn个可能有重复的四位数,每次变换可以改变一个数字的任意一位,求最少多少次变换后没有相同的数字。

思路

一开始是想记录前三位相同的数字可用(没有已有数字填充)的个数,然后再贪心,然后换前2位……

后来发现n<=10n<=10n<=10的致命条件。

于是难度跳崖下降。我们每个数字肯定最多只需要变换一次就能保证不重复(0-9有10个位置)。

开一个map记录当前位置有多少个数,然后找到当前位置数个数>1>1>1就使ans+=mp[now]−1ans += mp[now] - 1ans+=mp[now]−1即可统计出变换次数。

那么求变换后序列呢?因为每个数肯定都可以通过改变其中一位来保证不重复,因此直接枚举改变一位,如果改变后的位置无数则变过去就可以了。

复杂度O(nt)O(nt)O(nt)

代码

#include <cstdio>
#include <iostream>
#include <string>
#include <map>
using namespace std;
int t, n, ans;
map<string, int> ht;
string all[11];
int main()
{
cin.tie(0);
ios::sync_with_stdio(false);
cin >> t;
while (t--)
{
ans = 0;
ht.clear();
cin >> n;
for (int i = 1; i <= n; ++i)
cin >> all[i], ++ht[all[i]];
for (map<string, int>::iterator it = ht.begin(); it != ht.end(); ++it)
ans += it->second - 1;
cout << ans << endl;//直接先统计出答案,每组都要且只要进行n-1次变换
for (int i = 1; i <= n; ++i)
{
map<string, int>::iterator it = ht.find(all[i]);
if (it == ht.end())
continue;
if (it->second > 1)
{
string s = all[i];
for (int j = 0; j <= 9; ++j)
{
s[3] = j + '0';
if (ht.find(s) == ht.end())
{
ht[s] = 1;
cout << s << endl;
--it->second;//这里只进行1次变换,因为每个输入的数据都会遍历到,所以最后一定会减到1
break;
}
}
}
else
cout << all[i] << endl;
}
}
return 0;
}

最新文章

  1. [linux basic基础]----套接字
  2. mongodb的查询方式与sql语句对比
  3. zoj 3620 Escape Time II
  4. BT5 firefox Flash插件问题
  5. 现代JVM内存管理方法的发展历程,GC的实现及相关设计概述(转)
  6. 从基于idea的第一个javaweb项目到shell脚本项目自动发布(jdk1.8,mysql5.7,maven3.5,tomcat9,subversion,centos7.3)之一
  7. python使用sqlmap API检测SQL注入
  8. 排错:Windows系统异常导致Filebeat无法正常运行
  9. golang中使用mysql数据库
  10. git添加删除远程tag
  11. 局域网内yum源搭建
  12. Python request 和response 初使用
  13. C# 锁系列目录
  14. AI LeNet
  15. java interview
  16. 如何确定Isilon cluster的网卡类型是40GbE的还是10GbE的
  17. 【转载】JVM系列二:GC策略&amp;内存申请、对象衰老
  18. 【WPF】帐号系统中,用户注册的校验逻辑(正则表达式)
  19. 异常信息ASM ClassReader failed to parse class file的问题解决
  20. 实现KbmMw web server 支持https

热门文章

  1. 计算机基础- 序列化(Serialization)和持久化(Persistence)的区别
  2. 标签UILabel的讲解
  3. 碰到的问题——建设基于TensorFlow的深度学习环境
  4. Set和Map集合的比较
  5. Java - 实现双向链表
  6. JAVA--文件内容属性替换
  7. Spark教程——(4)Spark-shell调用SQLContext(HiveContext)
  8. DICOM设备Raw Data与重建
  9. vue移动端transition兼容
  10. django的save方法被调用时数据是如何被保存的