[Codeforces]1263B PIN Code
题目
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;
}
[Codeforces]1263B PIN Code的更多相关文章
- [Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路)
[Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路) 题面 有n个空心物品,每个物品有外部体积\(out_i\)和内部体积\(in_i\),如果\(in_i& ...
- CodeForces - 965E Short Code
Discription Arkady's code contains nn variables. Each variable has a unique name consisting of lower ...
- Codeforces 543A Writing Code
http://codeforces.com/problemset/problem/543/A 题目大意:n个人,一共要写m行程序,每个程序员每行出现的bug数为ai,要求整个程序出现的bug数不超过b ...
- CodeForces 543A - Writing Code DP 完全背包
有n个程序,这n个程序运作产生m行代码,但是每个程序产生的BUG总和不能超过b, 给出每个程序产生的代码,每行会产生ai个BUG,问在总BUG不超过b的情况下, 我们有几种选择方法思路:看懂了题意之后 ...
- Codeforces 768B B. Code For 1
参考自:https://www.cnblogs.com/ECJTUACM-873284962/p/6423483.html B. Code For 1 time limit per test:2 se ...
- Codeforces 965E Short Code 启发式合并 (看题解)
Short Code 我的想法是建出字典树, 然后让后面节点最多的点优先向上移到不能移为止, 然后gg. 正确做法是对于当前的节点如果没有被占, 那么从它的子树中选出一个深度最大的点换到当前位置. 用 ...
- 【codeforces 768B】Code For 1
[题目链接]:http://codeforces.com/contest/768/problem/B [题意] 一开始给你一个数字n; 让你用这个数字n根据一定的规则生成序列; (如果新生成的序列里面 ...
- 【codeforces 765B】Code obfuscation
[题目链接]:http://codeforces.com/contest/765/problem/B [题意] 让你把每个变量都依次替换成a,b,c,-.d这些字母; 且要按顺序先用a再用b-.c.d ...
- CodeForces 1129C Morse Code
洛谷题目页面传送门 & CodeForces题目页面传送门 题意见洛谷里的翻译. 首先我们可以用区间DP算出对于每个子01串,能表示的字母串的个数. 设\(dp_{i,j}\)表示长度为\(i ...
随机推荐
- python 可视化 pyecharts
github搜索pyecharts https://github.com/pyecharts/pyecharts echarts : https://www.echartsjs.com/zh/inde ...
- 标签UILabel的讲解
首先,我先自定义几个名词,方便接下来的讲解工作.如下图所示: 接下来,通过五个方面来讲解我们能对UILabel做出哪些改变或者称之为设置: 1.文字 1.1普通文字:内容text.字体大小font.字 ...
- 机器学习之SVM多分类
实验要求数据说明 :数据集data4train.mat是一个2*150的矩阵,代表了150个样本,每个样本具有两维特征,其类标在truelabel.mat文件中,trainning sample 图展 ...
- kali安装vm tools正确操作
参考博文:https://blog.csdn.net/qq_39536876/article/details/79501471 前言:每次在执行完 ./vmware-install.pl 重启后,总是 ...
- Servlet 学习(六)
会话 1.定义 一般意义会话:指两人以上的对话(多用于学习别种语言或方言时) 计算机中的会话:客户端和服务器的通讯 web客户端 A ------>Tomcat web客户端 B ------& ...
- 【剑指Offer面试编程题】题目1360:乐透之猜数游戏--九度OJ
题目描述: 六一儿童节到了,YZ买了很多丰厚的礼品,准备奖励给JOBDU里辛劳的员工.为了增添一点趣味性,他还准备了一些不同类型的骰子,打算以掷骰子猜数字的方式发放奖品.例如,有的骰子有6个点数(点数 ...
- 各种STL的基本用法
目录 STL及一些常用函数的基本用法 1.vector(向量)的基本用法 2.queue(队列)的基本用法 3.stack(栈)的基本操作 4.set(集合)的基本用法 5.map(映射)的基本用法 ...
- vue-cli 手脚架mock虚拟数据的运用,特别是坑!!!
1.现在基本的趋势就是前后分离,前后分离就意味着当后台接口还没完成之前,前端是没有接口可以拿来调用的 ,那么mock虚拟数据就很好的解决了这一问题,前端可以直接模拟真实的数据AJAX请求! 运用 步骤 ...
- PostgreSQL存取jsonb
从PostgreSQL 9.3开始,json就成了postgres里的一种数据类型,也就是和varchar.int一样,我们表里的一个字段的类型可以为json了. 与此同时,postgres还提供了j ...
- 一道快速考察 Python 基础的面试题
这是前一阵子群友发在群里的一道面试题,利用 Python 字典的特性,可以巧妙地使用精简代码达成完美解. 题目 将 data 转换成 new_data 这种形式,写出转换过程. data = { 'a ...