L1-064 估值一亿的AI核心代码 (20 分)
以上图片来自新浪微博。
本题要求你实现一个稍微更值钱一点的 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
***注意,如果你也是卡在第二个测试点,可以测试 [can I can] -> [can you can] 和 [can you you] -> [I can you]***
- #include <bits/stdc++.h>
- using namespace std;
- string line, orign;
- bool is_fuhao(char c) {
- return (!isspace(c) && !isalpha(c) && !isdigit(c));
- }
- bool is_duli(int s, int t, int limit) {
- return (((s == ) || isspace(line[s - ]) || is_fuhao(line[s - ]))
- && ((t == limit) || isspace(line[t + ]) || is_fuhao(line[t + ])));
- }
- int find_duli(string s, int pos) {
- int idx;
- int len = line.length();
- while(pos < len && (idx = line.find(s, pos)) >= ) {
- if(is_duli(idx, idx + s.length() - , len - ))
- return idx;
- else
- pos += s.length();
- }
- return -;
- }
- void replace_unit(string o, string t) {
- bool isUpdate = true;
- int pos = ;
- while(pos < (int)line.length() && isUpdate) {
- isUpdate = false;
- int idx = find_duli(o, pos);
- if(idx >= ) {
- line.replace(idx, o.length(), t);
- isUpdate = true;
- pos = idx + o.length();
- }
- }
- }
- void proc_replace() {
- replace_unit("I", "You");
- replace_unit("me", "You");
- replace_unit("can you", "I can");
- replace_unit("could you", "I could");
- }
- void del_blank() {
- bool isUpdate = true;
- while(isUpdate) {
- int len = line.length(), pos = ;
- isUpdate = false;
- for(int i = pos; i < len; i++) { // 结尾不会是空格,不会越界
- if(isspace(line[i]) && (isspace(line[i + ]) || is_fuhao(line[i + ]))) {
- line.erase(i, );
- pos = i;
- isUpdate = true;
- break;
- }
- }
- }
- }
- void to_lower_tanhao() {
- transform(line.begin(), line.end(), line.begin(), [](char c)->char {
- if(c == '?') return '!';
- else if(c == 'I') return 'I';
- return tolower(c);
- });
- }
- void input_and_trim() {
- getline(cin, orign);
- line = orign;
- if(!line.empty()) {
- line.erase(,line.find_first_not_of(" "));
- line.erase(line.find_last_not_of(" ") + );
- }
- }
- int main() {
- int n;
- cin >> n;
- getchar();
- while(n --) {
- input_and_trim();
- to_lower_tanhao();
- del_blank();
- proc_replace();
- to_lower_tanhao();
- cout << orign << endl;
- cout <<"AI: "<< line << endl;
- }
- }
L1-064 估值一亿的AI核心代码 (20 分)的更多相关文章
- PTA --- 天梯赛 L1-064 估值一亿的AI核心代码
L1-064 估值一亿的AI核心代码 (20 point(s)) 本题要求你实现一个稍微更值钱一点的 AI 英文问答程序,规则是: 无论用户说什么,首先把对方说的话在一行中原样打印出来: 消除原文中多 ...
- L1-064 估值一亿的AI核心代码
以上图片来自新浪微博. 本题要求你实现一个稍微更值钱一点的 AI 英文问答程序,规则是: 无论用户说什么,首先把对方说的话在一行中原样打印出来: 消除原文中多余空格:把相邻单词间的多个空格换成 1 个 ...
- PTA 估值一亿的AI核心代码
题面 比赛时被模拟题打自闭了,本来以为是个比较麻烦的模拟,实际上只要会C++的regex不到40行就能把这个题过掉了(orz smz) regex是用来处理正则表达式,里面有个函数regex_repl ...
- PTA天梯赛训练题L1-064:估值一亿的AI核心代码(字符串模拟)
Update:smz说regex秒过Orz,yzd记在这里了. 听说今年天梯赛有个烦人的模拟,我便被队友逼着试做一下……一发15,二发20.记一记,要不然枉费我写这么久…… 自己还是代码能力太菜了,校 ...
- go & AI核心代码
- 【五子棋AI循序渐进】关于VCT,VCF的思考和核心代码
前面几篇发布了一些有关五子棋的基本算法,其中有一些BUG也有很多值得再次思考的问题,在框架和效果上基本达到了一个简单的AI的水平,当然,我也是初学并没有掌握太多的高级技术.对于这个程序现在还在优化当中 ...
- .NET核心代码保护策略
.NET核心代码保护策略-隐藏核心程序集 经过之前那个道德指责风波过后也有一段时间没写博客了,当然不是我心怀内疚才这么久不写,纯粹是程序员的通病..怎一个懒字了得,本来想写一些长篇大论反讽一下那些道德 ...
- 基于Mysql数据库亿级数据下的分库分表方案
移动互联网时代,海量的用户数据每天都在产生,基于用户使用数据的用户行为分析等这样的分析,都需要依靠数据都统计和分析,当数据量小时,问题没有暴露出来,数据库方面的优化显得不太重要,一旦数据量越来越大时, ...
- Libgdx: android单机斗地主支持局域网wifi联网的网络模块核心代码
这个作品是我近期写的,结合我的毕业设计的通信模块和之前的单机版斗地主.我已经上架到豌豆荚了,贴了点广告,看看能不能赚点茶钱. 但是一点也不乐观.因此我想分享给大家源代码. 仅仅要不用于商业. 以下先贴 ...
随机推荐
- 年视图,选择月份 ---bootstrap datepicker
$.fn.datepicker.dates['cn'] = { //切换为中文显示 days: ["周日", "周一", "周二", &qu ...
- django 快速实现登陆,接着注册的项目写(五)
1.改项目的urls.py from django.conf.urls import url,include from django.contrib import admin admin.autodi ...
- IntelliJ IDEA 2017.3 搭建一个多模块的springboot项目(三)
你得先看完前两篇文章才可以进行第三章.这章我又新建了一个模块,起名叫project-core.建立方式与(一)里面一致,一个普通的maven模块.不需要勾选archetype的那种. 大家自己建好后, ...
- 如何配置git send-email相关的邮箱信息?
关键是配置smtpserver,请参考此处
- UDDI:百科
ylbtech-UDDI:百科 UDDI是一种用于描述.发现.集成Web Service的技术,它是Web Service协议栈的一个重要部分.通过UDDI,企业可以根据自己的需要动态查找并使用Web ...
- 对每个CheckBox的循环
$("input[name='ck2']").each(function(){ if(this.checked == false){ $(this).parent().parent ...
- Nginx知识
OpenResty最佳实践->location匹配规则传说中图片防盗链的爱恨情仇
- 在spring的业务层获取request,response
1.直接通过controller层获取到传输到业务层2.SpringMVC提供的RequestContextHolder可以直接获取代码: RequestAttributes requestAttri ...
- SpringBoot: 8.整合freemarker(转)
1.创建maven项目,添加pom依赖 <!--springboot项目依赖的父项目--> <parent> <groupId>org.springframewor ...
- 爬虫——简单处理js中嵌入的json数据
看了群里一个人提问道https://www.amazon.com/,商品分类那里无法用xpath拿得到列表.遂对其研究. 通过抓包工具可以得知,原始数据存在于js代码中,我的方式是手动解析js,从里面 ...