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.

AC代码(没有想到aabba这种情况的输出应该是aabab)

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int main()
{
    char a[1000+10];
    int t, i, b[1000+10], j;
    scanf("%d",&t);
    getchar();
    while(t--)
    {
        int f = 0;
        char g;
        int g1;
        memset(b, 0, sizeof(b));
        cin>>a;
        int n = strlen(a);
        for(i = 0; i<n; i++)
        {
            b[a[i]-'a']++;
        }
        for(i = 0; i<26; i++)
        {
            if(b[i]%2!=0)
            {
                f++;
                g = i+'a';
                g1 = b[i];
                b[i]--;
            }
        }
        if(f>1)
            printf("impossible\n");
        else
        {
            for(i = 0; i<26; i++)
            {
                for(j = 0; j<b[i]/2; j++)
                    printf("%c",i+'a');
            }
            if(f!=0)
            {
                    printf("%c",g);
            }
            for(i = 25; i>=0; i--)
            {
                for(j = 0; j<b[i]/2; j++)
                    printf("%c",i+'a');
            }
            printf("\n");
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值