版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zzuli_Acmer/article/details/88919992
本题要求你实现一个稍微更值钱一点的 AI 英文问答程序,规则是:
- 无论用户说什么,首先把对方说的话在一行中原样打印出来;
- 消除原文中多余空格:把相邻单词间的多个空格换成 1 个空格,把行首尾的空格全部删掉,把标点符号前面的空格删掉;
- 把原文中所有大写英文字母变成小写,除了
I
; - 把原文中所有独立的
can you
、could you
对应地换成I can
、I could
—— 这里“独立”是指被空格或标点符号分隔开的单词; - 把原文中所有独立的
I
和me
换成you
; - 把原文中所有的问号
?
换成惊叹号!
; - 在一行中输出替换后的句子作为 AI 的回答。
输入格式:
输入首先在第一行给出不超过 10 的正整数 N,随后 N 行,每行给出一句不超过 1000 个字符的、以回车结尾的用户的对话,对话为非空字符串,仅包括字母、数字、空格、可见的半角标点符号。
输出格式:
按题面要求输出,每个 AI 的回答前要加上 AI:
和一个空格。
输入样例:
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
输出样例:
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
写了快两个小时,头晕眼花,不知自己是谁,想了无数样例依然只有19分。。。希望有大佬能。。给个错误样例。。感谢!
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stack>
using namespace std;
char ch[10010];
char canyou[] = "can you";
char couldyou[] = "could you";
void delSpace(int x,int sl,int l);
int isCan(int x);
int isCould(int x);
void delSpace(int x,int sl,int l)
{
for(int i=x; i<l-sl; i++)
ch[i] = ch[i+sl];
}
int isCan(int x,int l)
{
for(int i=0; i<7; i++)
{
if(ch[x+i] != canyou[i])
return 0;
}
return 1;
}
int isCould(int x,int l)
{
for(int i=0; i<9; i++)
{
if(ch[i+x] != couldyou[i])
return 0;
}
return 1;
}
int main()
{
int n;
scanf("%d",&n);
getchar();
while(n--)
{
memset(ch,'\0',sizeof(ch));
int l = 0;
while(1)
{
char xx;
scanf("%c",&xx);
if(xx == '\n')
break;
ch[l++] = xx;
}
puts(ch);
printf("AI: ");
stack<char> s1;
stack<char> s2;
while(!s1.empty()) s1.pop();
while(!s2.empty()) s2.pop();
for(int i=0; i<l; i++)
{
if(ch[i]>='A' && ch[i]<='Z' && ch[i]!='I')
ch[i]+=32;
s1.push(ch[i]);
}
while(!s1.empty())
{
char cs = s1.top();
s1.pop();
if(s2.empty() && cs!=' ')
s2.push(cs);
else
{
if(cs == ' ' && s2.empty())
continue;
else if(cs == ' ' && cs == s2.top())
continue;
else s2.push(cs);
}
}
l = 0;
while(!s2.empty())
{
ch[l++] = s2.top();
s2.pop();
}
/* for(int i=0; i<l; i++)
{
if(ch[i] == ' ' && ch[i+1] == ' ')
delSpace(i,++sl,l);
else if(ch[i] == ' ' && ch[i-1] == ' ')
delSpace(i-1,++sl,l);
}
l-=sl;*/
//int ff = 0;
for(int i=l-1; i>=0; i--)
{
if(ch[i] == ' ')
l--;
else break;
}
int ff = 0;
for(int i=0; i<l; i++)
{
if(ch[i] == ' ' && ff==0)
continue;
ff=1;
if(ch[i] == ' ' && i-1>=0 && i+1<l &&!isalpha(ch[i+1]) &&!isdigit(ch[i+1]))
continue;
if(ch[i] == 'c'&& i+7<=l&&isCan(i,l))
{
if(i==0)
{
if(i+7 == l)
{
if(isCan(i,l))
{
printf("I can");
i+=6;
}
else printf("c");
}
else if(i+7 > l)
printf("c");
else
{
if(isCan(i,l) && !isdigit(ch[i+7]) &&!isalpha(ch[i+7]))
{
printf("I can");
i+=6;
}
else printf("c");
}
}
else if(i>0)
{
if(isdigit(ch[i-1] )|| isalpha(ch[i-1]))
{
printf("c");
continue;
}
if(i+7 == l)
{
if(isCan(i,l))
{
printf("I can");
i+=6;
}
else printf("c");
}
else if(i+7 > l)
printf("c");
else
{
if(isCan(i,l) && !isdigit(ch[i+7]) &&!isalpha(ch[i+7]))
{
printf("I can");
i+=6;
}
else printf("c");
}
}
}
else if(ch[i] == 'c'&& i+9<=l&&isCould(i,l))
{
if(i==0)
{
if(i+9 == l)
{
if(isCould(i,l))
{
printf("I could");
i+=8;
}
else printf("c");
}
else if(i+9 > l)
printf("c");
else
{
if(isCould(i,l) && !isdigit(ch[i+9]) &&!isalpha(ch[i+9]))
{
printf("I could");
i+=8;
}
else printf("c");
}
}
else if(i>0)
{
if(isdigit(ch[i-1] )|| isalpha(ch[i-1]))
{
printf("c");
continue;
}
if(i+9 == l)
{
if(isCould(i,l))
{
printf("I could");
i+=8;
}
else printf("c");
}
else if(i+9 > l)
printf("c");
else
{
if(isCould(i,l) && !isdigit(ch[i+9]) &&!isalpha(ch[i+9]))
{
printf("I could");
i+=8;
}
else printf("c");
}
}
}
else if(ch[i] == 'I')
{
if(i==0)
{
if(i+1<l&&!isalpha(ch[i+1])&&!isdigit(ch[i+1]))
printf("you");
else printf("I");
}
else if(i==l-1)
{
if(!isalpha(ch[i-1])&&!isdigit(ch[i-1]))
printf("you");
else printf("I");
}
else
{
if(!isdigit(ch[i-1]) && !isdigit(ch[i-1]) && !isalpha(ch[i+1]) && !isalpha(ch[i+1]))
printf("you");
else printf("I");
}
}
else if(ch[i] == 'm')
{
if(i==0 && l>=2 && ch[i+1]=='e')
{
if(l==2)
{
printf("you");
i++;
}
else if(l>=3 && !isdigit(ch[i+2]) && !isalpha(ch[i+2]))
{
printf("you");
i++;
}
else
printf("m");
}
else if(i == l-2)
{
if(ch[i+1] == 'e' &&!isdigit(ch[i-1]) && !isalpha(ch[i-1]))
printf("you"),i++;
else printf("m");
}
else
{
if(ch[i+1]=='e' && !isdigit(ch[i-1]) &&!isalpha(ch[i-1]) && !isdigit(ch[i+2]) && !isalpha(ch[i+2]))
printf("you"),i++;
else printf("m");
}
}
else if(ch[i] == '?')
printf("!");
else printf("%c",ch[i]);
}
printf("\n");
}
return 0;
}