当前位置:   article > 正文

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

l1-064 估值一亿的ai核心代码

两要素:

1.多余空格的处理

2.读取并更改指定字符串

还得多学习

  1. #include<iostream>
  2. #include<algorithm>
  3. #include<cctype>
  4. using namespace std;
  5. string str;
  6. bool check(int x,int y,int len) //x为单词前一位,y为单词后一位,len为字符串长度
  7. {
  8. if((x < 0|| str[x]==' '||ispunct(str[x]))&&(y>=len||str[y]==' '||ispunct(str[y])))
  9. return true;
  10. return false;
  11. }
  12. int main()
  13. {
  14. int T;
  15. cin >> T;
  16. getchar();
  17. while(T--)
  18. {
  19. getline(cin,str);
  20. cout << str << endl;
  21. int i = 0, j = str.length() - 1;
  22. //去掉首、尾空格
  23. while(str[i] == ' ') i++;
  24. while(str[j] == ' ') j--;
  25. //处理str字符串
  26. int cot = 0;
  27. for(;i <= j;i ++, cot ++) //利用双指针处理多余空格问题
  28. {
  29. if(isupper(str[i]) && str[i] != 'I')
  30. str[cot] = str[i] + 32;
  31. else if(str[i] == '?')
  32. str[cot] = '!';
  33. else if(str[i] == ' ')
  34. {
  35. str[cot] = ' ';
  36. while(str[++i] == ' '); //判断下一个字符是否为空格
  37. if(ispunct(str[i])) //符号前不需要空格
  38. cot --;
  39. i --;
  40. }
  41. else // 不修改
  42. str[cot] = str[i];
  43. }
  44. string goal="";
  45. for(int i = 0;i < cot;i ++)
  46. {
  47. if(str[i] == 'I' && check(i - 1,i + 1, cot))
  48. goal += "you";
  49. else if(str.substr(i,2) == "me" && check(i - 1, i + 2, cot))
  50. goal += "you",i += 1;
  51. else if(str.substr(i,7) == "can you" && check(i - 1,i + 7, cot))
  52. goal += "I can", i += 6;
  53. else if(str.substr(i,9) == "could you" && check(i - 1,i + 9, cot))
  54. goal += "I could",i += 8;
  55. else goal += str[i];
  56. }
  57. goal = "AI: " + goal;
  58. cout << goal << endl;
  59. }
  60. return 0;
  61. }

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

闽ICP备14008679号