This question requires you to implement a slightly more valuable AI English question and answer program. The rules are:
- No matter what the user says, first print out what the other party said in one line;
- Eliminate extra spaces in the original text: replace multiple spaces between adjacent words with 1 space, delete all spaces at the beginning and end of the line, and delete spaces before punctuation;
- Change all uppercase English letters in the original text to lowercase, except
I
; - All the original separate
can you
,could you
correspondingly replacedI can
,I could
- where "independent" means that the word is separated spaces or punctuation; - The text of all independent
I
andme
replacedyou
; - Replace all the question marks in the original text with
?
exclamation marks!
; - Output the replaced sentence in one line as AI's answer.
Input format:
Enter a positive integer N that does not exceed 10 in the first line, and then N lines. Each line provides a user's dialogue with no more than 1000 characters and ending with a carriage return. The dialogue is a non-empty string and only includes Letters, numbers, spaces, visible half-width punctuation marks.
Output format:
Output according to the requirements of the question, AI:
and add a space before each answer of AI .
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
Sample output:
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
I really wrote for a long time
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f;
priority_queue<int,vector<int>,greater<int>>q;
const int mod=10e9+7;
string s;
int is_eors(char c)
{
if(isdigit(c)||isalpha(c))
return 0;
if(c==' ')
return 1;
return 2;
}
void check()
{
int len=s.size();
int is_empty=true;
for(int i=0;i<len;i++)
{
if(s[i]!=' ')
is_empty=false;
}
if(is_empty==true)
{
cout<<endl;
return;
}
for(int i=0;i<len;i++)
{
if(isalpha(s[i])&&!islower(s[i])&&s[i]!='I')
{
s[i]+=32;
}
if(s[i]=='?')
s[i]='!';
}
string nsp=" ";
for(int i=0;i<len;i++)
{
if(s[i]==' '&&s[i-1]==' ')
continue;
else
nsp+=s[i];
}
nsp+=' ';
len=nsp.size();
string out="";
for(int i=0;i<len;i++)
{
out+=nsp[i];
if(nsp[i+1]=='I'&&is_eors(nsp[i])>0&&is_eors(nsp[i+2])>0)
{
i++;
out+="you";
}
else if(nsp[i+1]=='m'&&nsp[i+2]=='e'&&is_eors(nsp[i])>0&&is_eors(nsp[i+3])>0)
{
i+=2;
out+="you";
}
else if(nsp[i+1]=='c'&&nsp[i+2]=='a'&&nsp[i+3]=='n'&&nsp[i+5]=='y'&&nsp[i+6]=='o'&&nsp[i+7]=='u'&&is_eors(nsp[i])>0&&is_eors(nsp[i+8])>0)
{
i+=7;
out+="I can";
}
else if(nsp[i+1]=='c'&&nsp[i+2]=='o'&&nsp[i+3]=='u'&&nsp[i+4]=='l'&&nsp[i+5]=='d'&&nsp[i+7]=='y'&&nsp[i+8]=='o'&&nsp[i+9]=='u'&&is_eors(nsp[i])>0&&is_eors(nsp[i+10])>0)
{
i+=9;
out+="I could";
}
}
len=out.size();
for(int i=len-1;i>=0;i--)
{
if(out[i]!=' ')
{
len=i;
break;
}
}
int t=0;
for(int i=0;i<=len;i++)
{
if(i>0&&out[i]==' '&&is_eors(out[i+1])==2)
continue;
else if(out[i]!=' ')
{
cout<<out[i];
t++;
}
else if(out[i]==' '&&t>0)
{
cout<<" ";
}
}
cout<<endl;
}
int main()
{
int N;
cin>>N;
getchar();
for(int i=0;i<N;i++)
{
getline(cin,s);
cout<<s<<endl;
cout<<"AI: ";
check();
}
return 0;
}
int is_eors( char c)//is empty or symbol, must be written, used to classify characters
{
if(isdigit(c)||isalpha(c))
return 0;
if(c==' ')
return 1;
return 2;
}
isalpha() function: determine whether a character is an English letter
islower() function: determine whether a character is a lowercase letter
isdigit() function: determine whether a character is a number
1⃣️ Determine whether the sentence is all empty
2⃣️ Replace uppercase with lowercase and? Replace with!
3⃣️nsp(not_space) remove extra spaces
❀Add empty characters at the beginning and end of nsp to deal with the following replacements for I or me
string nsp=" ";
for(int i=0;i<len;i++)
{
if(s[i]==' '&&s[i-1]==' ')
continue;
else
nsp+=s[i];
}
nsp + = '';
4⃣️out to replace words and phrases
❀Just copy to the new string and skip the replacement part of the original string
if(nsp[i+1]=='I'&&is_eors(nsp[i])>0&&is_eors(nsp[i+2])>0)
{
i++;
out+="you";
}
5⃣️Remove trailing spaces
for(int i=len-1;i>=0;i--)
{
if(out[i]!=' ')
{
len = i;
break;
}
}
for(int i=0;i<=len;i++)
6⃣️Remove the leading space and the space before special characters and output
int t=0;
for(int i=0;i<=len;i++)
{
if (i>0&&out[i]==''&&is_eors(out[i+1])==2)//Skip spaces before special characters
continue;
else if(out[i]!=' ')
{
cout<<out[i];
t++;
}
else if (out[i]==''&&t>0)//Skip the leading space
{
cout<<" ";
}
}