当前位置:网站首页>L1-064 估值一亿的AI核心代码
L1-064 估值一亿的AI核心代码
2022-07-24 11:39:00 【Cod_ing】
一道简单模拟题,比较考验对一些方法的运用,如果不能很好得运用造好的“轮子”实际在比赛中会很耗时间,得不偿失!
1、建议建立映射关系,方便查找替换
2、使用STL容器中“string”类型的replace成员函数
3、功能单一且调用频繁的部分用子函数代替
#include<iostream>
#include<string>
#include<map>
using namespace std;
bool is(char ch) {
//判断是否为符号
if (ch >= 'A' && ch <= 'Z') return false;
if (ch >= 'a' && ch <= 'z') return false;
if (ch >= '0' && ch <= '9') return false;
return true;
}
bool ok(string s,int len, int pos) {
//判断该单词是否独立
if (pos > 0) {
if (is(s[pos - 1]) && is(s[pos + len]))
return true;
}
else if (is(s[pos + len])) return true;
return false;
}
int main() {
int N;
string s1, s2;
string M[4][2] = {
//建立映射关系,可以有效减少代码量
{
"can you","& can"},{
"could you","& could"},{
"I","you"},{
"me","you"}
};
cin >> N;
cin.get();
while (N--) {
getline(cin, s1);
s2 = s1;
while (s2[0] == ' ') s2.erase(s2.begin()); //删除行首、行尾空格
while (s2[s2.size() - 1] == ' ') s2.erase(s2.end()-1);
for (int i = 0; i < s2.size(); i++) {
//删除单词间和符号间多余空格
if (s2[i] == ' ') {
while (s2[i + 1] == ' ') s2.erase(i + 1, 1);
if (is(s2[i + 1])) {
s2.erase(i, 1);
i--;
}
}
}
for (int i = 0; i < s2.size(); i++) {
//替换大写和问号
if (s2[i] >= 'A' && s2[i] <= 'Z' && s2[i] != 'I') s2[i] = s2[i] - 'A' + 'a';
if (s2[i] == '?') s2[i] = '!';
}
for (int i = 0; i < s2.size(); i++) {
//替换部分单词,注意could I 和 can I中的“I”不应该被后续替换为“you”
for (int j = 0; j < 4; j++) {
if (s2.substr(i, M[j][0].size()) == M[j][0] && ok(s2, M[j][0].size(), i)) s2.replace(i, M[j][0].size(), M[j][1]);
}
}
for (int i = 0; i < s2.size(); i++) {
if (s2[i] == '&') s2[i] = 'I';
}
cout << s1 << endl;
cout << "AI: " << s2 << endl;
}
return 0;
}
边栏推荐
- 哈希——242.有效的字母异位词
- CSDN会员的魅力何在?我要他有什么用?
- Build resume editor based on Nocode
- Online customer service chat system source code_ Beautiful and powerful golang kernel development_ Binary operation fool installation_ Construction tutorial attached
- A*与JPS
- Video playback | how to become an excellent reviewer of international journals in the field of Geoscience and ecology?
- Talk about software testing - automated testing framework
- String - 541. Reverse string II
- 哈希——1. 两数之和——有人白天相爱,有人夜里看海,有人力扣第一题都做不出来
- Two important laws about parallelism
猜你喜欢

Stream stream

Directional crawling Taobao product name and price (teacher Songtian)

链表——剑指offer面试题 02.07. 链表相交

Linked list - Sword finger offer interview question 02.07. linked list intersection

安装jmeter

Shengxin weekly issue 37

Semaphore详解

Is there any charge for PDF processing? impossible!

1184. Distance between bus stops: simple simulation problem

1184. 公交站间的距离 : 简单模拟题
随机推荐
Talk about software testing - automated testing framework
字符串——344.反转字符串
Grep actually uses ps/netstat/sort
Sorting out the ideas of data processing received by TCP server, and the note of select: invalid argument error
2022,软测人的平均薪资,看完我瞬间凉了...
In BS4.String and Difference between text
Cgo+gsoap+onvif learning summary: 9. Go and C conduct socket communication and onvif protocol processing
Introduction to Devops and common Devops tools
Collision, removal and cleaning
Operational amplifier - Notes on rapid recovery [1] (parameters)
Fiddler packet capture tool summary
6k+ star, a deep learning code base for Xiaobai! One line of code implements all attention mechanisms!
Code of login page
C # entry series (29) -- preprocessing commands
2 万字详解,吃透 ES!
链表——142. 环形链表 II
Semaphore details
IT圈中的Bug的类型与历史
JVM visualvm: multi hop fault handling tool
[golang] golang implements the string interception function substr