1. 程式人生 > >PTA 估值一億的AI核心代碼

PTA 估值一億的AI核心代碼

repl sca orz bsp ack () can 函數 begin

題面

比賽時被模擬題打自閉了,本來以為是個比較麻煩的模擬,實際上只要會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);
	while(n--) {
		cout << s << endl;
		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;	
	}
} 

  

PTA 估值一億的AI核心代碼