Problem
Ada has an array of N crayons, some crayons are pointing upwards and some downwards. Ada thinks that an array of crayons is beautiful if all the crayons are pointing in the same direction.
In one step you can flip any segment of consecutive crayons. After flipping a segment, all crayons pointing downwards will point upwards and visceversa
What is the minimum number of steps to make the array of crayons beautiful?
Input
The first line of the input contains T the number of test cases. Each test case is described in one line containing a string S of N characters, the i-th character is 'U' if the i-th crayon is pointing upwards and 'D' if it is pointing downwards.
Output
For each test case, output a single line containing the minimum number of flips needed to make all crayons point to the same direction.
Constraints
- 1 ≤ T ≤ 3000
- 1 ≤ N ≤ 50
Sample 1:
1 UUDDDUUU
1
Explanation:
Example case 1. In one step we can flip all the crayons pointing downwards
Program :
#include <stdio.h>
int main(void) {
// your code goes here
int t;
scanf("%d",&t);
while(t--)
{
int c=0,i;
char s[50];
scanf("%s",s);
for(i=1;s[i]!='\0';i++)
{
if(s[i-1]!=s[i])
{
c++;
}
}
printf("%d",((c/2)+(c%2)));
printf("\n");
}
return 0;
}
0 Comments