Skip to content

Instantly share code, notes, and snippets.

@ravichandrae
Created February 19, 2015 00:29
Given a string containing the only symbols '+' or '-', how do we find the minimum number of changes to transform it to a chain. A chain of length N is the sequence of alternative + and - symbols.
#include <iostream>
#include <string>
using namespace std;
int main()
{
cin.sync_with_stdio(false);
int t;
cin >> t;
while(t--)
{
string input;
cin >> input;
int i, result = 0, result1 = 0;
char p1 = '+', p2 = '-';
for( i = 0; i < input.size(); i++ )
{
if( input[i] != p1 )
result++;
if( input[i] != p2 )
result1++;
p1 = (p1 == '+')? '-':'+';
p2 = (p2 == '+')? '-':'+';
}
cout << min(result,result1) << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment