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

以上图片来自新浪微博。

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

  • 无论用户说什么,首先把对方说的话在一行中原样打印出来;
  • 消除原文中多余空格:把相邻单词间的多个空格换成 1 个空格,把行首尾的空格全部删掉,把标点符号前面的空格删掉;
  • 把原文中所有大写英文字母变成小写,除了 I
  • 把原文中所有独立的 can youcould you 对应地换成 I canI could—— 这里“独立”是指被空格或标点符号分隔开的单词;
  • 把原文中所有独立的 I 和 me 换成 you
  • 把原文中所有的问号 ? 换成惊叹号 !
  • 在一行中输出替换后的句子作为 AI 的回答。

输入格式:

输入首先在第一行给出不超过 10 的正整数 N,随后 N 行,每行给出一句不超过 1000 个字符的、以回车结尾的用户的对话,对话为非空字符串,仅包括字母、数字、空格、可见的半角标点符号。

输出格式:

按题面要求输出,每个 AI 的回答前要加上 AI: 和一个空格。

输入样例:

  1. 6
  2. Hello ?
  3. Good to chat with you
  4. can you speak Chinese?
  5. Really?
  6. Could you show me 5
  7. What Is this prime? I,don 't know

输出样例:

  1. Hello ?
  2. AI: hello!
  3. Good to chat with you
  4. AI: good to chat with you
  5. can you speak Chinese?
  6. AI: I can speak chinese!
  7. Really?
  8. AI: really!
  9. Could you show me 5
  10. AI: I could show you 5
  11. What Is this prime? I,don 't know
  12. AI: what Is this prime! you,don't know
  13. ***注意,如果你也是卡在第二个测试点,可以测试 [can I can] -> [can you can] [can you you] -> [I can you]***
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. string line, orign;
  5.  
  6. bool is_fuhao(char c) {
  7. return (!isspace(c) && !isalpha(c) && !isdigit(c));
  8. }
  9.  
  10. bool is_duli(int s, int t, int limit) {
  11.  
  12. return (((s == ) || isspace(line[s - ]) || is_fuhao(line[s - ]))
  13. && ((t == limit) || isspace(line[t + ]) || is_fuhao(line[t + ])));
  14. }
  15.  
  16. int find_duli(string s, int pos) {
  17. int idx;
  18. int len = line.length();
  19. while(pos < len && (idx = line.find(s, pos)) >= ) {
  20. if(is_duli(idx, idx + s.length() - , len - ))
  21. return idx;
  22. else
  23. pos += s.length();
  24. }
  25. return -;
  26. }
  27.  
  28. void replace_unit(string o, string t) {
  29. bool isUpdate = true;
  30. int pos = ;
  31. while(pos < (int)line.length() && isUpdate) {
  32. isUpdate = false;
  33. int idx = find_duli(o, pos);
  34. if(idx >= ) {
  35. line.replace(idx, o.length(), t);
  36. isUpdate = true;
  37. pos = idx + o.length();
  38. }
  39. }
  40. }
  41.  
  42. void proc_replace() {
  43. replace_unit("I", "You");
  44. replace_unit("me", "You");
  45. replace_unit("can you", "I can");
  46. replace_unit("could you", "I could");
  47. }
  48.  
  49. void del_blank() {
  50. bool isUpdate = true;
  51. while(isUpdate) {
  52. int len = line.length(), pos = ;
  53. isUpdate = false;
  54. for(int i = pos; i < len; i++) { // 结尾不会是空格,不会越界
  55. if(isspace(line[i]) && (isspace(line[i + ]) || is_fuhao(line[i + ]))) {
  56. line.erase(i, );
  57. pos = i;
  58. isUpdate = true;
  59. break;
  60. }
  61. }
  62. }
  63. }
  64.  
  65. void to_lower_tanhao() {
  66.  
  67. transform(line.begin(), line.end(), line.begin(), [](char c)->char {
  68. if(c == '?') return '!';
  69. else if(c == 'I') return 'I';
  70. return tolower(c);
  71. });
  72.  
  73. }
  74.  
  75. void input_and_trim() {
  76.  
  77. getline(cin, orign);
  78. line = orign;
  79.  
  80. if(!line.empty()) {
  81. line.erase(,line.find_first_not_of(" "));
  82. line.erase(line.find_last_not_of(" ") + );
  83. }
  84.  
  85. }
  86.  
  87. int main() {
  88.  
  89. int n;
  90. cin >> n;
  91. getchar();
  92. while(n --) {
  93. input_and_trim();
  94. to_lower_tanhao();
  95. del_blank();
  96. proc_replace();
  97. to_lower_tanhao();
  98. cout << orign << endl;
  99. cout <<"AI: "<< line << endl;
  100. }
  101.  
  102. }
  1.  

