[code] PTA胡凡算法笔记 DAY016

本文深入探讨了使用C++处理字符串的三个经典问题,包括密码字符替换、公共后缀查找及中文数字读取,提供了详尽的代码示例与解析。

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

题目A1035 Password

在这里插入图片描述

  • 题意
    将输入密码中存在题目需要替换字符的字符替换,并且按要求根据有无替换等情况输出。

  • 思路
    建立替换表,然后利用结构体存储输入数据,设置bool属性表示是否修改。是否需要修改单独利用一个函数去判断,并且需要替换的时候就直接替换。最后根据题目格式输出即可。
    注意:当没有替换的时候需要注意输出语句的单复数情况。

  • Code in C++

#include <iostream>
#include <map>
#include <string>

#define maxn 1001
// 修改转换表
std::map<char, char> trans = {{'1', '@'},
                              {'0', '%'},
                              {'l', 'L'},
                              {'O', 'o'},};

struct user {
    std::string username;
    std::string password;
    bool modified = false;  // 标识密码是否修改
} input[maxn];

// 判断是否需要修改,需要时修改
bool isModified(std::string &str) {
    bool flag = false;
    for (int i = 0; i < str.size(); ++i) {
        if (str[i] == '1' || str[i] == '0' || str[i] == 'l' || str[i] == 'O') {
            flag = true;
            str[i] = trans[str[i]];
        }
    }

    return flag;
}

int main()
{
    int N;
    int count = 0;
    std::cin >> N;

    for (int i = 0; i < N; ++i) {
        std::cin >> input[i].username >> input[i].password;
        if (isModified(input[i].password)) {
            ++count;
            input[i].modified = true;
        }
    }

    // 输出判断
    if (count == 0) {
        std::cout << "There " << (N == 1 ? "is " : "are ") << N << (N == 1 ? " account" : " accounts")  << " and no account is modified" ;
    } else {
        std::cout << count;
        for (int i = 0; i < N; ++i) {
            if (input[i].modified) {
                std::cout << std::endl;
                std::cout << input[i].username << " " << input[i].password;
            }
        }
    }

    return 0;
}
 

题目A1077 Kuchiguse

在这里插入图片描述

  • 题意
    求公共后缀,没有时输出nai

  • 思路
    从后向前扫描,用两个字符串存储即可,每次求两个字符串的后缀,不断迭代。
    注意: 这里要注意输入数字之和的回车符是否会被存到后面的字符串中。

  • Code in C++

#include <iostream>
#include <string>

std::string getSuffix(const std::string &pre, const std::string &cur) {

    int plenth = pre.size();
    int clenth = cur.size();

    // 为空或者一开始就不同直接返回
    if (plenth == 0 || clenth == 0 || pre[plenth-1] != cur[clenth - 1])
        return "";
    int i = 0;
    for ( ; i < plenth && i < clenth; ++i) {
        if (pre[plenth - 1 - i] != cur[clenth - 1 - i]) {
            break;
        }
    }

    return cur.substr(clenth - i, i);
}

int main()
{
    int N;
    char tmp;
    std::string pre, cur;
    std::cin >> N;
    std::cin.get(tmp);  // 去除换行符
    getline(std::cin, pre);
    for (int i = 1; i < N; ++i) {
        getline(std::cin, cur);
        pre = getSuffix(pre, cur);
    }

    if (pre.size() != 0) {
        std::cout << pre;
    } else {
        std::cout << "nai";
    }
    return 0;
}


题目A1082 Read Number in Chinese

在这里插入图片描述

  • 题意
    用中文读数,表示方式看题目例子。

  • 思路
    按人的习惯都是一小节一小节读的,这里也采用这种方式去处理。有几条处理规则:① 零要积累读,不能遇到一个零就读出来。 ② 当这一小节都是零的时候不用输出这一节的单位(比如万) ③ 当这一小节只要有数输出就需要输出这一小节的单位。所以在处理每一小节的时候需要设置变量去记录,积累0和这一小节是否有输出数字的情况。

测试用例:
80000000 // ba Qian Wan
80008000 // ba Qian Wan ba Qian
800000000 // ba Yi
808080808 // ba Yi ling ba Bai ling ba Wan ling ba Bai ling ba
-880808080 // Fu ba Yi ba Qian ling ba Shi Wan ba Qian ling ba Shi

  • Code in C++
#include <iostream>
#include <string>

char units[][5] = {"Shi", "Bai", "Qian", "Wan", "Yi"};
char number[][5] = {"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"};

int main()
{
    std::string num;
    std::cin >> num;

    int lenth = num.size();
    int left = 0, right = lenth - 1;

    if (num[0] == '-') {
        std::cout << "Fu";
        ++left;
    }

    // 将left与right定位到同一小节
    while (left + 4 <= right) {
        right -= 4;
    }

    // 遍历每个小节
    for (; left < lenth; right += 4) {
        bool flag = false;    // 是否含有积累的0
        bool isPrint = false; // 是否输出该节的任意数
        for (; left <= right; ++left) {
            if (left > 0 && num[left] == '0') {
                flag = true;
            } else {
                if (flag == true) {
                    std::cout << " " << number[0];
                    flag = false;
                }

                // 不是首位,需要输出空格
                if (left > 0) std::cout << " ";
                std::cout << number[num[left] - '0'];
                isPrint = true;
                if (left != right) {
                    std::cout << " " << units[right - left - 1];
                }
            }
        }
        // 不是个位就要输出万或亿
        if (isPrint && right != lenth - 1) {
            std::cout << " " << units[(lenth - 1 - right) / 4 + 2];
        }
    }

    return 0;
}


小结

字符串的读入方式:cin.get(char ch) 读字符; getline(cin, string) 读字符串。 两个都可以读空格。

PS:字符串小节刷完了,最大的感觉就是字符串的输入操作和输出格式很捉急。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值