"you"时又重新改变了。解决办法:在第一步时先用‘A’替换'I',全部完成后再替换成'I'_估值一亿的ai核心代码">
当前位置:   article > 正文

L1-064 估值一亿的AI核心代码——按照规则逐一击破

估值一亿的ai核心代码

一、题目

估值一亿的AI核心代码

二、分析

1、消除原文中多余空格:把相邻单词间的多个空格换成 1 个空格,把行首尾的空格全部删掉,把标点符号前面的空格删掉;

判断是否为符号

  1. bool If(char op) //判断op是否为符号
  2. {
  3. if (op == '0')
  4. return false;
  5. if (op >= 'a' && op <= 'z')
  6. return false;
  7. if (op >= 'A' && op <= 'Z')
  8. return false;
  9. if (op >= '0' && op <= '9')
  10. return false;
  11. return true;
  12. }
  1. for (int i = 0; i < str.size(); i++)
  2. {
  3. if (str[i] == ' ')
  4. {
  5. int j = i;
  6. while (j + 1 < str.size() && str[j + 1] == ' ')
  7. j++;
  8. str.erase(i, j - i);
  9. i = j;
  10. }
  11. }
  12. if (str.size() != 0 && str[0] == ' ')
  13. str.erase(0, 1);
  14. if (str.size() != 0 && str[str.size() - 1] == ' ')
  15. str.erase(str.size() - 1, 1);
  16. for (int i = 1; i < str.size(); i++)
  17. {
  18. if (If(str[i]) && str[i - 1] == ' ')
  19. {
  20. str.erase(i - 1, 1);
  21. i--;
  22. }
  23. }

2、把原文中所有大写英文字母变成小写,除了 I

  1. for (int i = 0; i < str.size(); i++)
  2. {
  3. if (str[i] >= 'A' && str[i] <= 'Z' && str[i] != 'I')
  4. str[i] += 'a' - 'A';
  5. }

3、把原文中所有独立的 can youcould you 对应地换成 I canI could—— 这里“独立”是指被空格或标点符号分隔开的单词;把原文中所有独立的 I 和 me 换成 you

这里有一个小问题是把"can you"替换成"I can"后,进行"I"->"you"时又重新改变了。

解决办法:在第一步时先用‘A’替换'I',全部完成后再替换成'I'

思路来源:L1-064 估值一亿的AI核心代码 (20分)(极短代码)_给个选择的博客-CSDN博客

  1. string replace(string str, string ch, string op)
  2. {
  3. for (int i = 0;; i++)
  4. {
  5. int l = str.find(ch, i), r = l + ch.size() - 1;
  6. if (l == -1)
  7. break;
  8. if (l == 0 && (str[r + 1] == ' ' || If(str[r + 1]))) //位于最前面
  9. {
  10. str.replace(l, r - l + 1, op);
  11. }
  12. else if (r == str.size() - 1 && (str[l - 1] == ' ' || If(str[l - 1]))) //位于最后
  13. {
  14. str.replace(l, r - l + 1, op);
  15. }
  16. else if ((str[r + 1] == ' ' || If(str[r + 1])) && (str[l - 1] == ' ' || If(str[l - 1]))) //位于中间
  17. {
  18. str.replace(l, r - l + 1, op);
  19. }
  20. }
  21. return str;
  22. }
  23. str = replace(str, "can you", "A can");
  24. str = replace(str, "could you", "A could");
  25. str = replace(str, "I", "you");
  26. str = replace(str, "me", "you");

4、把原文中所有的问号 ? 换成惊叹号 !

  1. for (int i = 0; i < str.size(); i++)
  2. {
  3. if (str[i] == '?')
  4. str[i] = '!';
  5. if (str[i] == 'A')
  6. str[i] = 'I';
  7. }

三、代码

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. bool If(char op) //判断op是否为符号
  4. {
  5. if (op == '0')
  6. return false;
  7. if (op >= 'a' && op <= 'z')
  8. return false;
  9. if (op >= 'A' && op <= 'Z')
  10. return false;
  11. if (op >= '0' && op <= '9')
  12. return false;
  13. return true;
  14. }
  15. string replace(string str, string ch, string op)
  16. {
  17. for (int i = 0;; i++)
  18. {
  19. int l = str.find(ch, i), r = l + ch.size() - 1;
  20. if (l == -1)
  21. break;
  22. if (l == 0 && (str[r + 1] == ' ' || If(str[r + 1]))) //位于最前面
  23. {
  24. str.replace(l, r - l + 1, op);
  25. }
  26. else if (r == str.size() - 1 && (str[l - 1] == ' ' || If(str[l - 1]))) //位于最后
  27. {
  28. str.replace(l, r - l + 1, op);
  29. }
  30. else if ((str[r + 1] == ' ' || If(str[r + 1])) && (str[l - 1] == ' ' || If(str[l - 1]))) //位于中间
  31. {
  32. str.replace(l, r - l + 1, op);
  33. }
  34. }
  35. return str;
  36. }
  37. int main()
  38. {
  39. int n;
  40. cin >> n;
  41. getchar();
  42. while (n--)
  43. {
  44. string str;
  45. getline(cin, str);
  46. cout << str << endl;
  47. for (int i = 0; i < str.size(); i++)
  48. {
  49. if (str[i] == ' ')
  50. {
  51. int j = i;
  52. while (j + 1 < str.size() && str[j + 1] == ' ')
  53. j++;
  54. str.erase(i, j - i);
  55. i = j;
  56. }
  57. }
  58. if (str.size() != 0 && str[0] == ' ')
  59. str.erase(0, 1);
  60. if (str.size() != 0 && str[str.size() - 1] == ' ')
  61. str.erase(str.size() - 1, 1);
  62. for (int i = 1; i < str.size(); i++)
  63. {
  64. if (If(str[i]) && str[i - 1] == ' ')
  65. {
  66. str.erase(i - 1, 1);
  67. i--;
  68. }
  69. }
  70. for (int i = 0; i < str.size(); i++)
  71. {
  72. if (str[i] >= 'A' && str[i] <= 'Z' && str[i] != 'I')
  73. str[i] += 'a' - 'A';
  74. }
  75. str = replace(str, "can you", "A can");
  76. str = replace(str, "could you", "A could");
  77. str = replace(str, "I", "you");
  78. str = replace(str, "me", "you");
  79. for (int i = 0; i < str.size(); i++)
  80. {
  81. if (str[i] == '?')
  82. str[i] = '!';
  83. if (str[i] == 'A')
  84. str[i] = 'I';
  85. }
  86. cout << "AI: " << str << endl;
  87. }
  88. return 0;
  89. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/277191
推荐阅读
相关标签
  

闽ICP备14008679号