题目
1035 Password (20分)
To prepare for PAT, the judge sometimes has to generate random passwords for the users. The problem is that there are always some confusing passwords since it is hard to distinguish 1
(one) from l
(L
in lowercase), or 0
(zero) from O
(o
in uppercase). One solution is to replace 1
(one) by @
, 0
(zero) by %
, l
by L
, and O
by o
. Now it is your job to write a program to check the accounts generated by the judge, and to help the juge modify the confusing passwords.
Input Specification:
Each input file contains one test case. Each case contains a positive integer N (≤1000), followed by N lines of accounts. Each account consists of a user name and a password, both are strings of no more than 10 characters with no space.
Output Specification:
For each test case, first print the number M of accounts that have been modified, then print in the following M lines the modified accounts info, that is, the user names and the corresponding modified passwords. The accounts must be printed in the same order as they are read in. If no account is modified, print in one line There are N accounts and no account is modified
where N
is the total number of accounts. However, if N
is one, you must print There is 1 account and no account is modified
instead.
Sample Input 1:
3
Team000002 Rlsp0dfa
Team000003 perfectpwd
Team000001 R1spOdfa
Sample Output 1:
2
Team000002 RLsp%dfa
Team000001 R@spodfa
Sample Input 2:
1
team110 abcdefg332
Sample Output 2:
There is 1 account and no account is modified
Sample Input 3:
2
team110 abcdefg222
team220 abcdefg333
Sample Output 3:
There are 2 accounts and no account is modified
题目大意
给出N个id和密码,有些密码需要规范化,规范对规则为:1->@ 0->% l->L O->o;
输出需要规范对密码条数,并输出对应的id和规范后的密码。
思路
方法一
每次输入密码字符串时就遍历,发现需要规范的字符则修改并加入新的字符串中,最后新的字符串作为规范后的密码输出;
方法二
使用正则匹配,然后替换对应的字母
代码
#include<bits/stdc++.h>
using namespace std;
struct person{
string id,pwd;
};
int main(int argc, const char * argv[]) {
int N, M=0;
cin>>N;
person p;
bool flag = false;
string rep;
vector<person> v;
unordered_map<char, char> modify={{'1','@'}, {'0','%'}, {'l','L'}, {'O', 'o'}};
for(int i=0; i<N; i++){
cin>>p.id>>p.pwd;
rep = "";
flag = false;
for(int j=0; j<p.pwd.size(); j++){
if(modify.count(p.pwd[j])){
rep += modify[p.pwd[j]];
flag = true;
}
else rep += p.pwd[j];
}
if(flag) {
M++;
p.pwd = rep;
v.push_back(p);
}
}
if(M == 0) {
if(N>1)
printf("There are %d accounts and no account is modified", N);
else printf("There is 1 account and no account is modified");
}
else{
printf("%d\n", M);
for(auto it : v)
printf("%s %s\n", it.id.c_str(), it.pwd.c_str());
}
return 0;
}
// 正则
//#include <iostream>
//#include <bits/stdc++.h>
//using namespace std;
//struct node{
// string id,passwd;
//};
//int main(int argc, const char * argv[]) {
// int n,sum = 0;
// cin>>n;
// string s1,s2;
// regex r1("1"), r0("0"),rl("l"),rO("O");
// vector<node> v;
// for(int i=0; i<n; i++){
// cin>>s1>>s2;
// if(regex_search(s2,r1) || regex_search(s2,r0) || regex_search(s2,rl) || regex_search(s2,rO)){
// sum ++;
// s2 = regex_replace(s2 ,regex("1"), "@");
// s2 = regex_replace(s2 ,regex("0"), "%");
// s2 = regex_replace(s2 ,regex("l"), "L");
// s2 = regex_replace(s2 ,regex("O"), "o");
// v.push_back({s1, s2});
// }
// }
// if(sum != 0 ){
// cout<<sum<<endl;
// for(int i=0; i<v.size();i++)
// cout<<v[i].id<<' '<<v[i].passwd<<endl;
// }else{
// if(n > 1)
// printf("There are %d accounts and no account is modified", n);
// else
// printf("There is 1 account and no account is modified");
// }
//
// return 0;
//}