Byte beating - written test (clever editing)

Disclaimer: This article is written self-study, pointed out that if the error also hope, thank you ^ - ^ https://blog.csdn.net/weixin_43871369/article/details/90374461

My name sledgehammer, an editor for a publishing house. I was responsible for proofreading the manuscript submission to English, this work is very annoying, because many have to fix spelling mistakes every day. But good people can always find the truth in the ordinary work. I found a shortcut to find misspelled:

1. The same three letters together, must be misspelled, the removal of one thousand million: such helllo -> hello

2. Two of the same letters (the AABB type) are linked together, it must be spelling errors, remove a second pair thousand million letters: such helloo -> hello

3. The above rules take precedence "left to right" match, that if it is AABBCC, although AABB and BBCC are misspellings, should give priority to repair the AABB, the result is AABCC

I especially meow is a genius! I studied at Lanxiang had excavator and programming, according to this principle wrote an automatic verifier, taking off from work efficiency. Pretty soon, I will serve as CEO, became chairman of the board, marry white Formica, took to the pinnacle of life, think about all a little excited about it!

……

I never thought I was fired, and before leaving the boss said to me:. "Doing things to be dedicated, diligent, the sub-points, if the person line, line by line, line by line dry trekking trekking; and if not, dry No line of his party, his party will not do trekking not. "I am now the whole person booming trance ......

Please listen to the question: Please realize sledgehammer automatic proofreading program

输入描述:
第一行包括一个数字N,表示本次用例包括多少个待校验的字符串。

后面跟随N行,每行为一个待校验的字符串。

输出描述:
N行,每行包括一个被修复后的字符串。
输入例子1:
2
helloo
wooooooow

输出例子1:
hello
woow
//O(n)时间复杂度
#include<iostream>
#include<string>
#include<vector>
using namespace std;
string getans(string s)
{
    string res="";
    int size=static_cast<int>(s.size());
    int index=0;
    bool front2=false;//前面已经出现了重复数超过2个的情况
    while(index<size)
    {
        if(index+1<size)
        {
           if(s[index+1]!=s[index])
          {
            res+=s[index];
            ++index;
            front2=false;
          }
          else{
            if(!front2)
            {
                res+=s[index];
                res+=s[index+1];
                int i=index+2;
                while(i<size&&s[i]==s[index+1]) ++i;
                index=i;
                front2=true;
            }
            else{
                res+=s[index];
                int j=index+1;
                while(j<size&&s[j]==s[index]) ++j;
                index=j;
                front2=false;
            }
           }
        }
        else
        {
            res+=s[index];
            break;
        }
    }
    return res;
}
int main()
{
    int n;
    cin>>n;
    vector<string>str(n);
    for(int i=0;i<n;++i)
    {
        cin>>str[i];
    }
    for(int i=0;i<n;++i)
        cout<<getans(str[i])<<endl;
    return 0;
}

 

Guess you like

Origin blog.csdn.net/weixin_43871369/article/details/90374461