https://pintia.cn/problem-sets/994805046380707840/problems/1111914599412858885
题解:
/*
*@Author: STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
#define endl "\n"
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=100000+10;
const int M=100000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k,p,l,r,u,v;
int ans,cnt,flag,temp,sum;
int a[N];
char str[N];
char str1[N];
const char st1[20]="can you";
const char st2[20]="could you";
struct node{};
int main()
{
#ifdef DEBUG
freopen("input.in", "r", stdin);
//freopen("output.out", "w", stdout);
#endif
//ios::sync_with_stdio(false);
//cin.tie(0);
//cout.tie(0);
scanf("%d",&t);
getchar();
while(t--){
memset(str,' ',sizeof(str));
memset(str1,' ',sizeof(str1));
cin.getline(str+1,100000,'\n');
int len=strlen(str+1);
if(len==0){
cout<<endl;
cout<<"AI: "<<endl;
continue;
}
cnt=1;
int pos=1;
str1[0]=str[0]=' ';
while(str[pos]==' ')
pos++;
for(int i=pos;i<=len;i++){
if(str[i-1]==' '&&str[i]==' ')
continue;
if(str[i]=='!'||str[i]=='?'||str[i]==','||str[i]=='.'||str[i]=='\''){
if(str1[cnt-1]==' '&&cnt!=1)cnt--;
str1[cnt++]=str[i];
continue;
}//
if('A'<=str[i]&&str[i]<='Z'&&str[i]!='I'){
str1[cnt++]=str[i]+32;
}else{
str1[cnt++]=str[i];
}
}
if(str1[cnt-1]==' '&&cnt!=1)
cnt--;
str1[cnt]='\0';
cout<<str+1<<endl;
cout<<"AI: ";
for(int i=1;i<cnt;i++){
if(str1[i]=='?'){
cout<<'!';
}else if(str1[i]=='I'&&!isalpha(str1[i-1])&&!isalpha(str1[i+1])){
cout<<"you";
}else if(str1[i]=='m'&&i+2<=cnt&&str1[i+1]=='e'&&!isalpha(str1[i+2])&&str1[i-1]==' '){
cout<<"you";
i++;
}else if(str1[i]=='c'&&!isalpha(str1[i-1])){
if(i+7>cnt){
cout<<str1[i];
continue;
}
int f=1;
for(int j=0;j<7;j++){
if(str1[i+j]!=st1[j]){
f=0;
break;
}
}
if(f&&!isalpha(str1[i+7])){
cout<<"I can";
i+=6;
continue;
}
if(i+9>cnt){
cout<<str1[i];
continue;
}
f=1;
for(int j=0;j<9;j++){
if(str1[i+j]!=st2[j]){
f=0;
break;
}
}
if(f&&!isalpha(str1[i+9])){
cout<<"I could";
i+=8;
continue;
}
cout<<str1[i];
}else{
cout<<str1[i];
}
}
cout<<endl;
}
#ifdef DEBUG
printf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC);
#endif
//cout << "Hello world!" << endl;
return 0;
}