L1-064 估值一亿的AI核心代码——按照规则逐一击破

这篇博客介绍了一个AI核心代码的实现过程,包括消除字符串中的多余空格,将大写字母转为小写(除了'I'),以及将特定词汇替换为其他词汇(如 'canyou' -> 'Ican', 'I' -> 'you')。同时,它还将问号转换为惊叹号,展示了C++代码实现这些功能的详细步骤。

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

一、题目

估值一亿的AI核心代码

二、分析

1、消除原文中多余空格:把相邻单词间的多个空格换成 1 个空格,把行首尾的空格全部删掉,把标点符号前面的空格删掉;

判断是否为符号

bool If(char op) //判断op是否为符号
{
    if (op == '0')
        return false;
    if (op >= 'a' && op <= 'z')
        return false;
    if (op >= 'A' && op <= 'Z')
        return false;
    if (op >= '0' && op <= '9')
        return false;
    return true;
}
        for (int i = 0; i < str.size(); i++)
        {
            if (str[i] == ' ')
            {
                int j = i;
                while (j + 1 < str.size() && str[j + 1] == ' ')
                    j++;
                str.erase(i, j - i);
                i = j;
            }
        }
        if (str.size() != 0 && str[0] == ' ')
            str.erase(0, 1);
        if (str.size() != 0 && str[str.size() - 1] == ' ')
            str.erase(str.size() - 1, 1);
        for (int i = 1; i < str.size(); i++)
        {
            if (If(str[i]) && str[i - 1] == ' ')
            {
                str.erase(i - 1, 1);
                i--;
            }
        }

2、把原文中所有大写英文字母变成小写,除了 I

        for (int i = 0; i < str.size(); i++)
        {
            if (str[i] >= 'A' && str[i] <= 'Z' && str[i] != 'I')
                str[i] += 'a' - 'A';
        }

3、把原文中所有独立的 can youcould you 对应地换成 I canI could—— 这里“独立”是指被空格或标点符号分隔开的单词;把原文中所有独立的 I 和 me 换成 you

这里有一个小问题是把"can you"替换成"I can"后,进行"I"->"you"时又重新改变了。

解决办法:在第一步时先用‘A’替换'I',全部完成后再替换成'I'

思路来源:L1-064 估值一亿的AI核心代码 (20分)(极短代码)_给个选择的博客-优快云博客

string replace(string str, string ch, string op)
{
    for (int i = 0;; i++)
    {
        int l = str.find(ch, i), r = l + ch.size() - 1;
        if (l == -1)
            break;
        if (l == 0 && (str[r + 1] == ' ' || If(str[r + 1]))) //位于最前面
        {
            str.replace(l, r - l + 1, op);
        }
        else if (r == str.size() - 1 && (str[l - 1] == ' ' || If(str[l - 1]))) //位于最后
        {
            str.replace(l, r - l + 1, op);
        }
        else if ((str[r + 1] == ' ' || If(str[r + 1])) && (str[l - 1] == ' ' || If(str[l - 1]))) //位于中间
        {
            str.replace(l, r - l + 1, op);
        }
    }
    return str;
}



        str = replace(str, "can you", "A can");
        str = replace(str, "could you", "A could");
        str = replace(str, "I", "you");
        str = replace(str, "me", "you");

4、把原文中所有的问号 ? 换成惊叹号 !

        for (int i = 0; i < str.size(); i++)
        {
            if (str[i] == '?')
                str[i] = '!';
            if (str[i] == 'A')
                str[i] = 'I';
        }

三、代码

#include <bits/stdc++.h>
using namespace std;

bool If(char op) //判断op是否为符号
{
    if (op == '0')
        return false;
    if (op >= 'a' && op <= 'z')
        return false;
    if (op >= 'A' && op <= 'Z')
        return false;
    if (op >= '0' && op <= '9')
        return false;
    return true;
}

string replace(string str, string ch, string op)
{
    for (int i = 0;; i++)
    {
        int l = str.find(ch, i), r = l + ch.size() - 1;
        if (l == -1)
            break;
        if (l == 0 && (str[r + 1] == ' ' || If(str[r + 1]))) //位于最前面
        {
            str.replace(l, r - l + 1, op);
        }
        else if (r == str.size() - 1 && (str[l - 1] == ' ' || If(str[l - 1]))) //位于最后
        {
            str.replace(l, r - l + 1, op);
        }
        else if ((str[r + 1] == ' ' || If(str[r + 1])) && (str[l - 1] == ' ' || If(str[l - 1]))) //位于中间
        {
            str.replace(l, r - l + 1, op);
        }
    }
    return str;
}

int main()
{
    int n;
    cin >> n;
    getchar();
    while (n--)
    {
        string str;
        getline(cin, str);
        cout << str << endl;

        for (int i = 0; i < str.size(); i++)
        {
            if (str[i] == ' ')
            {
                int j = i;
                while (j + 1 < str.size() && str[j + 1] == ' ')
                    j++;
                str.erase(i, j - i);
                i = j;
            }
        }
        if (str.size() != 0 && str[0] == ' ')
            str.erase(0, 1);
        if (str.size() != 0 && str[str.size() - 1] == ' ')
            str.erase(str.size() - 1, 1);
        for (int i = 1; i < str.size(); i++)
        {
            if (If(str[i]) && str[i - 1] == ' ')
            {
                str.erase(i - 1, 1);
                i--;
            }
        }

        for (int i = 0; i < str.size(); i++)
        {
            if (str[i] >= 'A' && str[i] <= 'Z' && str[i] != 'I')
                str[i] += 'a' - 'A';
        }

        str = replace(str, "can you", "A can");
        str = replace(str, "could you", "A could");
        str = replace(str, "I", "you");
        str = replace(str, "me", "you");

        for (int i = 0; i < str.size(); i++)
        {
            if (str[i] == '?')
                str[i] = '!';
            if (str[i] == 'A')
                str[i] = 'I';
        }
        cout << "AI: " << str << endl;
    }
    return 0;
}

### PTA L1-064 题目解析与Python解法 #### 解题思路 对于PTA L1-064这类问题,核心在于理解题目要求的操作逻辑。具体来说,是从给定的字符串`A`中移除所有出现在字符串`B`里的字符[^2]。 为了高效处理这一需求,可以采用如下策略: - **初始化映射表**:创建一个用于记录字符串`B`中各字符存在的哈希表(或集合),以便快速查询某个字符是否属于要删除的目标集。 - **遍历过滤**:逐一遍历字符串`A`中的每一个字符,利用上述建立好的哈希表检查当前字符是否存在于`B`内;如果不存在,则保留该字符作为最终结果的一部分。 这种方法能够有效减少不必要的重复比较操作,提高算法效率。 #### 代码实现 以下是基于以上思路编写的Python函数示例: ```python def string_subtraction(A, B): # 创建一个set存储需要被移除的字符 remove_chars = set(B) # 使用列表推导式筛选出不在remove_chars中的字符,并重新组合成新的字符串返回 result = ''.join([char for char in A if char not in remove_chars]) return result if __name__ == "__main__": # 测试用例模拟输入输出流程 test_A = input().strip() # 获取第一个字符串A test_B = input().strip() # 获取第二个字符串B output = string_subtraction(test_A, test_B) print(output) ``` 这段代码首先定义了一个名为`string_subtraction`的功能函数,它接收两个参数——即待处理的大串`A`以及其中需剔除的小串`B`。接着,在主程序部分实现了简单的交互界面,允许用户通过标准输入提供具体的测试案例来进行验证。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值