PAT甲级刷题记录——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(Lin lowercase), or 0(zero) from O(oin uppercase). One solution is to replace 1(one) by @, 0(zero) by %, lby L, and Oby 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 modifiedwhere Nis the total number of accounts. However, if Nis one, you must print There is 1 account and no account is modifiedinstead.

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

思路

这题也是一题很简单的字符串处理题哈~每次读入一个字符串,就对它进行处理,如果碰到1、0、l、O这四个字符,就先把flag置为true(说明这个密码有误),然后把按照题意把这四个字符进行替换处理。在对一个字符串遍历完之后,如果flag是true,就说明这是个问题密码,且已经按题意修复,那么就它放进一个数组里(我这里用了map,string映射到string,即:账户->密码),如果flag依然是false,说明这个密码是无误的,于是cnt++(记录正确密码的数量)。

最后,只要检查map的size()就知道有几个错误的密码了:如果size()==0,说明输入的密码都是正确的,这里还要再分情况讨论,如果正确密码的数量是1个,那么输出There is……如果正确密码的数量不止1个,那么输出There are……。

如果size()不为0,就说明输入的数据中有错误的密码,然后需要按照输入的顺序输出有错误的密码,但是因为map的key是自动按从小到大排序的,但是我们输出的顺序是要求和读入顺序一样的,因此,我这里用了一个vector数组来存放读入的顺序,这样,只要遍历vector数组,看看其值在map里的length()是否为0(因为map里key是string,value也是string,所以可以判某个账户的value长度是否为0来判断是否存储过这个账户的信息)。

如果不为0,说明这个账户的密码是错误的,然后直接输出,如果length()为0,就说明根本没有存这个账户的信息,说明这个账户的密码是正确的,就跳到下一轮循环继续检查。

代码

#include<cstdio>
#include<stdlib.h>
#include<algorithm>
#include<vector>
#include<map>
#include<string.h>
#include<iostream>
using namespace std;
map<string, string> m;
vector<string> data;
int main()
{
    int N;
    cin>>N;
    int cnt = 0;
    for(int i=0;i<N;i++){
        bool flag = false;
        string tmpAccount, tmpPassword;
        cin>>tmpAccount>>tmpPassword;
        data.push_back(tmpAccount);//保存输入顺序
        for(int i=0;i<tmpPassword.length();i++){
            if(tmpPassword[i]=='1'){
                tmpPassword[i] = '@';
                flag = true;
            }
            else if(tmpPassword[i]=='0'){
                tmpPassword[i] = '%';
                flag = true;
            }
            else if(tmpPassword[i]=='l'){
                tmpPassword[i] = 'L';
                flag = true;
            }
            else if(tmpPassword[i]=='O'){
                tmpPassword[i] = 'o';
                flag = true;
            }
        }
        if(flag==true) m[tmpAccount] = tmpPassword;
        else cnt++;//说明这个密码无需修正
    }
    if(m.size()==0){
        if(cnt==1) cout<<"There is 1 account and no account is modified";
        else cout<<"There are "<<cnt<<" accounts and no account is modified";
    }
    else{
        cout<<m.size()<<'\n';
        for(int i=0;i<data.size();i++){
            if(m[data[i]].length()!=0) cout<<data[i]<<" "<<m[data[i]]<<'\n';
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值