2018년 3월 30일 금요일

[C++] HackerRank Super Reduced String

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

It's a good challenge to practise C++ String.

push_back(), pop_back(), empty()  are useful functions for this problem.


*/



#include <bits/stdc++.h>

using namespace std;

string super_reduced_string(string s){
    string result;
    int cur=0;
   
    for(int i=0;i<s.length();i++) {
        if(cur == 0) {
            result.push_back(s[i]);
            cur++;
        } else if(result[cur-1] == s[i]) {
            result.pop_back();
            cur--;
        } else {
            result.push_back(s[i]);
            cur++;
        }
    }
   
    if(result.empty()) {
        return "Empty String";
    }
   
    return result;
}

int main() {
    string s;
    cin >> s;
    string result = super_reduced_string(s);
    cout << result << endl;
    return 0;
}

댓글 없음:

댓글 쓰기