参考自: http://www.manongjc.com/detail/28-gsdqaxrxcgnqqlv.html
有改动。
个人认为已经是这道题最好的解法了。
注意点为测试点四:(以标点符号打头的时候别忽略掉前面有空格)
输入:
1
,dac
输出
,dec
AI: ,dec
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
cin.get();
string s;
while (n--) {
getline(cin, s);
cout << s << '\n';
//找到其中第一个空格的位置
string res = "";
int i = 0;
while (i < s.size() && s[i] == ' ') i++;
for (; i < s.size(); i++) {
if (s[i] == ' ' && i + 1 < s.size() && isalnum(s[i + 1])) {
res += s[i];//只有下面是字母的时候才会选择保留空格
}
if (s[i] != ' ') {
if (!isalnum(s[i])) res += ' ';//符号前面加上空格
res += s[i];
}
}
//将?转化为!
for (int i = 0; i < res.size(); i++) {
if (res[i] == '?') res[i] = '!';
if (res[i] >= 'A' && res[i] <= 'Z' && res[i] != 'I') {
res[i] = 'a' + (res[i] - 'A');
}
}
vector<string> vec;
stringstream is;
is << res;
string t;
while (is >> t) {
vec.push_back(t);
}
cout << "AI:";
if(vec.size()==0 || !isalnum(vec[0][0])) cout << " ";
for (int i = 0; i < vec.size(); i++) {
if (!isalnum(vec[i][0])) {//如果是标点符号
cout << vec[i];
}
else if (vec[i] == "can" && i + 1 < vec.size() && vec[i + 1] == "you") {
cout << " I can";
i++;
}
else if (vec[i] == "could" && i + 1 < vec.size() && vec[i + 1] == "you") {
cout << " I could";
i++;
}
else if (vec[i] == "me" || vec[i] == "I") {
cout << " you";
}
else {
cout << " " << vec[i];
}
}
is.clear();
is.str("");
cout << '\n';
}
return 0;
}
标签:20,cout,AI,cin,int,vec,064
From: https://www.cnblogs.com/chengyiyuki/p/18080412