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

题解

直接模拟即可,使用string库的substr可以直接判断某段是否相等,使用replace可以方便的将某段字符改为其它串。
独立的"can you",“could you"需要先判断前后是否都不是字符,处理独立的"I”, “me"时需要在转换为"I can”,"I could"后elseif。

AC代码

#include <stdio.h>
#include <bits/stdc++.h>
#define fst first
#define sed second
using namespace std;
typedef long long ll;

const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
string s;

bool match(int i, const string str, int one = 0) //one独立
{
	bool res = (((!i || !isalpha(s[i - 1])) &&
		(i + str.size() >= s.size() || !isalpha(s[i + str.size()])) ) || !one) //前后无字母或one=0
		&& s.substr(i, str.size()) == str;
	return res;
}
int main()
{
#ifdef LOCAL
	freopen("C:/input.txt", "r", stdin);
#endif
	int T;
	cin >> T;
	getchar();
	while (T--)
	{
		getline(cin, s);
		cout << s << endl;
		for (int i = 0; i < s.size(); ++i)
			if (match(i, "  ") || match(i, " .") || match(i, " ,") || match(i, " ?") || match(i, " !") || match(i, " '"))
				s.erase(i, 1), --i; //删除当前空格 连续处理
			else if (isalpha(s[i]) && s[i] != 'I')
				s[i] = tolower(s[i]);
			else if (s[i] == '?')
				s[i] = '!';
		while (s.size() && s.back() == ' ')
			s.pop_back();
		while (s.size() && s.front() == ' ')
			s.erase(0, 1);
		for (int i = 0; i < s.size(); ++i)
			if (match(i, "can you", 1)) //先独立匹配can you could you
				s.replace(i, 7, "I can");
			else if (match(i, "could you", 1))
				s.replace(i, 9, "I could");
			else if (match(i, "I", 1))
				s.replace(i, 1, "you");
			else if (match(i, "me", 1))
				s.replace(i, 2, "you");
		cout << "AI: " << s << endl;
	}

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值