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"
Input
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
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
3
Mamad
Asflkj
Aabb
Sample Output
3
Impossible
2
Effect:
Give you a string and ask you now. can become a palindrome string, if not output impossible. If you can turn into a palindrome and only allow the exchange of adjacent two strings, now ask you how many times you can turn into a palindrome.
#include <bits/stdc++.h> using namespace std;
string S;
int n,mark[27];
int getfirst (int s,int e,string T,char c)//Find string T start from S to e end first occurrence of character C {for (int i=s;i<=e;i++) if (t[i]==c)
return i;
} int getlast (int s,int e,string T,char c)//Find the string T starting from S to E to end the last occurrence of the character C {for (int i=e;i>=s;i--) if (t[i]==c)
return i; } void Rswap (string &t,int s,int e)//swap the character in S to the right to the position of E where {for (int i=s;i<e;i++) swap (t[i],t[i+1]);} void L
Swap (string &t,int s,int e)//swap the character at E to the left of the S position {for (int i=e;i>s;i--) swap (t[i],t[i-1]);} int main () {
Ios::sync_with_stdio (FALSE);
cin>>n;
while (n--) {cin>>s;
int ans=0;
memset (Mark,0,sizeof (Mark));
int len=s.size ();
for (int i=0;i<len;i++) mark[s[i]-' a ']++;
int coun=0;
for (int i=0;i<26;i++) if (mark[i]%2) coun++; if (coun>1) {cout<< "ImposSible "<<endl;
Continue
} int l=0,r=len-1;
while (r-l>0) {if (S[r]==s[l]) {r--;l++;
} else {int lc=getlast (l,r,s,s[l]);
int Rc=getfirst (l,r,s,s[r]);
if (r-lc<rc-l)//{Rswap (s,lc,r);
ANS+=R-LC;
} else {Lswap (S,L,RC);
Ans+=rc-l;
}}} cout<<ans<<endl;
} return 0;
}
Answer:
First consider whether to become palindrome, this is better to think. If the odd number of characters in this string is 1 extra, it is impossible to become a palindrome. The
then considers how the interchange can be the minimum number of times.
The final form of the string must be a palindrome string, that is, the position of each character at the end is certain, here must refer to the principle of minimum movement each time. For example, the string AABBC can eventually become ABCBA can also become bacab, to see which Exchange is the fewest number of times.
Consider the left and right characters, such as ABBCAC
1. First look at the leftmost character a, to move the least, it must be to find a character from the far right and then move to the far right, is also the left side of a symmetrical position, so to calculate, to become Abbcca need to exchange 1 times on the line. Record this number, but do not move, because it is not the best to determine the next step.
2. Next, look at the string ABBCAC, but look at the rightmost character C, and then find the first character C from left to right, and calculate the number of times the found character C is moved to the leftmost interchange, knowing that the programming Cabbac need to be exchanged 3 times.
3. The above comparison allows you to know that the swap character A is better than the swap character C, so the string becomes Abbcca, then the subscript on the left is added 1, the subscript on the right minus 1, and the string bbcc is considered.
then in the same way to judge the BBCC, the number of exchanges is summed up is the result ~