"you"时又重新改变了。解决办法:在第一步时先用‘A’替换'I',全部完成后再替换成'I'_估值一亿的ai核心代码">
赞
踩
1、消除原文中多余空格:把相邻单词间的多个空格换成 1 个空格,把行首尾的空格全部删掉,把标点符号前面的空格删掉;
判断是否为符号
- bool If(char op) //判断op是否为符号
- {
- if (op == '0')
- return false;
- if (op >= 'a' && op <= 'z')
- return false;
- if (op >= 'A' && op <= 'Z')
- return false;
- if (op >= '0' && op <= '9')
- return false;
- return true;
- }
- for (int i = 0; i < str.size(); i++)
- {
- if (str[i] == ' ')
- {
- int j = i;
- while (j + 1 < str.size() && str[j + 1] == ' ')
- j++;
- str.erase(i, j - i);
- i = j;
- }
- }
- if (str.size() != 0 && str[0] == ' ')
- str.erase(0, 1);
- if (str.size() != 0 && str[str.size() - 1] == ' ')
- str.erase(str.size() - 1, 1);
- for (int i = 1; i < str.size(); i++)
- {
- if (If(str[i]) && str[i - 1] == ' ')
- {
- str.erase(i - 1, 1);
- i--;
- }
- }

2、把原文中所有大写英文字母变成小写,除了 I
;
- for (int i = 0; i < str.size(); i++)
- {
- if (str[i] >= 'A' && str[i] <= 'Z' && str[i] != 'I')
- str[i] += 'a' - 'A';
- }
3、把原文中所有独立的 can you
、could you
对应地换成 I can
、I could
—— 这里“独立”是指被空格或标点符号分隔开的单词;把原文中所有独立的 I
和 me
换成 you
;
这里有一个小问题是把"can you"替换成"I can"后,进行"I"->"you"时又重新改变了。
解决办法:在第一步时先用‘A’替换'I',全部完成后再替换成'I'
思路来源:L1-064 估值一亿的AI核心代码 (20分)(极短代码)_给个选择的博客-CSDN博客
- string replace(string str, string ch, string op)
- {
- for (int i = 0;; i++)
- {
- int l = str.find(ch, i), r = l + ch.size() - 1;
- if (l == -1)
- break;
- if (l == 0 && (str[r + 1] == ' ' || If(str[r + 1]))) //位于最前面
- {
- str.replace(l, r - l + 1, op);
- }
- else if (r == str.size() - 1 && (str[l - 1] == ' ' || If(str[l - 1]))) //位于最后
- {
- str.replace(l, r - l + 1, op);
- }
- else if ((str[r + 1] == ' ' || If(str[r + 1])) && (str[l - 1] == ' ' || If(str[l - 1]))) //位于中间
- {
- str.replace(l, r - l + 1, op);
- }
- }
- return str;
- }
-
-
-
- str = replace(str, "can you", "A can");
- str = replace(str, "could you", "A could");
- str = replace(str, "I", "you");
- str = replace(str, "me", "you");
-

4、把原文中所有的问号 ?
换成惊叹号 !
- for (int i = 0; i < str.size(); i++)
- {
- if (str[i] == '?')
- str[i] = '!';
- if (str[i] == 'A')
- str[i] = 'I';
- }
- #include <bits/stdc++.h>
- using namespace std;
-
- bool If(char op) //判断op是否为符号
- {
- if (op == '0')
- return false;
- if (op >= 'a' && op <= 'z')
- return false;
- if (op >= 'A' && op <= 'Z')
- return false;
- if (op >= '0' && op <= '9')
- return false;
- return true;
- }
-
- string replace(string str, string ch, string op)
- {
- for (int i = 0;; i++)
- {
- int l = str.find(ch, i), r = l + ch.size() - 1;
- if (l == -1)
- break;
- if (l == 0 && (str[r + 1] == ' ' || If(str[r + 1]))) //位于最前面
- {
- str.replace(l, r - l + 1, op);
- }
- else if (r == str.size() - 1 && (str[l - 1] == ' ' || If(str[l - 1]))) //位于最后
- {
- str.replace(l, r - l + 1, op);
- }
- else if ((str[r + 1] == ' ' || If(str[r + 1])) && (str[l - 1] == ' ' || If(str[l - 1]))) //位于中间
- {
- str.replace(l, r - l + 1, op);
- }
- }
- return str;
- }
-
- int main()
- {
- int n;
- cin >> n;
- getchar();
- while (n--)
- {
- string str;
- getline(cin, str);
- cout << str << endl;
-
- for (int i = 0; i < str.size(); i++)
- {
- if (str[i] == ' ')
- {
- int j = i;
- while (j + 1 < str.size() && str[j + 1] == ' ')
- j++;
- str.erase(i, j - i);
- i = j;
- }
- }
- if (str.size() != 0 && str[0] == ' ')
- str.erase(0, 1);
- if (str.size() != 0 && str[str.size() - 1] == ' ')
- str.erase(str.size() - 1, 1);
- for (int i = 1; i < str.size(); i++)
- {
- if (If(str[i]) && str[i - 1] == ' ')
- {
- str.erase(i - 1, 1);
- i--;
- }
- }
-
- for (int i = 0; i < str.size(); i++)
- {
- if (str[i] >= 'A' && str[i] <= 'Z' && str[i] != 'I')
- str[i] += 'a' - 'A';
- }
-
- str = replace(str, "can you", "A can");
- str = replace(str, "could you", "A could");
- str = replace(str, "I", "you");
- str = replace(str, "me", "you");
-
- for (int i = 0; i < str.size(); i++)
- {
- if (str[i] == '?')
- str[i] = '!';
- if (str[i] == 'A')
- str[i] = 'I';
- }
- cout << "AI: " << str << endl;
- }
- return 0;
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。