codeforces B. PIN Codes

原题链接: 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;
}
发布了36 篇原创文章 · 获赞 1 · 访问量 1414

猜你喜欢

转载自blog.csdn.net/atnanajiang/article/details/103446066