CF Playing With Strings

    I. Playing With Strings
time limit per test    2.0 s
memory limit per test    64 MB
standard output

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
Copy
4
abacb
acmicpc
aabaab
bsbttxs
Output
Copy
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.

  

首先判断给定的字符串是否可组成回文串,从每个字母出现的次数出发,如果奇数次出现的字母的个数大于1,则

不能形成回文串。下面我们讨论如何将一个可形成回文串的字符串排成回文串。

   将所有的出现次数大于1的字母存到结构体“two”中并sort一下,将那个奇数次出现的字母单独存起来(如果有的话)。输出方式在代码中解释。

AC代码:

#include<stdio.h>
#include<stdlib.h>
#include<cstring>
#include<algorithm>
using namespace std;
struct per{
    char c;
    int time;
};
bool cmp(int a,int b)
{
    return a<b;
}
bool pmc(struct per a,struct per b)
{
    return a.c<b.c;
}
int main()
{
    struct per two[1009],one[1009];
    char con[1009];
    int t,k,n,i,p,j,a,b;
    scanf("%d",&t);
    for(i=1;i<=t;i++)
    {

        a=b=0;
        n=1;
        scanf("%s",con);
        k=strlen(con);
        sort(con,con+k,cmp);
        for(j=1;j<=k;j++)
            if(con[j]==con[j-1])
                n++;
            else
            {
                if(n%2==0)
                {
                    two[a].c=con[j-1];
                    two[a++].time=n;
                }
                else if(n>2)
                {
                    two[a].c=con[j-1];
                    two[a++].time=n-1;
                    one[b].c=con[j-1];
                    one[b++].time=n;
                }
                else
                {
                    one[b].c=con[j-1];
                    one[b++].time=n;
                }
                n=1;
            }
        sort(two,two+a,pmc);
        if(b>1)
            printf("impossible");
        else
        {
            for(j=0;j<a;j++)
                for(p=1;p<=two[j].time/2;p++)
                    printf("%c",two[j].c);           //输出回文串的前半部分,所以是字母出现次数的一半
            if(b==1)
                printf("%c",one[0].c);                //输出那个奇数次出现的字母
            for(j=a-1;j>=0;j--)
                for(p=1;p<=two[j].time/2;p++)         //输出后半部分
                    printf("%c",two[j].c);
        }
        putchar('\n');
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值