codeforces B. PIN Codes

这篇博客介绍了如何解答codeforces上的B. PIN Codes问题。博主提供了输入输出示例,并分享了解题思路,即利用map来查找并修改相同PIN码,确保所有卡片的PIN码变得不同。最后给出了正确代码实现。

原题链接: codeforces B. 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.

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

解题思路

在本题中,我采用了“map<string,int>searc;” 来构造键值对。 将原本是string类的a[i]变成了map的下标,很方便寻找相同的数字行。
但是我在用的过程中发现,要想修改a[i]行的任何一个数字,都要根据ASCII码进行修改,也就是要加48。
这样下来,该题也不是特别难了呢,嘻嘻。
ps:小伙伴们在用map的时候,千万别忘了map头文件哦~~~

正确代码

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string>
#include<cstring>
#include<map>

using namespace std;

#define ll long long;
string a[15];

int main()
{
    int  t;
    cin>>t;
    while(t--){
        int n;
        map<string,int>searc;  //构造键值对,map[a]=x,a是string类,x是int类;
        cin>>n;
        int num=0;
        for(int i=0;i<n;i++){
             cin>>a[i];
             searc[a[i]]++;
             if(searc[a[i]]>=2)//该行重复次数+1;
                 num+=1;
        }
        cout<<num<<endl;//总共重复的行数;
         for(int i=0;i<n;i++)
            if(searc[a[i]]>=2){
                searc[a[i]]--;
                for(int j=0;j<=9;j++){
                a[i][3]=j+48;   //因为最多10行,所以如果出现10个重复的,修改后,也不会在发生重复。
                if(searc[a[i]]==0) {
                    searc[a[i]]=1;break;
                }
            }
            }
         for(int i=0;i<n;i++){
            cout<<a[i]<<endl;
         }
    }
    return 0;
}
【源码免费下载链接】:https://renmaiwang.cn/s/2gdnj 《R语言数据挖掘方法及应用》由薛薇编写而成的一本系统阐述R语言在数据挖掘领域前沿技术的著作。该书旨在指导读者学会使用R语言进行高效、实用的数据分析与建模工作,涵盖了从理论基础到实践操作的全过程。作为一款功能强大且开源的统计计算和图形处理平台,R语言凭借其丰富的工具库和社区支持,在数据分析与可视化方面展现出显著优势。在数据挖掘领域,R语言提供了包括`caret`、`randomForest`、`tm`、`e1071`等广泛使用的专用包,这些工具能够帮助用户更便捷地进行数据预处理、特征选择、模型构建和结果评估。全书首先介绍R语言的基本知识体系,涵盖环境配置与安装方法、基础语法规范以及常见数据类型分析等内容。这些基础知识是开展后续数据分析工作的必备技能,通过学习可以快速掌握R语言的核心功能。随后章节深入讲解了数据挖掘的主要概念与流程,包括数据清洗、转换整理和探索性分析等环节,同时详细阐述了分类、聚类、关联规则挖掘及预测等多种典型任务的具体实施方法。这些内容有助于读者全面理解数据挖掘的整体架构及其核心工作步骤。在应用实践部分,薛薇老师结合真实案例展示了R语言在实际业务场景中的具体运用,例如市场细分分析、客户流失预测以及个性化推荐系统等。通过这些案例研究,读者可以深入学习如何利用相关工具包解决实际问题,并提升数据分析能力。此外,书中配套的“案例数据集”和“代码资源”为读者提供了实践操作的机会,使理论知识能够更好地转化为动手技能。通过实际操作分析,读者可以加深对R语言数据挖掘方法的理解并灵活运用。总之,《R语言数据挖掘方法及应用》是一部全面讲解R语言在数据分析与建模领域的教材,无论你是刚开始学习的新人还是经验丰富的专业人士,都能从中获益匪浅。通过深入研读此书,你可以掌握R语言的数据挖掘技巧,并将其应用到实
### Codeforces Div.2 比赛难度介绍 Codeforces Div.2 比赛主要面向的是具有基础编程技能到中级水平的选手。这类比赛通常吸引了大量来自全球不同背景的参赛者,包括大学生、高中生以及一些专业人士。 #### 参加资格 为了参加 Div.2 比赛,选手的评级应不超过 2099 分[^1]。这意味着该级别的竞赛适合那些已经掌握了一定算法知识并能熟练运用至少一种编程语言的人群参与挑战。 #### 题目设置 每场 Div.2 比赛一般会提供五至七道题目,在某些特殊情况下可能会更多或更少。这些题目按照预计解决难度递增排列: - **简单题(A, B 类型)**: 主要测试基本的数据结构操作和常见算法的应用能力;例如数组处理、字符串匹配等。 - **中等偏难题(C, D 类型)**: 开始涉及较为复杂的逻辑推理能力和特定领域内的高级技巧;比如图论中的最短路径计算或是动态规划入门应用实例。 - **高难度题(E及以上类型)**: 对于这些问题,则更加侧重考察深入理解复杂概念的能力,并能够灵活组合多种方法来解决问题;这往往需要较强的创造力与丰富的实践经验支持。 对于新手来说,建议先专注于理解和练习前几类较容易的问题,随着经验积累和技术提升再逐步尝试更高层次的任务。 ```cpp // 示例代码展示如何判断一个数是否为偶数 #include <iostream> using namespace std; bool is_even(int num){ return num % 2 == 0; } int main(){ int number = 4; // 测试数据 if(is_even(number)){ cout << "The given number is even."; }else{ cout << "The given number is odd."; } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值