L1-064 估值一亿的AI核心代码 (20 分)的更多相关文章

  1. PTA --- 天梯赛 L1-064 估值一亿的AI核心代码

    L1-064 估值一亿的AI核心代码 (20 point(s)) 本题要求你实现一个稍微更值钱一点的 AI 英文问答程序,规则是: 无论用户说什么,首先把对方说的话在一行中原样打印出来: 消除原文中多 ...

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

    以上图片来自新浪微博. 本题要求你实现一个稍微更值钱一点的 AI 英文问答程序,规则是: 无论用户说什么,首先把对方说的话在一行中原样打印出来: 消除原文中多余空格:把相邻单词间的多个空格换成 1 个 ...

  3. PTA 估值一亿的AI核心代码

    题面 比赛时被模拟题打自闭了,本来以为是个比较麻烦的模拟,实际上只要会C++的regex不到40行就能把这个题过掉了(orz smz) regex是用来处理正则表达式,里面有个函数regex_repl ...

  4. PTA天梯赛训练题L1-064:估值一亿的AI核心代码(字符串模拟)

    Update:smz说regex秒过Orz,yzd记在这里了. 听说今年天梯赛有个烦人的模拟,我便被队友逼着试做一下……一发15,二发20.记一记,要不然枉费我写这么久…… 自己还是代码能力太菜了,校 ...

  5. go & AI核心代码

  6. 【五子棋AI循序渐进】关于VCT,VCF的思考和核心代码

    前面几篇发布了一些有关五子棋的基本算法,其中有一些BUG也有很多值得再次思考的问题,在框架和效果上基本达到了一个简单的AI的水平,当然,我也是初学并没有掌握太多的高级技术.对于这个程序现在还在优化当中 ...

  7. .NET核心代码保护策略

    .NET核心代码保护策略-隐藏核心程序集 经过之前那个道德指责风波过后也有一段时间没写博客了,当然不是我心怀内疚才这么久不写,纯粹是程序员的通病..怎一个懒字了得,本来想写一些长篇大论反讽一下那些道德 ...

  8. 基于Mysql数据库亿级数据下的分库分表方案

    移动互联网时代,海量的用户数据每天都在产生,基于用户使用数据的用户行为分析等这样的分析,都需要依靠数据都统计和分析,当数据量小时,问题没有暴露出来,数据库方面的优化显得不太重要,一旦数据量越来越大时, ...

  9. Libgdx: android单机斗地主支持局域网wifi联网的网络模块核心代码

    这个作品是我近期写的,结合我的毕业设计的通信模块和之前的单机版斗地主.我已经上架到豌豆荚了,贴了点广告,看看能不能赚点茶钱. 但是一点也不乐观.因此我想分享给大家源代码. 仅仅要不用于商业. 以下先贴 ...

随机推荐

  1. 年视图,选择月份 ---bootstrap datepicker

    $.fn.datepicker.dates['cn'] = { //切换为中文显示 days: ["周日", "周一", "周二", &qu ...

  2. django 快速实现登陆,接着注册的项目写(五)

    1.改项目的urls.py from django.conf.urls import url,include from django.contrib import admin admin.autodi ...

  3. IntelliJ IDEA 2017.3 搭建一个多模块的springboot项目(三)

    你得先看完前两篇文章才可以进行第三章.这章我又新建了一个模块,起名叫project-core.建立方式与(一)里面一致,一个普通的maven模块.不需要勾选archetype的那种. 大家自己建好后, ...

  4. 如何配置git send-email相关的邮箱信息?

    关键是配置smtpserver,请参考此处

  5. UDDI:百科

    ylbtech-UDDI:百科 UDDI是一种用于描述.发现.集成Web Service的技术,它是Web Service协议栈的一个重要部分.通过UDDI,企业可以根据自己的需要动态查找并使用Web ...

  6. 对每个CheckBox的循环

    $("input[name='ck2']").each(function(){ if(this.checked == false){ $(this).parent().parent ...

  7. Nginx知识

    OpenResty最佳实践->location匹配规则传说中图片防盗链的爱恨情仇 ​

  8. 在spring的业务层获取request,response

    1.直接通过controller层获取到传输到业务层2.SpringMVC提供的RequestContextHolder可以直接获取代码: RequestAttributes requestAttri ...

  9. SpringBoot: 8.整合freemarker(转)

    1.创建maven项目,添加pom依赖 <!--springboot项目依赖的父项目--> <parent> <groupId>org.springframewor ...

  10. 爬虫——简单处理js中嵌入的json数据

    看了群里一个人提问道https://www.amazon.com/,商品分类那里无法用xpath拿得到列表.遂对其研究. 通过抓包工具可以得知,原始数据存在于js代码中,我的方式是手动解析js,从里面 ...