这道题代码我写了170行,还发生了一些错误,题目理解起来并不难,难在处理字符串很复杂。
做题思路
第一步:先把简单的问题解决,例如换?大小写这部分可以写在前面。
第二步:删掉开头的空格,删掉结尾的空格。
第三步:寻找标点符号前面的空格,这里考察了一个转义字符的应用,例如 '\' 。
第四步:找me和I,都换成you。
第五步:找can you ,找 could you。
我按照以上的步骤写出来,最终结果才10分,望大佬指教。
#include<bits/stdc++.h>
using namespace std;
typedef struct Node{
int x;
int y;
}Node;
int main(){
int n;
cin>>n;
getchar();
while(n--){
string str;
getline(cin,str);
cout<<str<<endl;
string temp[1000]={""};
for(int i=0;i<str.length();i++){
if(str[i]=='?'){
str[i]='!';
}
if(str[i]<='Z'&&str[i]>='A'&&str[i]!='I'){
str[i]+=32;
}
}
int k=0;
string tm;
for(int i=0;i<str.length();i++){
if(k==0&&str[i]==' ') continue;
else{
k = i;
break;
}
}
tm = str.substr(k);
k=0;
for(int i=tm.length()-1;i>=0;i--){
if(k==0&&str[i]==' ') continue;
else{
k = i;
break;
}
}
tm = tm.substr(0,k+1);
vector<Node> v;
int wei=-1;
for(int i=0;i<tm.length();i++){
if(tm[i]<='z'&&tm[i]>='a') wei=i;
if(tm[i]==','||tm[i]=='\''||tm[i]=='!'||tm[i]=='?'){
if(wei!=-1){
v.push_back({wei,i});
wei=-1;
}
}
}
for(int i=0;i<v.size();i++){
int u1 = v[i].x;
int u2 = v[i].y;
tm.erase(tm.begin()+u1+1,tm.begin()+u2);
}
wei=-1;
v.clear();
int chuk=-1;
for(int i=0;i<tm.length();i++){
if(tm[i]<='z'&&tm[i]>='a'||(tm[i]==','||tm[i]=='\''||tm[i]=='!'||tm[i]=='?')){
if(wei!=-1&&chuk!=-1){
v.push_back({wei,chuk});
wei=-1;
chuk = -1;
}
wei=i;
}
if(tm[i]==' '){
chuk = i;
}
}
for(int i=0;i<v.size();i++){
int u1 = v[i].x;
int u2 = v[i].y;
tm.erase(tm.begin()+u1+1,tm.begin()+u2);
// cout<<u1<<" "<<u2<<endl;
}
vector<int> jiai;
v.clear();
// wei = tm.find("me");
for(int i=0;i<tm.length()-1;i++){
if(tm[i]=='m'&&tm[i+1]=='e'){
v.push_back({i,i+1});
}
}
for(int i=0;i<v.size();i++){
wei = v[i].x;
if(wei==0&&(tm[wei+2]>'z'||tm[wei+2]<'a')){
string uu = tm.substr(0,wei);
string uu2 = tm.substr(wei+2);
tm = uu+"you";
tm = tm+uu2;
}
else if(wei>0&&(tm[wei-1]>'z'||tm[wei-1]<'a')&&(tm[wei+2]>'z'||tm[wei+2]<'a')){
string uu = tm.substr(0,wei);
string uu2 = tm.substr(wei+2);
tm = uu+"you";
tm = tm+uu2;
}
}
jiai.clear();
for(int i=0;i<tm.length();i++){
if(tm[i]=='I'){
jiai.push_back(i);
}
}
for(int i=0;i<jiai.size();i++){
wei = jiai[i];
if(wei==0&&(tm[wei+1]>'z'||tm[wei+1]<'a')){
string uu = tm.substr(0,wei);
string uu2 = tm.substr(wei+1);
tm = uu+"you";
tm = tm+uu2;
}
else if(wei>0&&(tm[wei-1]>'z'||tm[wei-1]<'a')&&(tm[wei+1]>'z'||tm[wei+1]<'a')){
string uu = tm.substr(0,wei);
string uu2 = tm.substr(wei+1);
tm = uu+"you";
tm = tm+uu2;
}
}
jiai.clear();
for(int i=0;i<tm.length();i++){
if(tm[i]=='c'&&tm[i+1]=='a'&&tm[i+2]=='n'&&tm[i+3]==' '&&tm[i+4]=='y'&&tm[i+5]=='o'&&tm[i+6]=='u'){
jiai.push_back(i);
}
}
for(int i=0;i<jiai.size();i++){
wei = jiai[i];
// wei = tm.find("can you");
if(wei==0){
string uu = tm.substr(0,wei);
string uu2 = tm.substr(wei+7);
tm = uu+"I can";
tm = tm+uu2;
}
else if(tm[wei-1]==' '){
tm.erase(tm.begin()+wei,tm.begin()+wei+7);
}
}
jiai.clear();
for(int i=0;i<tm.length();i++){
if(tm[i]=='c'&&tm[i+1]=='o'&&tm[i+2]=='u'&&tm[i+3]=='l'&&tm[i+4]=='d'&&tm[i+5]==' '&&tm[i+6]=='y'&&tm[i+7]=='o'&&tm[i+8]=='u'){
jiai.push_back(i);
}
}
// wei = tm.find("could you");
for(int i=0;i<jiai.size();i++){
wei = jiai[i];
if(wei==0){
string uu = tm.substr(0,wei);
string uu2 = tm.substr(wei+9);
tm = uu+"I could";
tm = tm+uu2;
}
else if(tm[wei-1]==' '){
tm.erase(tm.begin()+wei,tm.begin()+wei+9);
}
}
cout<<"AI: "<<tm<<endl;
}
return 0;
}