小米字符串第一题,状态机思路
这题用状态机比较好解决,代码看起来清晰很多
#include <iostream> #include <vector> #include <string> #include <list> #include <queue> #include <unordered_map> #include <algorithm> using namespace std; string to_upper(string& s) { for (auto& c : s) { c = toupper(c); } return s; } int main() { string s; while (cin >> s) { int state = 0; vector<string> v; int i, j; for (i = 0, j = 0; j < s.length(); j++) { if (s[j] == '.') { state = 0; v.push_back(s.substr(i, j - i)); i = j + 1; } else if (isupper(s[j])) { if (state == 0) state = 1; else if (state == 1) state = 3; else if (state == 2 || state == 4 || state == 5) { v.push_back(s.substr(i, j - i)); state = 1; i = j; } } else if (islower(s[j])) { if (state == 0) state = 5; else if (state == 1) state = 2; else if (state == 3) { v.push_back(s.substr(i, j - i - 1)); state = 2; i = j - 1; } else if (state == 4) { v.push_back(s.substr(i, j - i)); state = 5; i = j; } } else if (isdigit(s[j])) { if (state == 0) state = 4; else if (state != 4) { v.push_back(s.substr(i, j - i)); state = 4; i = j; } } } if (state != 0) v.push_back(s.substr(i, j - i)); string res = "_"; for (auto& e : v) { res += to_upper(e), res += "_"; } cout << res << endl; } return 0; }#小米#