UVa 10716:evil Straw Warts Live

Source: Internet
Author: User

Link

Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_ problem&problem=1657

"Original question"

A palindrome is a string of symbols it equal to itself when reversed. Given an input string, not necessarily a palindrome, compute the number of swaps necessary to transform the string into a Palindrome. By swap we mean reversing the order of two adjacent symbols. For example, the string "Mamad" May is transformed into the palindrome "Madam" with 3 swaps:

Swap "ad" to yield "MAMDA"

Swap "MD" to yield "MADMA"

Swap "Ma" to yield "madam"

The number of input gives n, the number of test cases. For each test case, one line of input follows, containing a string of the up to lowercase letters. Output consists of one line/test case. This line would contain the number of swaps, or "impossible" if it isn't possible to transform the input to a palindrome.

"Sample Input"

3
mamad
asflkj
AABB

"Output for Sample Input"

3
Impossible
2

"The main effect of the topic"

Give a word, in order to let it become a palindrome string, allow the adjacent two number of exchange. Ask for the least number of exchanges, so that the word becomes a palindrome string.

"Analysis and Summary"

The first is to determine whether the word can constitute a palindrome string, only need to see the number of occurrences of each word, if the number of odd numbers is greater than 1, it can not constitute a palindrome string.

This column more highlights: http://www.bianceng.cn/Programming/sjjg/

Then, in order to move the least, you should first let the word outside the two sides into the same letter, set both ends of the left, right, then at the beginning of the outermost end is left=0, right=len-1, let these two positions into the same letter, and then in turn inward, ++left, --right;

The next thing is to choose which letter to turn into the same letter on both ends, and this choice involves greedy thinking.

In order to minimize the number of moves, it is necessary to move the letters to the left and right for the least number of times, so you need to find the same letters for the first time, the last occurrence, and their position at the lowest total distance between left and right. Let this letter appear for the first time to move to left, and the last occurrence of the move to right.

This loop, when the left==right ends, gets the least number of moves.

Code

* * * Uva:10716-evil Straw warts Live * time:0.012s * * author:d_double * * * * #include <iostream>  
      
#include <cstdio> #include <cstring> #define MAXN 8005 using namespace std;  
Char STR[MAXN];  
      
int VIS[MAXN];  
      
inline void swap (int &a,int &b) {int t=a; a=b; b=t;}    
    BOOL Ispossible () {memset (Vis, 0, sizeof (VIS));        
    for (int i=0; str[i]; ++i) ++vis[str[i]-' a '];                    
    int odd=0;    
        for (int i=0; i<26; ++i) {if (vis[i]%2) ++odd;    
    if (odd>1) return false;    
return true;  
    int solve () {int Len=strlen (str), cnt=0;  
    int left=0, right=len-1;  
        while (left<right) {//First find the shortest distance between the two letters int minfirst=left, minlast=right, maxdist=maxn+100;  
        memset (Vis, 0, sizeof (VIS));  
            for (int i=left; i<right; ++i) if (!vis[i]) {vis[i]=true; int LASTOCCUr=i, J;  
                For (j=i+1 j<=right; ++j) if (str[j]==str[i)) {vis[j]=true;  
            Lastoccur=j;  
                } if (i-left+right-lastoccur<maxdist) {minfirst=i, minlast=lastoccur;  
            Maxdist=i-left+right-lastoccur;    
            Start switching for (int i=minfirst; i>left;-i) {swap (str[i), str[i-1] after finding;  
        ++cnt;  
            for (int i=minlast; i<right; ++i) {swap (str[i), str[i+1]);  
        ++cnt;   
        //Inward shrinkage ++left;  
    --right;  
} return CNT;  
    int main () {int T;  
    scanf ("%d", &t);  
        while (t--) {scanf ("%s", str);  
            if (ispossible ()) {int ans=solve ();  
        printf ("%d\n", ans);  
    Else puts ("impossible");  
return 0; }

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.