Problem
You are given a binary string of length .
You can perform the following type of operation on the string :
- Choose two different indices and ;
- Change and to . Here represents the bitwise XOR operation.
Find the minimum number of operations required to convert the given string into a palindrome.
Input Format
- First line of the input contains , the number of test cases. Then the test cases follow.
- First line of each test case contains an integer denoting the size of the string.
- Second line of each test case contains a binary string of length containing s and s only.
Output Format
For each test case, print the minimum number of operations required to make the string a palindrome.
Constraints
- Sum of over all test cases does not exceeds .
Sample 1:
4 5 11011 7 0111010 1 1 4 1100
0 1 0 1
Explanation:
Test Case : The given string is already a palindrome. So, no operation is required. The answer for the case is .
Test Case : The given string is not a palindrome.
- Choose the indices and . Now, . Thus, we set and equal to .
After the operation, the resulting string is which is a palindrome. The number of operations required is .
Test Case : The given string is already a palindrome. So, no operation is required. The answer for the case is .
Test Case : The given string is not a palindrome.
- Choose the indices and . Now, . Thus, we set and equal to .
After the operation, the resulting string is which is a palindrome. The number of operations required is .
Program :
#include <stdio.h>
int main(void) {
// your code goes here
int t;
scanf("%d",&t);
while(t--){
int n;
scanf("%d",&n);
char s[n+1];
scanf("%s",s);
int c=0;
for(int i=0, j=n-1; i<j; i++, j--){
if(s[i]!=s[j])
c++;
}
printf("%d\n",(c+1)/2);
}
return 0;
}
0 Comments