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

#include <bits/stdc++.h>
using namespace std;
using LL = long long;
void solve();
int main() {
// 	ios::sync_with_stdio(false);
// 	cin.tie(nullptr);
	int T = 1; cin >> T;

	getchar();
	while(T -- ) solve();	
	return 0;
}
void solve() {
	string s; getline(cin, s);
	int n = s.size(); string res;
	cout << s << "\nAI:";
	//deal blank
	int i = 0; while(i < n && s[i] == ' ') i ++;
	for(; i < n; i ++ ) {
		if(s[i] == ' ' && i + 1 < n && isalnum(s[i + 1]))  res += s[i];
		if(s[i] != ' ') {
			if(!isalnum(s[i])) res += ' ';
			assert(i < n);
			res += s[i];
		}
	}
	
	s = res;
	n = s.size();
	
	//tolower   and  '?' to '!'
	for(char &c: s) if(isalpha(c) && c != 'I') c = tolower(c); else if(c == '?') c = '!';
	
	vector<string> str;
	res.clear();
	
	
	
	for(i = 0; i < n; i ++ ) {
		if(i == n - 1 || s[i] == ' ') {	
			if(s[i] != ' ') res.push_back(s[i]);
			if(!res.empty()) str.push_back(res);
			res.clear();
			if(i == n - 1) break;
		} else {
			res.push_back(s[i]);
		}
	} 
	
	int m = str.size();
	if (str.empty() || !isalnum(str[0][0])) cout << " ";
	
	for(i = 0; i < m; i ++ ) {
		if(!isalnum(str[i][0])) {
			assert(i < m);
			cout << str[i];
		} else if (str[i] == "can" && i + 1 < m && str[i + 1] == "you") {
			cout << " I can"; i ++;
		} else if (str[i] == "could" && i + 1 < m && str[i + 1] == "you") {
			cout << " I could"; i  ++;
		} else if(str[i] == "I" || str[i] == "me") {
			cout << " you";
		} else {
			assert(i < m);
			cout << " " << str[i];
		}
	}
	cout << "\n";
	
}
posted @ 2022-03-14 18:55  ccz9729  阅读(123)  评论(0编辑  收藏  举报