天梯赛 L1-064 估值一亿的AI核心代码 (20 分) stringstream+正则表达式

天梯赛 L1-064 估值一亿的AI核心代码 (20 分)


题目

天梯赛 L1-064 估值一亿的AI核心代码 (20 分) stringstream+正则表达式_第1张图片
本题要求你实现一个稍微更值钱一点的 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

简单分析:

就是一个模拟题,按着步骤来就是了。我看网上的代码太长、太复杂,就借鉴了下大佬的代码,参考链接在最底部。

代码:

stringstream,有详细注解:

#include 
#include 

using namespace std;

int n;
string s;

int main() {
	cin >> n;
	getchar();
	while (n--) {
		getline(cin, s);
		cout << s << endl; // 1 输出原话
		cout << "AI:";
		for (int i = 0; i < s.size(); i++) {
			if (isupper(s[i]) && s[i] != 'I') s[i] = tolower(s[i]);
			// 3 除了I,将其余字符全部转换为小写
			if (s[i] == '?') s[i] = '!'; //5 ?换为!
			if (!isalnum(s[i])) { // 对每个非字母和数字(空格 问号 逗号 其他的符号)之前加空格
				s.insert(i, " ");
				i++;
			}
		}
		string str[1010]; // 体中最大长度1000
		int cnt = 0; // 统计除了空格,有多少个单词
		stringstream ss(s);//使用字符串流,先忽略s中的所有空格
		while (ss >> s)
			str[cnt++] = s; // 将所有除了空格的符号读入str中
		if (!isalnum(str[0][0])) cout << " "; // isalnum(大小写字母+数字)
		// 如果第一个单词的第一个位置是标点,那么要输出一个空格,题中要求回答前要加上AI:和一个空格
		for (int i = 0; i < cnt; i++) {
			if (!isalnum(str[i][0])) cout << str[i];
			// 断第一个字符是否为标点,标点前面不能输出空格
			else if (str[i] == "can" && str[i + 1] == "you") {
				cout << " I can";
				i++; // 如果满足,i会移动两次,刚好移动到i+2
			}
			else if (str[i] == "could" && str[i + 1] == "you") {
				cout << " I could";
				i++;
			}
			else if (str[i] == "I" || str[i] == "me")
				cout << " you";
			else cout << " " << str[i]; // 如果是其他的字母 ,补全中间的空格
		}
		cout << endl;
	}
	return 0;
}

正则表达式,我也看不太懂= =,诸君自学成才吧:

#include 
#include 

using namespace std;

int n;
string s;

int main() {
    cin >> n;
    getchar();
    while (n --) {
        getline(cin, s);
        cout << s << '\n' << "AI: ";
        s = regex_replace(s, regex(R"(\s+)"), " ");
        s = regex_replace(s, regex(R"(^\s+|\s+$|\s+(?=\W))"), "");
        s = regex_replace(s, regex(R"(\bI\b)"), "mark_mark");
        for (int i = 0; i < (int)s.size(); i++) if (s[i] != 'I') s[i] = tolower(s[i]);
        s = regex_replace(s, regex(R"(\bcan you\b)"), "I can");
        s = regex_replace(s, regex(R"(\bcould you\b)"), "I could");
        s = regex_replace(s, regex(R"(mark_mark|\bme\b)"), "you");
        s = regex_replace(s, regex(R"(\?)"), "!");
        cout << s << '\n';
    }
    return 0;
}

感悟

L1的题就是基本语法题。一定要好好努力啊!!!4.23比赛,加油!

参考链接

正则表达式
stringstream

你可能感兴趣的:(天梯赛,c++)