L1-064 估值一亿的AI核心代码

本题要求你实现一个稍微更值钱一点的 AI 英文问答程序,规则是:
1.无论用户说什么,首先把对方说的话在一行中原样打印出来;
2.消除原文中多余空格:把相邻单词间的多个空格换成 1 个空格,把行首尾的空格全部删掉,把标点符号前面的空格删掉;
3.把原文中所有大写英文字母变成小写,除了 I;
4.把原文中所有独立的 can you、could you 对应地换成 I can、I could—— 这里“独立”是指被空格或标点符号分隔开的单词;
5.把原文中所有独立的 I 和 me 换成 you;
6.把原文中所有的问号 ? 换成惊叹号 !;
7.在一行中输出替换后的句子作为 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
整体解题思路:
这道题是一道非常冗余的模拟题,首先,题干规则第一条就是将输入原样打印出来(作者就是因为没注意第一个规则导致WA了n次(手动笑哭))。其次,需要注意的是在执行第4,5规则时必须先执行第三规则,也就是先将除 I 以外的大写字母转换为小写字母且第四规则和第五规则不会互相影响(也就是 I can 里面的 I 不能根据第五规则转换成 you )。注意以上几点,你就可以“快乐”模拟了。
AC代码:
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
char s[1100];
char ans_1[1100];
char ans_2[1100];
char ans_3[1100];
char ans_4[1100];
bool check(char ch)//判断是否为符号
{
if(ch >= 'a' && ch <= 'z')
return false;
if(ch >= 'A' && ch <= 'Z')
return false;
if(ch >= '0' && ch <= '9')
return false;
if(ch == ' ')
return false;
return true;
}
int check_6(char s[], char ans_1[], int index)
{
int index_pre = 0;
for(int i = 0; i < index; i ++)
{
if(s[i] == '?')
{
ans_1[index_pre ++] = '!';
}
else
ans_1[index_pre ++] = s[i];
}
return index_pre;
}
int check_2(char ans_1[], char ans_2[], int index)
{
int index_pre = 0;
int flag = 0;
for(int i = 0; i < index; i ++)
{
if(ans_1[i] != ' ')
flag = 1;
if(flag == 1)
{
if(i + 1 <= index && ans_1[i] == ' ' && check(ans_1[i + 1]) == false && ans_1[i + 1] != ' ')
{
ans_2[index_pre ++] = ' ';
}
else if(ans_1[i] != ' ')
ans_2[index_pre ++] = ans_1[i];
}
}
return index_pre;
}
int check_3(char ans_2[], char ans_3[], int index)
{
int index_pre = 0;
for(int i = 0; i < index; i ++)
{
if(ans_2[i] >= 'A' && ans_2[i] <= 'Z' && ans_2[i] != 'I')
ans_3[index_pre ++] = ans_2[i] + 32;
else
ans_3[index_pre ++] = ans_2[i];
}
return index_pre;
}
int check_4_5(char ans_3[], char ans_4[], int index)//第四,五规则写的有点长,读者自行优化(手动狗头)
{
int index_pre = 0;
for(int i = 0; i < index; i ++)
{
if(i + 7 <= index && (i==0||ans_3[i-1]==' '||check(ans_3[i-1]))&&ans_3[i]=='c'&&ans_3[i+1]=='a'&&ans_3[i+2]=='n'&&ans_3[i+3]==' '&&ans_3[i+4]=='y'&&ans_3[i+5]=='o'&&ans_3[i+6]=='u'&&(i+7==index||ans_3[i+7]==' '||check(ans_3[i+7])))
{
ans_4[index_pre ++] = 'I';
ans_4[index_pre ++] = ' ';
ans_4[index_pre ++] = 'c';
ans_4[index_pre ++] = 'a';
ans_4[index_pre ++] = 'n';
i += 6;
}
else if(i + 9 <= index && (i==0||ans_3[i-1]==' '||check(ans_3[i-1]))&&ans_3[i]=='c'&&ans_3[i+1]=='o'&&ans_3[i+2]=='u'&&ans_3[i+3]=='l'&&ans_3[i+4]=='d'&&ans_3[i+5]==' '&&ans_3[i+6]=='y'&&ans_3[i+7]=='o'&&ans_3[i+8]=='u'&&(i+9==index||ans_3[i+9]==' '||check(ans_3[i+9])))
{
ans_4[index_pre ++] = 'I';
ans_4[index_pre ++] = ' ';
ans_4[index_pre ++] = 'c';
ans_4[index_pre ++] = 'o';
ans_4[index_pre ++] = 'u';
ans_4[index_pre ++] = 'l';
ans_4[index_pre ++] = 'd';
i += 8;
}
else if(i == 0 && ans_3[i] == 'I' && (i + 1 == index || ans_3[i + 1] == ' ' || check(ans_3[i + 1])))
{
ans_4[index_pre ++] = 'y';
ans_4[index_pre ++] = 'o';
ans_4[index_pre ++] = 'u';
}
else if(i + 1 < index && i > 0 && (ans_3[i - 1] == ' ' || check(ans_3[i - 1])) && ans_3[i] == 'I' && (ans_3[i + 1] == ' ' || check(ans_3[i + 1])))
{
ans_4[index_pre ++] = 'y';
ans_4[index_pre ++] = 'o';
ans_4[index_pre ++] = 'u';
}
else if(i + 1 == index && i > 0 && (ans_3[i-1]==' '||check(ans_3[i-1]))&&ans_3[i]=='I')
{
ans_4[index_pre ++] = 'y';
ans_4[index_pre ++] = 'o';
ans_4[index_pre ++] = 'u';
}
else if(i + 2 <= index && ans_3[i - 1] == ' ' && ans_3[i] == 'm' && ans_3[i + 1] == 'e' && (i + 2 == index || check(ans_3[i + 2]) || ans_3[i + 2] == ' '))
{
ans_4[index_pre ++] = 'y';
ans_4[index_pre ++] = 'o';
ans_4[index_pre ++] = 'u';
i ++;
}
else if(i == 0 && ans_3[i] == 'm' && ans_3[i + 1] == 'e' && (ans_3[i + 2] == ' ' || check(ans_3[i + 2])))
{
ans_4[index_pre ++] = 'y';
ans_4[index_pre ++] = 'o';
ans_4[index_pre ++] = 'u';
i ++;
}
else
ans_4[index_pre ++] = ans_3[i];
}
return index_pre;
}
int main()
{
int n;
cin >> n;
getchar();
while(n --)
{
memset(s, -1, sizeof(s));
int index = 0;
char pre;
while(~scanf("%c", &pre) && pre != '\n')
s[index ++] = pre;
for(int i = 0; i < index; i ++)
cout << s[i];
cout << '\n';
memset(ans_1, -1, sizeof(ans_1));
index = check_6(s, ans_1, index);
memset(ans_2, -1, sizeof(ans_2));
index = check_2(ans_1, ans_2, index);
if(index == 0)
{
cout << "AI: " << '\n';
continue;
}
memset(ans_3, -1, sizeof(ans_3));
index = check_3(ans_2, ans_3, index);
memset(ans_4, -1, sizeof(ans_4));
index = check_4_5(ans_3, ans_4, index);
cout << "AI: ";
for(int i = 0; i < index; i ++)
{
cout << ans_4[i];
}
cout << '\n';
}
return 0;
}
写在最后:
希望读者明白思路后,自行码代码,很考验耐心哦!

本文档介绍了一道关于实现AI英文问答程序的题目,要求包括原样打印用户输入、消除多余空格、大小写转换、特定单词替换、问号转惊叹号等步骤。提供的AC代码展示了如何逐条满足这些规则,对输入的字符串进行处理,转换成AI的回答。题目强调了执行规则的顺序和注意事项,适合用于理解字符串操作和模拟题目。

1437

被折叠的 条评论
为什么被折叠?



