This question requires you to implement a slightly more valuable AI Question and Answer program. The rules are:
- No matter what the user says, first print the words of the other party in one line as they are.
- Eliminate the redundant spaces in the original text: replace the multiple spaces between adjacent words with one space, delete all the spaces at the beginning and end of the line, and delete the spaces in front of punctuation marks;
- Turn all capitalized English letters into lowercase except I.
- Replace all the independent words can you and can you in the original text with I can and I can, where "independence" refers to words separated by spaces or punctuation marks.
- Replace all independent I and me with you in the original text.
- Change all the question marks in the original text into exclamation marks!
- Output the replaced sentence in one line as the answer of AI.
Input format:
Input begins with a positive integer N of no more than 10 on the first line, followed by a dialog of no more than 1000 characters per line ending with carriage return. The dialog is a non-empty string consisting only of letters, numbers, spaces and visible half-corner punctuation symbols.
Output format:
Each AI answer should be preceded by an AI: and a space.
Input sample:
6 Hello ? Good to chat with you can you speak Chinese? Really? Could you show me 5 What Is this prime? I,don 't know
Output sample:
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
Author: Chen Yue
SETTING: Zhejiang University
Time limit: 400 ms
Memory limit: 64 MB
Thought: None
If you haven't checked this blog, you may know what's wrong. https://blog.csdn.net/dengkuomin/article/details/89053402
#include<iostream> #include<cstdio> #include<algorithm> #include<cmath> #include<string> #include<cstring> #include<map> #include<vector> #include<set> #include<list> #include<stack> #include<queue> #include<sstream> using namespace std; typedef long long ll; string s; bool judge(char c){ if(c>='a'&&c<='z') return false; if(c=='I') return false; if(c>='0'&&c<='9') return false; return true; } bool judge(string s){ if(s[0]>='a'&&s[0]<='z') return false; if(s[0]=='I') return false; if(s[0]>='0'&&s[0]<='9') return false; return true; } bool book[1005]; void solve(string s){ memset(book,0,sizeof(book)); int cnt=0; //Case and case for(int i=0;i<s.size();i++){ if(s[i]>='A'&&s[i]<='Z'&&s[i]!='I'){ s[i]=(s[i]-'A'+'a'); } } for(int i=0;i<s.size();i++){//Judging whether a symbol follows a space or not, true does not follow if(s[i]!=' '&&judge(s[i])) { cnt++; if(i+1<s.size()&&s[i+1]!=' ')book[cnt]=true; if(i==s.size()-1) book[cnt]=true; } } vector<string> v; string str=""; for(int i=0;i<s.size();i++){ if(s[i]!=' '){ if(judge(s[i])){ if(str!=""){ v.push_back(str); str=""; } str=str+s[i]; v.push_back(str); str=""; } else{ str=str+s[i]; } } else{ if(str!=""){ v.push_back(str); str=""; } } } if(str!="") v.push_back(str); str=""; if(v.size()==0){ cout<<"AI: "<<endl; return ; } for(int i=0;i<v.size();){ if(v[i]=="I"||v[i]=="me") { v[i]="you"; i++; } else if(i+1<v.size()&&v[i]=="could"&&v[i+1]=="you"){ v[i]="I"; v[i+1]="could"; i+=2; } else if(i+1<v.size()&&v[i]=="can"&&v[i+1]=="you"){ v[i]="I"; v[i+1]="can"; i+=2; } else i++; } cnt=0; str=v[0]; if(judge(v[0])){ ++cnt; if(!book[cnt]) str=str+" "; } for(int i=1;i<v.size();i++){ if(judge(v[i-1])){ if(judge(v[i])){ cnt++; if(!book[cnt]){ if(i+1<v.size()&&judge(v[i+1])) str=str+v[i]; else str=str+v[i]+" "; } else str=str+v[i]; } else str=str+v[i]; } else{ if(judge(v[i])){ cnt++; if(!book[cnt]) { if(i+1<v.size()&&judge(v[i+1])) str=str+v[i]; else str=str+v[i]+" "; } else str=str+v[i]; } else str=str+" "+v[i]; } } for(int i=0;i<str.size();i++){ if(str[i]=='?') str[i]='!'; } cout<<"AI: "<<str<<endl; } int main(){ int T; scanf("%d",&T); getchar(); while(T--){ getline(cin,s); cout<<s<<endl; solve(s); } return 0; }