The price of a basic exercise perfect

Source: Internet
Author: User

Time limit: 1.0s memory limit: 512.0MB The problem describes a palindrome string, which is a special string that reads from left to right and reads from right to left. Longron that palindrome is perfect. Now give you a string, it is not necessarily a palindrome, please calculate the minimum number of exchanges so that the string into a perfect palindrome string.
Interchange is defined as swapping two adjacent characters
such as Mamad
First time Exchange Ad:mamda
Second Exchange MD:MADMA
Third Exchange Ma:madam (palindrome!) Perfect! Input format the first line is an integer n, indicating the length of the next string (n <= 8000)
The second line is a string with a length of n. Only lowercase letter output format if possible, output a minimum number of interchanges.
Otherwise output impossible sample input 5
Mamad Sample Output 3

Figure 1, assuming that the symmetric position of the left a is the letter B in the initial string. First move the right of a to the right B position, and then move two B to the symmetrical position, with the first move the left B to the left a position, and then move two A to the symmetrical position, the number of moves is the same. First, the trailing pair of letters is not exchanged with the previous pair of letters that have been in place during the move. This is because, after moving a pair of letters, can only move in a single direction, if in the move process with the first pair of letters in exchange, then the equivalent of the first pair of letters moved in the opposite direction, and their original position is symmetrical, so that can not be symmetrical after moving. Therefore, the movement of two pairs of letters can be considered independent, and the total number of moves is the sum of the number of moves of two pairs of letters, and the number of moves of each pair of letters is independent of the possible position, so it doesn't matter which letter to move first.

(Fig. 1)

to ensure that the letters that are moved are not exchanged with the previously moved letters , we can start at the far left of the original string, select the pair of letters that are currently moving, and take the position of the letter as the final symmetrical position, which is another of the letters, Of course, select the right-most symmetrical position to begin with the same letter that appears first to the left. If the length of the string is an even number, then all letters appear to be even, the above process can be carried on, the last two pairs of letters in place, the final pair of letters in place at the same time, to get the result of palindrome string. If the length of the string is odd, it is possible to bump into the letter that should eventually be placed in the middle of the string in the process. According to the previous analysis, it is not possible to put it in the middle of a string immediately, because it does not guarantee that it will not be exchanged with subsequent letters. at this point we can turn to the letter on the right side of the symmetric position, as the current consideration of the pair, on the left of the current position to the right to find the first letter, to move.

The above algorithm has been basically clear, but also nearly small details, how do we know the current position of the letter is the end should be placed in the middle of the letter ? First of all, the letter must be the only number of odd occurrences of the alphabet, and we can make a record of whether the original string can become a palindrome. Secondly, since the letter can appear several times in the original string, it is not the first time that the letter encountered must have finally moved to the middle of the letter. Assuming that the letter has a total of n times, then it should be (n+1)/2 times that the letter encountered in the end of the string in the middle. This process needs to record that the letter has appeared several times, we can work around the letter processing, the method is as follows: As long as the letter is encountered, the symmetric position of the letter is the current to move the letter. In fact, if the symmetry position is also the letter, then it is exactly in place.

Based on the above analysis, we are able to change the original string to a palindrome string with the fewest number of moves, that is, the minimum number of exchanges, as long as the number of times each pair of letters will be added to the original topic required answer. Note that the subject is not to get palindrome string, just count the number of times, the number of each letter moved independently of each other.

1#include <stdio.h>2 3 intChangeCharStr[],CharXintLen) {4     intI,j,k,count =0;5      for(inti =0; I < Len/2; i++){6         if(Str[i] = =x) {7              for(j = I;j < Len-i-1; j + +){8                 if(Str[j] = = Str[len-i-1]) Break;9             }    TenCount + = J-i; One              for(k = j;k > i;k--) str[k] = str[k-1]; AStr[i] = Str[len-1-i]; -         } -         Else{ the              for(j = len-i-1; J >= i;j--){ -                 if(Str[i] = = Str[j]) Break; -             } -Count + = Len-1-I-j;//i,j symmetrical, then i+j=len-1; +              for(k = j;k < Len-1-i;k++) str[k] = str[k+1]; -Str[len-i-1] =Str[i]; +         } A     } at     returncount; - } -  - intMain () { -     Charstr[8000] = {0},x; -     intlen,b[ -] = {0},i,j,k =0; inscanf"%d",&len); - GetChar (); to      for(i =0; I < len;i++){ +scanf"%c",&str[i]); -     } the      for(i =0; I < len;i++){ *j = Str[i]-'a'; $b[j]++;Panax Notoginseng     } -      for(j =0; J < -; j + +){ the        if(B[j]%2!=0){ +k++; Ax = j +'a'; the        }     +     } -     if(k >=2){ $printf"impossible\n"); $     }      -     Elseprintf"%d\n", Change (Str,x,len)); -     return 0; the}
View Code

The price of a basic exercise perfect

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.