个人赛8 div.2总结

简单题组,a了八道比上次进步一些,英语题目还是短板,理解题意很慢,还是要加强英语能力。
贴一道我感觉比较新颖的题

I - Playing With Strings Gym - 101020I

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

题目大意 把字符串按最小字典序的回文串输出,如果无回文串输出“impossible”

先判断有多少字符出现奇数次,大于1则不能形成回文串。输出时用类似桶排序的思想存下字符出现次数,标记奇数次字符,按字典序正序输出一半->输出奇数次字符->逆序输出另一半。
输出部分代码有点冗杂,应该可以简化一下

C代码如下

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define INF 0x3f3f3f3f


int main()
{
    
    
    char s[1100];
    int a[200];
    int t,j,i,l,flag,x;
    scanf("%d",&t);
    getchar();
    while(t--)
    {
    
    
        memset(a,0,sizeof (a));
        flag=0;
        gets(s);
        l=strlen(s);
        for(i=0; i<l; i++)
        {
    
    
            x=s[i];
            a[x]++;
        }
        for(i=97; i<=122; i++)
        {
    
    
            if(a[i]%2)
                flag++;
        }
        if(flag>1)
            printf("impossible\n");
        else
        {
    
    
            x=0;
            for(i=97; i<=122; i++)
            {
    
    
                if(a[i]%2)
                    x=i;
                for(j=0; j<a[i]/2; j++)
                    printf("%c",i);
                if(a[i]%2)
                    a[i]=(a[i]+1)/2;
                else
                    a[i]=a[i]/2;
            }
            if(x)
            {
    
    
                printf("%c",x);
                a[x]--;
            }
            for(i=122; i>=97; i--)
            {
    
    
                for(j=0; j<a[i]; j++)
                    printf("%c",i);
            }
            printf("\n");

        }
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/ooold_six/article/details/107944134