个人赛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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值