NOJ [1148] Creating Palindrome

来源:互联网 发布:风险矩阵法判断准则 编辑:程序博客网 时间:2024/06/06 05:20
  • 问题描述
  • XadillaX likes to play with strings very much.
    His favorite string is palindrome. Once he come across with a string, XadillaX always wants to change it to a palindrome by resorting the letters.

    Now give you a string, you should tell him whether the string can be changed to a palindrome. If yes, you should output it. (The smallest one in Lexicographic order)

  • 输入
  • This problem contains several cases.
    Each case is a string, no longer than 20000. Only contains uppercase letters.
  • 输出
  • For each case, if it can be changed to a palindrome, output it. If can't, output 0.


    本题有点水。。。就不解释了

    #include<stdio.h>
    #include<string.h>

    int str[100];
    char letter[20010];
    char palindrome[20010];
    int main()
    {
    while(~scanf("%s",letter))
    {
    int i,j,len=strlen(letter);
    memset(str,0,sizeof(str));
    for (i = 0; i < len; ++i)
    str[letter[i]]++;
    int odd=0;
    for(i=65;i<=90;i++)
    {
    if(str[i]%2)
    odd++;
    if(odd>=2)
    break;
    }
    if(odd>=2)
    printf("0\n");
    else
    {
    int s=0,t=len-1;
    for(i=65;i<=90;i++)
    {
    if(str[i]%2==0 && str[i])//偶数个 且不为0
    {
    while(str[i])
    {
    palindrome[s]=(char)i;
    s++;
    palindrome[t]=(char)i;
    t--;
    str[i]-=2;
    }
    }
    else if(str[i]%2)
    {
    palindrome[len/2]=(char)i;
    for(j=0;j<str[i]-1;j+=2)
    {
    palindrome[s]=(char)i;
    s++;
    palindrome[t]=(char)i;
    t--;
    }
    }
    }
    palindrome[len]='\0';
    printf("%s\n",palindrome );
    }
    }
    return 0;
    }



0 0
原创粉丝点击