题解 | #万万没想到之聪明的编辑#

万万没想到之聪明的编辑

https://www.nowcoder.com/practice/42852fd7045c442192fa89404ab42e92

#include "string"
#include "deque"
#include "vector"
#include "iostream"

using namespace std;
//状态0  相同push   不同push且++
//状态1  相同no   不同push且++
//状态2  相同no   不同push且=0(到状态0)
int main()
{
    //int value[] = {1, 2, 5};
    int nLine;
    string strTemp;
    vector<string> strOutList;
    cin >> nLine;
    while(nLine--)
    {
        char letterOld = ' ';
        int count = 0;
        cin >> strTemp;
        letterOld = strTemp[0];
        string strOut;
        strOut.push_back(strTemp[0]);
        for (int i = 1; i < strTemp.size(); i++)
        {
            switch(count)
            {
                case 0:        //状态0
                    if (letterOld == strTemp[i])
                        count++;
                    strOut.push_back(strTemp[i]);
                    letterOld = strTemp[i];
                    break;
                case 1:        //状态1
                    if (letterOld != strTemp[i])
                    {
                        count++;
                        strOut.push_back(strTemp[i]);
                    }
                    letterOld = strTemp[i];
                    break;
                case 2:        //状态2
                    if (letterOld != strTemp[i])
                    {
                        count = 0;
                        strOut.push_back(strTemp[i]);
                    }
                    letterOld = strTemp[i];
                    break;
            }
        }
        strOutList.push_back(strOut);
    }
    for (int i = 0; i < strOutList.size(); i++)
        cout << strOutList.at(i) << endl;
    strOutList.clear();
    return 0;
}

//暴力解法
#if 0
string findTri(string str)
{
    int num = str.size();
    char old = str[0];
    int count = 1;
    string strout = "";
    strout.push_back(str[0]);
    for (int i = 1; i < num; i++)
    {
        if (old == str[i])
            count++;
        else
            count = 1;
        if (count == 3)
            count = 2;
        else
        {
            strout.push_back(str[i]);
        }
            
        old = str[i];
    }
    return strout;
}

string findDouble(string str)
{
    int num = str.size();
    char old = str[0];
    int count = 1;
    string strout = "";
    strout.push_back(str[0]);
    bool flag = false;
    for (int i = 1; i < num; i++)
    {
        if (old == str[i])
        {
            if (flag)
            {
                i++;
                if (i < num)
                    ;
                else
                    break;
                flag = false;
            }
            else
            {
                strout.push_back(str[i]);
                i++;
                if (i < num)
                    ;
                else
                    break;
                flag = true;
            }
        }
        else
        {
            flag = false;
        }
        strout.push_back(str[i]);
        old = str[i];
    }
    return strout;
}

int main(int argv, char* argc[])
{
    int numLine;
    cin >> numLine;
    
    deque<string> wordList;
    for (int i = 0; i < numLine; i++)
    {
        string wordTemp;
        cin >> wordTemp;
        wordList.push_back(wordTemp);
    }
    //ut << numLine << endl;
    while(!wordList.empty())
    {
        string wordTemp = wordList.at(0);
        string wordTemp1 = findTri(wordTemp);
        string wordTemp2 = findDouble(wordTemp1);
        cout << wordTemp2 << endl;
        wordList.pop_front();
    }
    return 0;
}
#endif

全部评论

相关推荐

04-27 08:59
常州大学 Java
牛客139242382号:《两门以上汇编语言》
点赞 评论 收藏
分享
劝退式:感觉有人回才是不正常的
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务