题意:
思路:
· 个人思路是先处理原文串a所有空格和大小写变成串b
· 再处理串b中的各种替换问题,由于在处理单词替换时虽然先考虑的“I”的替换,但是之后再不会变量到该单词,所以并未出现大家所说的测试点2(can you)的卡顿。
· 但是特别注意这个题的另外一个坑点,本人就是一直测试点1和测试点4格式错误,后面通过这篇博客的详细讲解才通过了本题。
· 测试点1:结尾没有标点符号而有多个空格
· 测试点4:三个空格,要求输出"AI: "即可。
AC代码:
#include<bits/stdc++.h>
#define int long long
//#define endl '\n'
using namespace std;
const int N = 1e5+10;
int n;
string a, b, c;
bool check(int x, int sz, int len)
{
if((x==0||b[x-1]==' '||ispunct(b[x-1]))&&(x+(sz-1)==len||b[x+sz]==' '||ispunct(b[x+sz]))) return 1;
return 0;
}
signed main()
{
cin >> n;
getchar();
while(n--){
a.clear(); b.clear(); c.clear();
getline(cin, a);
cout << a << endl;
int len = a.size()-1;
for(int i = 0; i <= len; i ++){
if(a[i]==' '){
// cout << "i: " << i << endl;
if(!b.size()||i==len) continue;
int ok = 0;
for(int j = i+1; j <= len; j ++){
if(a[j]==' ') continue;
else if(i==len){
i = j-1;
ok = 1;
break;
}
else if(ispunct(a[j])){
i = j;
ok = 1;
if(a[j]=='?') b += '!';
else b += a[j];
break;
}
else{
ok = 1;
if(b.size()) b += ' ';
i = j-1;
break;
}
}
}
else if(isupper(a[i])&&a[i]!='I') b += tolower(a[i]);
else if(a[i]=='?') b += '!';
else b += a[i];
}
// cout << "size: " << b.size() << endl;
// cout << b << endl;
len = b.size()-1;
for(int i = 0; i <= len; i ++){
// cout << "i: " << i << endl;
if(b[i]=='I'&&check(i, 1, len)){
c += "you";
continue;
}
if(b[i]=='m'&&b[i+1]=='e'&&check(i, 2, len)){
c += "you";
i++;
continue;
}
if(i+6<=len){
string tmp = b.substr(i, 7);
if(tmp=="can you"&&check(i, 7, len)){
c += "I can";
i += 6;
continue;
}
}
if(i+8<=len){
string tmp = b.substr(i, 9);
if(tmp=="could you"&&check(i, 9, len)){
c += "I could";
i += 8;
continue;
}
}
c += b[i];
}
// cout << "size: " << c.size() << "c:" << c << "endl" << endl;
cout << "AI: " << c << endl;
}
return 0;
}
/*
Hello ?
AI: hello!
Good to chat with you
AI: good to chat with you
can you speak Chinese?
AI: I can speak chinese!
Really?
AI: really!
Could you show me 5
AI: I could show you 5
What Is this prime? I,don 't know
AI: what Is this prime! you,don't know
*/