0w1

GCJ 2016 QR B. Revenge of the Pancakes ( Greedy )

Dashboard - Qualification Round 2016 - Google Code Jam
This can be solved by DP, too. However the greedy approach is easy to come up with and shorter in code.
With some observation, it is easy to find that the flips will be made from left to right, for all consecutive identical symbols. So we will keep flipping consecutive same symbol pancakes that starts from the top, until the sequence is valid.

#include <bits/stdc++.h>
using namespace std;
const int MAXS = 100 + 2;

string s;

void solve(){
    int ans = 0;
    for( int i = 1; i < s.size(); ++i )
        if( s[ i ] != s[ i - 1 ] )
            ++ans;
    ans += ( s[ (int)s.size() - 1 ] == '-' );
    cout << ans << "\n";
}

int main(){
    int T; cin >> T;
    for( int kase = 1; kase <= T; ++kase ){
        cin >> s;
        printf("Case #%d: ", kase);
        solve();
    }
    return 0;
}