PIN Codes CodeForces - 1263B

本文介绍了一种解决银行卡片PIN码差异最小化问题的算法,通过修改最少次数使所有PIN码变得不同。核心思想是在遇到重复PIN码时,通过改变任意一位数字来创建唯一码。文章提供了一个C++实现示例,展示了如何使用哈希映射来跟踪PIN码出现的频率,并有效地找到未使用的数字进行替换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、内容

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 ncodes 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 ttest sets. The answer to each set should consist of a n+1linesIn the first line print k— the least number of changes to make all PIN codes different. In the next nlines 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.

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

二、思路

  • 由于只有10个数字,所以只需要随便修改一位上面的数字就可以让n个数字不同。

三、代码

#include <cstdio>
#include <map>
#include <cstring>
#include <iostream>
using namespace std;
int t, n;
string str[15];
int main() {
	scanf("%d", &t);
	while (t--) {
		map<string, int> mp;
		scanf("%d", &n);
		for (int i = 1; i <= n; i++) {
			cin >> str[i];
			mp[str[i]]++;
		}
		int ans = 0;
		for (int i = 1; i <= n; i++) {
			if (mp[str[i]] == 1) continue;
			ans++;
			mp[str[i]]--;
			//替换字符
			for (int j = 0; j <= 9; j++) {
				str[i][0] = '0' + j;
				if (mp[str[i]] == 0) {
					mp[str[i]]++;
					break;
				}
			} 
		}
		cout << ans << endl; 
		for (int i = 1; i <= n; i++) {
			cout << str[i] << endl;
		}
	} 
	return 0;
}
### Codeforces Problem 976C Solution in Python For solving problem 976C on Codeforces using Python, efficiency becomes a critical factor due to strict time limits aimed at distinguishing between efficient and less efficient solutions[^1]. Given these constraints, it is advisable to focus on optimizing algorithms and choosing appropriate data structures. The provided code snippet offers insight into handling string manipulation problems efficiently by customizing comparison logic for sorting elements based on specific criteria[^2]. However, for addressing problem 976C specifically, which involves determining the winner ('A' or 'B') based on frequency counts within given inputs, one can adapt similar principles of optimization but tailored towards counting occurrences directly as shown below: ```python from collections import Counter def determine_winner(): for _ in range(int(input())): count_map = Counter(input().strip()) result = "A" if count_map['A'] > count_map['B'] else "B" print(result) determine_winner() ``` This approach leverages `Counter` from Python’s built-in `collections` module to quickly tally up instances of 'A' versus 'B'. By iterating over multiple test cases through a loop defined by user input, this method ensures that comparisons are made accurately while maintaining performance standards required under tight computational resources[^3]. To further enhance execution speed when working with Python, consider submitting codes via platforms like PyPy instead of traditional interpreters whenever possible since they offer better runtime efficiencies especially important during competitive programming contests where milliseconds matter significantly.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值