Super Reduced String

https://www.hackerrank.com/challenges/reduced-string/problem

He wants to reduce the string to its shortest length by doing a series of operations in which he selects a pair of adjacent lowercase letters that match, and then he deletes them. 

aaabccddd->abccddd->abddd->abd

aabb->""

#include <iostream>
#include <string>
using namespace std;
int main()
{
        string s;
        cin >> s;
        for (int i = 0; i < (int)(s.length() - 1); i++){
            if (!s.empty()&&s[i] == s[i + 1]){
                s.erase(i, 2);
                i = -1;//后面I还要加1,从头开始
            }
        }
        if (s.empty()){
            cout << "EMPTY" << endl;
        }
        else{
            cout << s << endl;
        }
        return 0;
    }

猜你喜欢

转载自www.cnblogs.com/newdawnfades/p/9142711.html