L1-064 估值一亿的AI核心代码 (20 分)

L1-064 估值一亿的AI核心代码 (20 分)

在这里插入图片描述
以上图片来自新浪微博。

本题要求你实现一个稍微更值钱一点的 AI 英文问答程序,规则是:

  • 无论用户说什么,首先把对方说的话在一行中原样打印出来;
  • 消除原文中多余空格:把相邻单词间的多个空格换成 1
    个空格,把行首尾的空格全部删掉,把标点符号前面的空格删掉;
  • 把原文中所有大写英文字母变成小写,除了 I
  • 把原文中所有独立的 can youcould you 对应地换成 I canI could—— 这里“独立”是指被空格或标点符号分隔开的单词;
  • 把原文中所有独立的 Ime 换成 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

不会,看了好久的题解,照着敲了好久,还是有地方过不去
嘿嘿,为了尊重原创性,这里就直接给出原代码吧

博客链接

原代码

#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
using namespace std;
int d(char x)
{
	if (x >= 'A'&&x <= 'Z') return -1;
	if (x >= 'a'&&x <= 'z') return -2;
	if (x >= '0'&&x <= '9') return -3;
	if (x == ' ') return 2;
	return 1;
}
string low(string str)
{
	string b;
	b += " ";
	for (int i = 0; i < str.size(); i++)
	{
		if (str[i] >= 'A'&&str[i] <= 'Z'&&str[i] != 'I')
		{
			str[i] += 32;
		}
		if (str[i] == '?') str[i] = '!';
	}
	b += str;
	b += " ";
	str.clear();
	str += " ";
	for (int i = 1; i < b.size() - 1; i++)
	{
		if (b[i] == ' ' && b[i - 1] == ' ')	continue;
		else if (d(b[i + 1]) == 1 && b[i] == ' ')	continue;
		else	str += b[i];
	}
	str += " ";
	return str;
}
int main()
{
	int t, i, j, k;
	string str;
	cin >> t;
	getchar();
	while (t--)
	{
		getline(cin, str);
		cout << str << endl;
		cout << "AI: ";
		bool kong = false;
		for (i = 0; i < str.size(); i++)
		{
			if (str[i] != ' ')
			{
				kong = true;
				break;
			}
		}
		if (kong == false)
		{
			cout << endl;
			continue;
		}
		str = low(str);
		string temp;
		for (i = 0; i < str.size(); i++)
		{
			temp += str[i];
			if (d(str[i])>0 && str[i + 1] == 'm'&&str[i + 2] == 'e'&&d(str[i + 3])>0)
			{
				i += 2;
				temp += "you";
			}
			else if (d(str[i])>0 && str[i + 1] == 'I'&&d(str[i + 2])>0)
			{
				i++;
				temp += "you";
			}
			else if (d(str[i])>0 && str[i + 1] == 'c'&& str[i + 2] == 'a'&& str[i + 3] == 'n'&& str[i + 5] == 'y'&& str[i + 6] == 'o'&& str[i + 7] == 'u'&&d(str[i + 8])>0)
			{
				i += 7;
				temp += "I can";
			}
			else if (d(str[i])>0 && str[i + 1] == 'c'&& str[i + 2] == 'o'&& str[i + 3] == 'u'&& str[i + 4] == 'l'&& str[i + 5] == 'd'&&  str[i + 7] == 'y'&& str[i + 8] == 'o'&& str[i + 9] == 'u'&&d(str[i + 10])>0)
			{
				i += 9;
				temp += "I could";
			}
		}
		str = "";
		str += temp;
		//cout << str << endl;
		int len;
		for (i = str.size() - 1; i >= 0; i--)
		{
			if (str[i] != ' ')
			{
				len = i;
				break;
			}
		}
		int cnt = 0;
		for (i = 0; i <= len; i++)
		{
			if (i>0 && str[i] == ' '&& d(str[i + 1]) == 1)	continue;
			if (str[i] != ' ')  cout << str[i], cnt++;
			else if (cnt>0) cout << " ";
		}
		cout << endl;
	}
	return 0;
}

自己的错误代码

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#include<sstream>

using namespace std;
typedef long long ll;
const int N=10010;

int check(char x)
{
	if(x >= 'A' && x <= 'Z') return -1;
	if(x >= 'a' && x <= 'z') return -2;
	if(x >= '0' && x <= '9') return -3;
	if(x == ' ') return 2;
	return 1;
}

string low(string a)
{
	// 转小写 
	for(int i = 0; i < a.size(); i ++ )
	{
		if(a[i] >= 'A' && a[i] <= 'Z' && a[i] != 'I')
			a[i] += 32;
		if(a[i] == '?') a[i] = '!';
	}
	string b;
	for(int i = 0; i < a.size() - 1; i ++ )
	{
		if(a[i] == ' ' && a[i + 1] == ' ')
			continue;
		else if(check(a[i + 1]) == 1 && a[i] == ' ')
			continue;
		else b += a[i];
	} 
	// 去掉末尾空格 
	if(a[a.size() - 1] != ' ') b += a[a.size() - 1];
	// 去掉首部空格 
	if(b[0] == ' ')
	{
		string c;
		for(int i = 1; i < b.size(); i ++ ) c += b[i];
		return c;
	}
	return b;
}

int main()
{
	int n;
	cin >> n;
	getchar();
	while(n -- )
	{
		string a;
		getline(cin, a);
		cout << a << endl;
		cout << "AI: ";
		a = low(a); 
		string temp;
		for(int i = 0; i < a.size(); i ++ )
		{
			temp += a[i];
			if(check(a[i]) > 0 && a[i + 1] == 'c' && a[i + 2] == 'a' && a[i + 3] == 'n' && a[i + 5] == 'y' && a[i + 6] == 'o' && a[i + 7] == 'u' && check(a[i + 8]) > 0)
			{
				i += 6;
				temp += "I can";
			}
			else if(check(a[i]) > 0 && a[i + 1] == 'm' && a[i + 2] == 'e' && check(a[i + 3]) > 0)
			{
				i += 2;
				temp += "you";
			}
			else if(check(a[i]) > 0 && a[i + 1] == 'I' && check(a[i + 2]) > 0)
			{
				i ++ ;
				temp += "you";
			}
			else if(check(a[i]) > 0 && a[i + 1] == 'c' && a[i + 2] == 'o' && a[i + 3] == 'u' && a[i + 4] == 'l' && a[i + 5] == 'd' && a[i + 7] == 'y' && a[i + 8] == 'o' && a[i + 9] == 'u' && check(a[i + 10]) > 0)
			{
				i += 8;
				temp += "I could";
			}
		}
		cout << temp << endl;
	}
	return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

在人间负债^

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值