Perfect price, perfect price
Problem description
A return string is a special string that reads from left to right and from right to left. Xiao Long believes that the reply string is perfect. Now, I will give you a string. It is not necessarily a return string. Please calculate the minimum number of exchanges to make the string a perfect return string.
Two adjacent characters are exchanged.
For example, mamad
First switching ad: mamda
Second exchange md: madma
The third ma exchange: madam (reply! Perfect !)
Input Format
The first line is an integer N, indicating the length of the subsequent string (N <= 8000)
The second line is a string with a length of N. It only contains lowercase letters.
Output Format
If possible, the minimum number of exchanges is output.
Otherwise, output Impossible.
Sample Input
5
Mamad
Sample output
3
Import java. io. File;
Import java. io. FileNotFoundException;
Import java. util. collections;
Public class Main {
Public static void main (String [] args) throws Exception {
Int n, I;
// File stream output pipeline SC = new pipeline (new File ("test. in "));
While (SC. hasNext ())
{
N = Integer. parseInt (SC. nextLine ());
// Use the array arr [] to reserve the number of inputs,
String str = SC. nextLine ();
Char arr [] = str. toCharArray ();
// Use ch to retain different letters that appear once
Char ch = '0 ';
// Use the marker to record the number of occurrences of different letters
Int Limit = 0;
// Use the count [] array to record the number of occurrences of each letter
Int index;
Int count [] = new int [26];
For (I = 0; I <arr. length; I ++)
{
Index = arr [I]-'A ';
Count [index] ++;
}
Show (count, ch, clerk, arr, n );
}
}
Private static void show (int [] count, char ch, int substring, char arr [], int n)
{
// TODO Auto-generated method stub
// Start to calculate the number of occurrences of different letters and record them.
For (int j = 0; j <count. length; j ++)
{
If (count [j] % 2! = 0)
{Region ++;
Ch = (char) (j + 'A ');
}
}
// If there are more than two or more entries, this certainly cannot constitute a retrieval Array
If (latency> 1)
{
System. out. println ("Impossible ");
}
// If there is only one different letter, perform the bubble search. Call find () and return the result;
Else
{
Int result = find (arr, ch, n );
System. out. println (result );
}
}
Private static int find (char [] arr, char ch, int n)
{
// TODO Auto-generated method stub
// The Idea Of searching is to use the bubble method of thinking and scanning from the left and right sections at the same time to find the same break and record its position.
// Determine the loop range
Int I, count = 0, j, k;
For (I = 0; I <n/2; I ++)
{
// Scan from the left to the right. The first character is a unique letter, traversing and comparing.
If (arr [I] = ch)
{
For (j = 0; j <n-1-I; j ++)
{
// Construct the number of input records when the first letter is the same as the last one (arr [n-i-1] is fixed) and its position is recorded for exchange preparation, not encountered to continue, next scan
If (arr [j] = arr [n-I-1]) break;
}
Count + = j-I;
// After recording the next position, start to switch the number of other positions and start scanning from the right to the left,
// If dmpaa ---> (locate location a in the third position and the last one is the same, so location a to Location d) dmmma ----> ddmma ends,
For (k = j; k> I; k --)
{
Arr [k] = arr [k-1];
}
// Here is the number of input files. It is the same as the last letter to the first position. If it is found, the next scan is performed.
Arr [I] = arr [n-I-1];
}
// Scan from the right,
Else
{
For (j = n-1-I; j> = I; j --)
{
If (arr [j] = arr [I]) break;
}
Count + = n-1-I-j;
// Start from the right n-1-i to the end of j, so scan back from left, start from j
For (k = j; k <n-1-I; k ++)
{
Arr [k] = arr [k + 1];
}
Arr [n-I-1] = arr [I];
}
}
Return count;
}
}