每一个字符的输入都应对不同的状态,参考一个自动机的题解,逐个输入字符串的每个字符,将符合需求的状态的字符进行拼接,最后输出字符。

#include <stdio.h>
#include <iostream>

using namespace std;

void fun(const string& str)
{
    int state = 0;
    char cur;
    char last = str[0];

    string rst = "";
    rst += str[0];

    for(int i=1;i<str.size();i++)
    {
        cur = str[i];
        switch(state)
        {
            case 0:
                {
                    if(cur == last)
                        state = 1;
                    else
                        state = 0;
                    break;
                }

            case 1:
                {
                    if(cur == last)
                    {
                        continue;
                    }
                    else
                    {
                        state = 2;
                    }
                    break;
                }

            case 2:
                {
                    if(cur == last)
                    {
                        continue;
                    }else
                    {
                        state = 0;
                    }
                    break;
                }

            default:
                break;
        }

        rst += cur;
        last = cur;
    }

    cout << rst << endl;
}
int main()
{
    int n;
    cin >> n;
    while(n--)
    {
        string str;
        cin >> str;
        fun(str);
    }
    return 0;
}