当前位置:   article > 正文

PTA 估值一亿的AI核心代码 (STL:各种string)_估值一亿的ai核心代码pta

估值一亿的ai核心代码pta

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

分析:

问题极为复杂就划分为7个小问题,分别进行求解操作,过程中用到了string头文件中的erase,replace,find,rfind等多个函数,STL的使用可以较好简化代码。

代码:

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5. int n;
  6. cin >> n;
  7. getchar();
  8. while (n--)
  9. {
  10. string s;
  11. getline(cin, s);
  12. //1)
  13. cout << s << endl;
  14. //2)
  15. while (s[0] == ' ')s.erase(s.begin());
  16. while (s[s.length() - 1] == ' ')s.erase(s.end() - 1);
  17. for (int i = 0; i<s.length(); i++)
  18. {
  19. if (s[i] == ' ')
  20. {
  21. while (s[i + 1] == ' ')s.erase(s.begin() + i + 1);
  22. if (s[i + 1]<'0' || s[i + 1]>'9'&&s[i + 1]<'A' || s[i + 1]>'Z'&&s[i + 1]<'a' || s[i + 1]>'z')
  23. {
  24. s.erase(s.begin() + i);
  25. }
  26. }
  27. }
  28. //3)
  29. for (int i = 0; i<s.length(); i++)
  30. {
  31. if (s[i] >= 'A'&&s[i] <= 'Z'&&s[i] != 'I')s[i] -= 'A' - 'a';
  32. }
  33. //4)
  34. for (int beg = 0;; beg++)
  35. {
  36. beg = s.find("can you", beg);
  37. if (beg == -1)break;
  38. if ((beg == 0 || s[beg - 1]<'0' || s[beg - 1]>'9'&&s[beg - 1]<'A' || s[beg - 1]>'Z'&&s[beg - 1]<'a' || s[beg - 1]>'z') && (beg + 7 == s.length() || s[beg + 7]<'0' || s[beg + 7]>'9'&&s[beg + 7]<'A' || s[beg + 7]>'Z'&&s[beg + 7]<'a' || s[beg + 7]>'z'))
  39. {
  40. s.replace(beg, 7, "A can");
  41. }
  42. }
  43. for (int beg = 0;; beg++)
  44. {
  45. beg = s.find("could you", beg);
  46. if (beg == -1)break;
  47. if ((beg == 0 || s[beg - 1]<'0' || s[beg - 1]>'9'&&s[beg - 1]<'A' || s[beg - 1]>'Z'&&s[beg - 1]<'a' || s[beg - 1]>'z') && (beg + 9 == s.length() || s[beg + 9]<'0' || s[beg + 9]>'9'&&s[beg + 9]<'A' || s[beg + 9]>'Z'&&s[beg + 9]<'a' || s[beg + 9]>'z'))
  48. {
  49. s.replace(beg, 9, "A could");//由于后面还要处理'I'这里先用'A'代替'I',最后在变成'I'
  50. }
  51. }
  52. //5)
  53. for (int beg = 0;; beg++)
  54. {
  55. beg = s.find("I", beg);
  56. if (beg == -1)break;
  57. if ((beg == 0 || s[beg - 1]<'0' || s[beg - 1]>'9'&&s[beg - 1]<'A' || s[beg - 1]>'Z'&&s[beg - 1]<'a' || s[beg - 1]>'z') && (beg + 1 == s.length() || s[beg + 1]<'0' || s[beg + 1]>'9'&&s[beg + 1]<'A' || s[beg + 1]>'Z'&&s[beg + 1]<'a' || s[beg + 1]>'z'))
  58. {
  59. s.replace(beg, 1, "you");
  60. }
  61. }
  62. for (int beg = 0;; beg++)
  63. {
  64. beg = s.find("me", beg);
  65. if (beg == -1)break;
  66. if ((beg == 0 || s[beg - 1]<'0' || s[beg - 1]>'9'&&s[beg - 1]<'A' || s[beg - 1]>'Z'&&s[beg - 1]<'a' || s[beg - 1]>'z') && (beg + 2 == s.length() || s[beg + 2]<'0' || s[beg + 2]>'9'&&s[beg + 2]<'A' || s[beg + 2]>'Z'&&s[beg + 2]<'a' || s[beg + 2]>'z'))
  67. {
  68. s.replace(beg, 2, "you");
  69. }
  70. }
  71. //6)
  72. for (int i = 0; i<s.length(); i++)
  73. {
  74. if (s[i] == '?')s[i] = '!';
  75. if (s[i] == 'A')s[i] = 'I';
  76. }
  77. //7)
  78. cout << "AI: " << s << endl;
  79. }
  80. return 0;
  81. }

 

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

闽ICP备14008679号