PTA 估值一亿的AI核心代码
比赛时被模拟题打自闭了,本来以为是个比较麻烦的模拟,实际上只要会C++的regex不到40行就能把这个题过掉了(orz smz)
regex是用来处理正则表达式,里面有个函数regex_replace(string s, regex re, string new_string)可以将s中满足正则表达式re的所有位置替换成new_string。
代码:
#include <bits/stdc++.h> #include <regex> using namespace std; int main() { int n; scanf("%d", &n); getchar(); while(n--) { string s; getline(cin, s); cout << s << endl; s = regex_replace(s, regex(R"(\s+)"), " "); if(s.front() == ' ') s.erase(s.begin()); if(s.back() == ' ') s.pop_back(); s = regex_replace(s, regex(R"( !)"), "!"); s = regex_replace(s, regex(R"( ,)"), ","); s = regex_replace(s, regex(R"( \.)"), "."); s = regex_replace(s, regex(R"( \?)"), "?"); s = regex_replace(s, regex(R"( ')"), "'"); for (auto &c : s) { if(c != 'I') c = tolower(c); } 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"(\bI\b)"), "you"); s = regex_replace(s, regex(R"(\bme\b)"), "you"); s = regex_replace(s, regex(R"(\?)"), "!"); s = regex_replace(s, regex(R"(\b_I\b)"), "I"); cout << "AI: " << s << endl; } }