Evil Straw warts Live (Uva10716 palindrome + greedy)

Source: Internet
Author: User

"Original question"


a palindrome is a string of symbols which is 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 the adjacent symbols. For example, the string "Mamad" is transformed to the palindrome "Madam" with 3 swaps:

    • Swap "ad" to yield "MAMDA"
    • Swap "MD" to yield "MADMA"
    • Swap "Ma" to yield "madam"

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

"Sample Input"
3mamadasflkjaabb
"Output for Sample Input"
3impossible2

Test instructions: To determine whether a string can be converted to a palindrome by the adjacent two characters multiple times, cannot output "impossible", can output the minimum number of exchanges.

Idea: Preprocessing the number of occurrences of each letter, if there is an odd number of >=2 letters is not possible. Then greedy from both sides to the middle, each fixed the front end, and then gradually from the back to the middle of the search, until the first to find the same letter with the front end of the break, will find the letter to move backwards, plus the number of moves (do not know why you can do so, online said this and every time to find the best final result is Not yet proven). If you hit an odd number of letters, move it backwards, and it will be in the central position at the end.

Code:

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < cmath> #include <string> #include <map> #include <stack> #include <vector> #include <set > #include <queue> #pragma comment (linker, "/stack:102400000,102400000") #define MAXN 8005#define MAXN 2005# Define mod 1000000009#define INF 0x3f3f3f3f#define pi ACOs ( -1.0) #define EPS 1e-6#define Lson rt<<1,l,mid#define RSO  N rt<<1|1,mid+1,r#define FRE (i,a,b) for (i = A, I <= b; i++) #define FREE (i,a,b) for (i = A; I >= b; i--) #define FRL (i,a,b) for (i = A; I < b; i++) #define FRLL (i,a,b) for (i = A; i > b; i--) #define MEM (T, v) memset ((t), V, si Zeof (t)) #define SF (n) scanf ("%d", &n) #define SFF (A, b) scanf ("%d%d", &a, &b) #define SFFF (a,b,c) scanf ("%d%d%d", &a, &b, &c) #define PF printf#define DBG pf ("hi\n") typedef long Long ll;using Nam Espace Std;char str[maxn];int Num[28];bool VIS[MAXN];int main () {int n,i,j;    SF (n);        while (n--) {mem (num,0);        scanf ("%s", str);        int Len=strlen (str);        FRL (I,0,len) num[str[i]-' a ']++;        int ans=0;        FRL (i,0,26) if (num[i]%2) ans++;            if (ans>1) {pf ("impossible\n");        Continue        } ans=0;        int l=0,r=len-1;            while (L&LT;LEN/2) {int j=r;                while (j>l) {if (str[l]==str[j]), break;            j--;                } if (j==l)//Find the letter that should be placed in the middle, move it backward {char ch=str[l];                STR[L]=STR[L+1];                Str[l+1]=ch;            ans++;                } else {ans+= (r-j);                FRL (I,j,r) str[i]=str[i+1];                r--;            l++;    }} PF ("%d\n", ans); } return 0;}


Evil Straw warts Live (Uva10716 palindrome + greedy)

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.