比赛时被模拟题打自闭了,本来以为是个比较麻烦的模拟,实际上只要会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"(can you)"), "_I can"); s = regex_replace(s, regex(R"(could you)"), "_I could"); s = regex_replace(s, regex(R"(I)"), "you"); s = regex_replace(s, regex(R"(me)"), "you"); s = regex_replace(s, regex(R"(?)"), "!"); s = regex_replace(s, regex(R"(_I)"), "I"); cout << "AI: " << s << endl; } }