# Blue bridge: the price of perfection

## subject

Problem description

Palindrome string is a special string, which is read from left to right and right to left. Bruce Lee thinks the palindrome string is perfect. Now I will give you a string, which is not necessarily palindrome. Please calculate the minimum number of exchanges to make the string a perfect palindrome string.

Exchange is defined as the exchange of two adjacent characters

For example, mamad

First exchange ad: mamda

Second exchange MD: madma

The third exchange Ma: Madam (palindrome! Perfect! )

Input format

The first line is an integer N, representing the length of the next string (N < = 8000)

The second line is a string of length N. It contains only lowercase letters

Output format

If possible, output the minimum number of exchanges.

Otherwise, output Impossible

sample input

5

mamad

sample output

3

## Title Analysis

(1) There are two cases of Impossibility:

When n is an odd number, if there is already a character with an odd number of times and a character with an odd number of times, then it cannot form a palindrome string;

When n is an even number, a palindrome string cannot be formed as long as the number of times a character appears is an odd number.

(2) The topic requires the output of the minimum number of exchanges, so how to ensure the minimum number of exchanges?

If n is an even number, the first character will be found from the first character, from the back to the front. If it is found, the found character will be exchanged to the last position. In the next traversal, you can ignore the two characters that have been exchanged just now; in the next traversal, you can start from the second character, and start from the penultimate character, and execute the above Same operation;

If n is an odd number and the character with odd occurrence times is found in a certain position of the string, we do not need to exchange the secondary character to the middle position now, but first calculate the number of times it needs to exchange to the middle position, then add it to cnt, exchange the remaining characters to symmetry, and then exchange the character.

Try to think that if the first character is an odd number of characters, then swap it to the middle position, and then swap other characters one more time each time. This is actually a general rule.

# Code (but I still don't understand some places)

import java.util.Scanner; public class The price of perfection { public static void main(String[] args){ Scanner sc=new Scanner(System.in); int n=sc.nextInt(); char arr[]=sc.next().toCharArray(); if (palindrome(arr,0,n-1)){//Call the function, the return value of the function is boolean type, so you can use if to judge that the input is 0 and, n-1, that is, 0 and 0-1 are interchanged, from 0 to n-1 System.out.println(cnt); }else{ System.out.println("Impossible"); } } private static int cnt=0;//Exchange times private static boolean haveMiddle=false;//Whether there is an intermediate number, i.e. whether it is an odd number private static boolean palindrome(char arr[],int a,int b){ //Why didn't I add this, I input it wrong, I reported it wrong, I added this, input it right? if (b <= a)//mamad, for example, a=0,b=4,Not less than { return true; } //Traverse from the last position for(int i=b;i>a;i--){//Traverse from the back if(arr[a]==arr[i]){//If you find the first character after it exchangeTo(arr,i,b);//Exchange, i subscript and b subscript are exchanged, that is to say, the first and the beginning of the same number are found, and this character is exchanged to the last position cnt +=getExchangeTime(i,b);//cnt is the difference between the sum of functions, I and the last, i.e. the exchange converted to the last position return palindrome(arr,a+1,b-1);//In the next traversal, you can start from the second character and start from the last two characters, regardless of the two characters you just exchanged } } //If an intermediate character appears, it is an odd number if(!haveMiddle){ haveMiddle=true; cnt +=getExchangeTime(a,arr.length/2); return palindrome(arr,a+1,b); } return false; } private static void exchangeTo(char arr[],int a,int b){//Exchange function with null return value char temp=arr[a]; for(int i=a;i<b;i++){ arr[i]=arr[i+1];//Mobile } arr[b]=temp;//Make the last equal to the value in which position } private static int getExchangeTime(int a,int b){//A function that returns a number of positive type return b-a; } //The swap starts at position 0, }

# The test point is greedy algorithm. I'm looking for some information

https://blog.csdn.net/Africa_South/article/details/87924966

https://blog.csdn.net/qq_32400847/article/details/51336300

https://blog.csdn.net/effective_coder/article/details/8736718