前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >L1-064 估值一亿的AI核心代码

L1-064 估值一亿的AI核心代码

作者头像
喜欢ctrl的cxk
发布2020-02-14 17:44:11
1.4K0
发布2020-02-14 17:44:11
举报
AI.jpg
AI.jpg

以上图片来自新浪微博。

本题要求你实现一个稍微更值钱一点的 AI 英文问答程序,规则是:

  • 无论用户说什么,首先把对方说的话在一行中原样打印出来;
  • 消除原文中多余空格:把相邻单词间的多个空格换成 1 个空格,把行首尾的空格全部删掉,把标点符号前面的空格删掉;
  • 把原文中所有大写英文字母变成小写,除了 I
  • 把原文中所有独立的 can youcould you 对应地换成 I canI could—— 这里“独立”是指被空格或标点符号分隔开的单词;
  • 把原文中所有独立的 Ime 换成 you
  • 把原文中所有的问号 ? 换成惊叹号 !
  • 在一行中输出替换后的句子作为 AI 的回答。

输入描述:

输入首先在第一行给出不超过 10 的正整数 N,随后 N 行,每行给出一句不超过 1000 个字符的、以回车结尾的用户的对话,对话为非空字符串,仅包括字母、数字、空格、可见的半角标点符号。

输出描述:

按题面要求输出,每个 AI 的回答前要加上 AI: 和一个空格。

输入样例:

6
Hello ?
 Good to chat   with you
can   you speak Chinese?
Really?
Could you show me 5
What Is this prime? I,don 't know

输出样例:

Hello ?
AI: hello!
 Good to chat   with you
AI: good to chat with you
can   you speak Chinese?
AI: I can speak chinese!
Really?
AI: really!
Could you show me 5
AI: I could show you 5
What Is this prime? I,don 't know
AI: what Is this prime! you,don't know

解题思路:

这是去年天梯赛L1级别的最后一题,当时硬做没有写出来。这题用C++的regex库来求解会比较简单,正则表达式还是牛逼嗷。替换的时候需要注意.和?前需要加 \,而 \ 又是转义字符,所以得在.和?前加 \\,\b是用于判断是否为独立的单词,\s是用来判断是否为空白符,它们前面的\都需要再加一个\进行转义。我用了regex求解之后测试样例没有问题,提交之后有个3分的测试点过不了,找了很久的bug之后发现必须得先把can you和could you替换成I can和I could后再替换I和me,而且替换过后的I can和I could里面的I不能够在 I 换成you的时候再次进行替换。所以我先把can you和could you替换成了_I can和_I could,等到I和me都替换成you之后再把_I替换回 I 。

17分代码:

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

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    int n;
    cin >> n;
    cin.ignore();
    while(n--)
    {
        string str;
        getline(cin,str);
        cout << str << endl;   //输出用户的原话
        cout << "AI: ";
        str = regex_replace(str,regex("\\s+")," "); //删除连续的多余空格
        str = regex_replace(str,regex(" ,"),",");  //删除,前的空格
        str = regex_replace(str,regex(" \\."),"."); //删除.前的空格
        str = regex_replace(str,regex(" !"),"!");  //删除!前的空格
        str = regex_replace(str,regex(" \\?"),"?");  //删除?前的空格
        str = regex_replace(str,regex(" '"),"'");  //删除'前的空格
        if(str.front()==' ') str.erase(str.begin());  //删除字符串行首的空格
        if(str.back()==' ') str.pop_back();  //删除字符串行末的空格
        for(auto &it : str)
        {
            //把所有除I外的大写字母转小写
            if(isupper(it) && it!='I')
            {
                it = tolower(it);
            }
        }
        str = regex_replace(str,regex("\\?"),"!"); //替换所有的?为!
        str = regex_replace(str,regex("\\bI\\b"),"you"); //替换所有的独立的I为you
        str = regex_replace(str,regex("\\bme\\b"),"you"); //替换所有的独立的me为you
        str = regex_replace(str,regex("\\bcan you\\b"),"I can"); //替换所有的can you为I can
        str = regex_replace(str,regex("\\bcould you\\b"),"I could"); //替换所有的could you为I could
        cout << str << endl;
    }
    return 0;
}

AC代码:

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

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    int n;
    cin >> n;
    cin.ignore();
    while(n--)
    {
        string str;
        getline(cin,str);
        cout << str << endl;   //输出用户的原话
        str = regex_replace(str,regex("\\s+")," "); //删除连续的多余空格
        str = regex_replace(str,regex(" ,"),",");  //删除,前的空格
        str = regex_replace(str,regex(" \\."),"."); //删除.前的空格
        str = regex_replace(str,regex(" !"),"!");  //删除!前的空格
        str = regex_replace(str,regex(" \\?"),"?");  //删除?前的空格
        str = regex_replace(str,regex(" '"),"'");  //删除'前的空格
        if(str.front()==' ') str.erase(str.begin());  //删除字符串行首的空格
        if(str.back()==' ') str.pop_back();  //删除字符串行末的空格
        for(auto &it : str)
        {
            //把所有除I外的大写字母转小写
            if(isupper(it) && it!='I')
            {
                it = tolower(it);
            }
        }
        str = regex_replace(str,regex("\\?"),"!"); //替换所有的?为!
        str = regex_replace(str,regex("\\bcan you\\b"),"_I can"); //替换所有的can you为_I can
        str = regex_replace(str,regex("\\bcould you\\b"),"_I could"); //替换所有的could you为_I could
        str = regex_replace(str,regex("\\bI\\b"),"you"); //替换所有的独立的I为you
        str = regex_replace(str,regex("\\bme\\b"),"you"); //替换所有的独立的me为you
        str = regex_replace(str,regex("\\b_I\\b"),"I");  //为了避免重复替换而设置的_I换回I
        //必须先换can you/could you再换I和mem,不然有一个测试点一直卡3分
        cout << "AI: " << str << endl;
    }
    return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 输入描述:
  • 输出描述:
  • 输入样例:
  • 输出样例:
  • 解题思路:
  • 17分代码:
  • AC代码:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档