ac代码:
#include<bits/stdc++.h>
#define f(i,a,b) for(int i=a;i<b;i++)
#define ff(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
int n;
string con(string s){
while(s[0]==' ')s.erase(s.begin());
while(s[s.length()-1]==' ')s.erase(s.end()-1);
for(int i=0;i<s.length();i++){
if(s[i]==' '){
while(i+1<s.length() && s[i+1]==' ')s.erase(s.begin()+i+1);
if(i+1<s.length() && !isalnum(s[i+1]))s.erase(s.begin()+i);
}
}
f(i,0,s.length()){
if(s[i]>='A'&&s[i]<='Z'&&s[i]!='I')
s[i]+=32;
}
for(int pos=0;;pos++){
pos=s.find("can you",pos);
if(pos==-1) break;
if((!pos || !isalnum(s[pos-1])) && (pos+7==s.size() || !isalnum(s[pos+7]))){
s.replace(pos,7,"A can");
}
}
for(int pos=0;;pos++){
pos=s.find("could you",pos);
if(pos==-1) break;
if((!pos || !isalnum(s[pos-1])) && (pos+9==s.size() || !isalnum(s[pos+9]))){
s.replace(pos,9,"A could");
}
}
for(int pos=0;;pos++){
pos=s.find("I",pos);
if(pos==-1) break;
if((!pos || !isalnum(s[pos-1])) && (pos+1==s.size() || !isalnum(s[pos+1]))){
s.replace(pos,1,"you");
}
}
for(int pos=0;;pos++){
pos=s.find("me",pos);
if(pos==-1) break;
if((!pos || !isalnum(s[pos-1])) && (pos+2==s.size() || !isalnum(s[pos+2]))){
s.replace(pos,2,"you");
}
}
f(i,0,s.size()){
if(s[i]=='?')s[i]='!';
else if(s[i]=='A')s[i]='I';
}
return s;
}
int main(){
cin>>n;
getchar();
string s;
f(line,0,n){
getline(cin,s);
cout<<s<<endl;
cout<<"AI: "<<con(s)<<endl;
}
}
ac代码(标记坑点):
#include<bits/stdc++.h>
#define f(i,a,b) for(int i=a;i<b;i++)
#define ff(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
int n;
string con(string s){
//行首尾空格
while(s[0]==' ')s.erase(s.begin());
while(s[s.length()-1]==' ')s.erase(s.end()-1);
// 错误示范:(会在第4个测试点段错误)
// for(int i=0;i<s.length()-1;i++){
// if(s[i]==' '){
// //相邻单词间的多个空格换成 1 个空格
// while(s[i+1]==' ')s.erase(s.begin()+i+1);
// //标点符号前面的空格,不能用while,会段错误
// if(!isalnum(s[i+1]))s.erase(s.begin()+i);
// }
// }
for(int i=0;i<s.length();i++){
if(s[i]==' '){
//相邻单词间的多个空格换成 1 个空格
while(i+1<s.length() && s[i+1]==' ')s.erase(s.begin()+i+1);
//标点符号前面的空格,不能用while,会段错误
if(i+1<s.length() && !isalnum(s[i+1]))s.erase(s.begin()+i);
}
}
f(i,0,s.length()){
if(s[i]>='A'&&s[i]<='Z'&&s[i]!='I')
// 错误示范:
// s[i]-=26;
s[i]+=32;
}
// 错误示范(会超时):
// for(int pos=0;;){
for(int pos=0;;pos++){
pos=s.find("can you",pos);
if(pos==-1) break;
// 错误示范:
// if((!pos || !isalnum(s[pos-1])) && (pos+7!=s.size() || !isalnum(s[pos+7])))
if((!pos || !isalnum(s[pos-1])) && (pos+7==s.size() || !isalnum(s[pos+7]))){
s.replace(pos,7,"A can");
}
}
for(int pos=0;;pos++){
pos=s.find("could you",pos);
if(pos==-1) break;
if((!pos || !isalnum(s[pos-1])) && (pos+9==s.size() || !isalnum(s[pos+9]))){
s.replace(pos,9,"A could");
}
}
for(int pos=0;;pos++){
pos=s.find("I",pos);
if(pos==-1) break;
if((!pos || !isalnum(s[pos-1])) && (pos+1==s.size() || !isalnum(s[pos+1]))){
// 错误示范:
// s.replace(pos,9,"you");
s.replace(pos,1,"you");
}
}
for(int pos=0;;pos++){
pos=s.find("me",pos);
if(pos==-1) break;
if((!pos || !isalnum(s[pos-1])) && (pos+2==s.size() || !isalnum(s[pos+2]))){
s.replace(pos,2,"you");
}
}
f(i,0,s.size()){
if(s[i]=='?')s[i]='!';
else if(s[i]=='A')s[i]='I';
}
return s;
}
int main(){
cin>>n;
getchar();
string s;
f(line,0,n){
getline(cin,s);
cout<<s<<endl;
cout<<"AI: "<<con(s)<<endl;
}
}