本题要求你实现一个简易版的 AI 英文问答程序,规则是:
- 无论用户说什么,首先把对方说的话在一行中原样打印出来;
- 消除原文中多余空格:把相邻单词间的多个空格换成 1 个空格,把行首尾的空格全部删掉,把标点符号前面的空格删掉;
- 把原文中所有大写英文字母变成小写,除了 I;
- 把原文中所有独立的 I 和 me 换成 you;
- 把原文中所有的问号 ? 换成惊叹号 !;
- 把原文中所有独立的 can you 换成 I can —— 这里“独立”是指被空格或标点符号分隔开的单词;
- 在一行中输出替换后的句子作为 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: could you show you 5
What Is this prime? I,don 't know
AI: what Is this prime! you,don't know
#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;
}
————————————————
版权声明:本文为CSDN博主「Iovebecky」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/zhaohaibo_/article/details/88982158