I - Playing With Strings

这一生的挚爱 提交于 2019-11-27 15:42:57

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.

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;

int main()
{
    int t,i,j,len,num,h;
    char str[1001];
    scanf("%d",&t);
    while(t--)
    {
        int a[26]= {0};
        scanf("%s",str);
        len=strlen(str);
        for(i=0; i<len; i++)
        {
            a[str[i]-'a']++;
        }
        num=0;
        for(i=0; i<26; i++)
        {
            if(a[i]%2!=0)
                num++;
        }
        if(num!=1&&num!=0)   
            printf("impossible\n");
        else
        {
            if(num==0)
            {
                for(i=0; i<26; i++)
                {
                    if(a[i])
                    {
                        for(j=0; j<a[i]/2; j++)
                        {
                            printf("%c",i+'a');
                        }
                        a[i]/=2;
                    }
                }
                for(i=25; i>=0; i--)
                {
                    if(a[i])
                    {
                        for(j=0; j<a[i]; j++)
                        {
                            printf("%c",i+'a');
                        }
                    }
                }
                printf("\n");
            }
            else
            {
                for(i=0; i<26; i++)
                {
                    if(a[i]%2==0)
                    {
                        for(j=0; j<a[i]/2; j++)
                        {
                            printf("%c",i+'a');
                        }
                        a[i]/=2;
                    }
                    else
                    {
                        if(a[i]!=1)
                        {
                            a[i]-=1;
                            for(j=0; j<a[i]/2; j++)
                                printf("%c",i+'a');
                            a[i]/=2;
                            h=i;
                        }
                        else
                        {
                          h=i;
                          a[i]--;
                        }
                    }
                }
                printf("%c",h+'a');
                for(i=25; i>=0; i--)
                {
                    if(a[i])
                    {
                        for(j=0; j<a[i]; j++)
                        {
                            printf("%c",i+'a');
                        }
                    }
                }
                printf("\n");
            }
        }
    }
    return 0;
}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!