I - Playing With Strings (模拟)

Dani and Mike are two kids ,They are playing games all day and when they don't find a game to play they invent a game . There is about an hour to arrive to school, because they love playing with strings Dani invented a game , Given a string and the winner is the first who form a palindrome string using all letters of this string according to the following sample rules : 1- player can rearrange letters to form a string . 2- the formed string must be palindrome and use all letters of the given string. 3- if there is more than one string chose the lexicographically smallest string . EX: string is "abacb" player can form : "abcba" and "bacab" ,but "abcba" is the lexicographically smallest. Mike asked you to write a Program to compute the palindrome string so he can beat Dani.

Input

Your program will be tested on one or more test cases. The first line of the input will be a single integer T, the number of test cases (1  ≤  T  ≤  1000). Every test case on one line contain one string ,the length of the string will not exceed 1000 lower case English letter.

Output

For each test case print a single line containing the lexicographically smallest palindrome string according to the rules above. If there is no such string print "impossible"

Examples

Input

4
abacb
acmicpc
aabaab
bsbttxs

Output

abcba
impossible
aabbaa
bstxtsb

Note

Palindrome string is a string which reads the same backward or forward.

Lexicographic order means that the strings are arranged in the way as they appear in a dictionary.

题解:将输入字符串按最小字典序回文输出,如果不能则输出impossible

#include<bits/stdc++.h>
using namespace std;
map<char,int>mp;
char a[1010];
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        string s;
        cin>>s;
        mp.clear();
        for(int i=0; i<s.size(); i++)
            mp[s[i]]++;
        int sum=0;
        for(char c='a'; c<='z'; c++)
        {
            if(mp[c]%2==1)
                sum++;
        }
        if(sum>1)//不管是奇数还是偶数,只要奇数字符出现了大于1次
        {
            cout<<"impossible"<<endl;
            continue;
        }
        int top=0;
        for(char c='a'; c<='z'; c++)
        {
            while(mp[c]>1)//这个数可以两边都放
            {
                cout<<c;//前面
                a[top++]=c;//后面
                mp[c]-=2;//减去2
            }
        }
        if(s.size()%2==1)//奇数特判中间位,将其孤立的点输出
        {
            for(char c='a'; c<='z'; c++)
            {
                if(mp[c]==1)//中间
                {
                    cout<<c;
                    break;
                }
            }
        }
        for(int i=top-1;i>=0;i--)//倒序
            cout<<a[i];
        cout<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43824158/article/details/89007524
今日推荐