C language · The price of Perfection

Source: Internet
Author: User

Basic exercise perfect cost time limit: 1.0s memory limit: 512.0MBThe brocade SAC 1 uses the greedy algorithm. The SAC 2 enumerates each character from left to right, moving the corresponding character. The number is a single word descriptors the middle. The problem describes a palindrome, 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
1 /*2 Perfect Price: The original string is converted to a palindrome string by exchanging adjacent characters. 3 */4#include <stdio.h>5#include <stdlib.h>6 intMain () {7     inti,j,l,n,k,sum=0, flat=1, c=-1;8     Char*A;9scanf"%d",&n);TenA= (Char*)malloc(nsizeof(Char)); Onescanf"%s", a); Aj=n-1;  -     //use the greedy idea to find the number of exchanges required for each character to be traversed and then exchanged to the correct position . -      for(i=0; i<j;i++){ the          for(k=j;k>=i;k--){ -             if(k==i) {//The description did not find the same character as A[i] -                 if(n%2==0|| c!=-1){//if n is even or a[i] is not the only single character without the same characters -flat=0; +                      Break; -                 } +C=1;//n is odd, the number of exchanges required to move the first single character A[i] to an intermediate position Asum=sum+n/2-i; at                  Break; -             } -             if(a[k]==A[i]) { -                  for(l=k;l<j;l++){   -a[l]=a[l+1];  -                 }   ina[j]=A[i];  -sum=sum+j-K;  toj--;  +                  Break;  -             }   the         }   *         if(flat==0){ $              Break;Panax Notoginseng         }  -     }   the     if(flat==0) +printf"Impossible"); A     Else if(sum==0) theprintf"0"); +     Else  -printf"%d\n", sum); $     return 0;  $}

C language · The price of Perfection

Related Article

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.