万万没想到之聪明的编辑

#include 
#include 
using namespace std;
 
string getStr(string input) {
    int left = 0;
    int right = 1;
    string ans1 = "";
    while (right < (int)input.size()) {
            if (input[left] == input[right]) {
                right++;
            }
            else if(input[left] != input[right]) {
                if (right - left >= 2) {
                    ans1 += input.substr(left, 2);
                }
                else {
                    ans1 += input[left];
                }
                left = right;
                right++;
            }
     }
 
    int xleft = 0;
    int xright = 1;
    ans1 = ans1 + '\r';
    string ans = "";
    bool flag = false;
    while (xright < (int)ans1.size()) {
        if (ans1[xleft] == ans1[xright]) {
            xright++;
        }
        else if (ans1[xleft] != ans1[xright]) {
            if (xright - xleft == 2) {
                if (flag) {
                    ans += ans1.substr(xleft, 1);
                }
                else {
                    ans += ans1.substr(xleft, 2);
                }
                flag = !flag;
            }
            else {
                ans += ans1[xleft];
                flag = false;
            }
            xleft = xright;
            xright++;
        }
         
    }
    return ans;
}
 
int main() {
    int n;
    cin >> n;
    string input;
    for (int i = 0; i < n; ++i) {
        cin >> input;
        input += '\r';
        cout << getStr(input) << endl;
    }
}

 

你可能感兴趣的:(万万没想到之聪明的编辑)