zoukankan      html  css  js  c++  java
  • CodeForces 600C——思维

    题目链接:https://vjudge.net/problem/127221/origin

    A string is called palindrome if it reads the same from left to right and from right to left. For example "kazak", "oo", "r" and "mikhailrubinchikkihcniburliahkim" are palindroms, but strings "abb" and "ij" are not.

    You are given string s consisting of lowercase Latin letters. At once you can choose any position in the string and change letter in that position to any other lowercase letter. So after each changing the length of the string doesn't change. At first you can change some letters in s. Then you can permute the order of letters as you want. Permutation doesn't count as changes.

    You should obtain palindrome with the minimal number of changes. If there are several ways to do that you should get the lexicographically (alphabetically) smallest palindrome. So firstly you should minimize the number of changes and then minimize the palindrome lexicographically.

    Input

    The only line contains string s (1 ≤ |s| ≤ 2·105) consisting of only lowercase Latin letters.

    Output

    Print the lexicographically smallest palindrome that can be obtained with the minimal number of changes.

    Examples

    Input
    aabc
    Output
    abba
    Input
    aabcd
    Output
    abcba

    题目意思:给你一个字符串,要你对字符串中的字符进行最少的更改(即选定字符换成其它任何一个字符),使得让他重新排列后得到的回文串字典序最小。
    思路:1.首先统计字符串中字母的出现次数。
    2.如果需要,把ASCII值大且出现奇数次的字母变一个成ASCII值小且出现奇数次的字母,这样两种字母都出现偶数次了。
    3.最后按规律输出最后的排列就好了。
    ac代码如下:
    #include<cstdio>
    #include<string.h>
    using namespace std;
    typedef long long LL;
    const double eps = 1e-6;
    const int INF = 2e9;
    const LL LNF = 9e18;
    const int mod = 1e9+7;
    const int maxn = 2e5+10;
    
    int sum[30], len;
    char s[maxn];
    
    void init()
    {
        scanf("%s",s+1);
        len = strlen(s+1);
        for(int i = 1; i<=len; i++)
            sum[s[i]-'a']++;
    }
    
    void solve()
    {
        int i = 0, j = 25;
        while(i<j)//每次把一个奇数次的大的换成小的 
        {
            while(i<j && !(sum[i]&1)) i++;
            while(i<j && !(sum[j]&1)) j--;
            sum[i]++;
            sum[j]--;
        }
    
        for(i = 0; i<=25; i++)
        for(j = 1; j<=sum[i]/2; j++)
            printf("%c",i+'a');
    
        for(i = 0; i<=25; i++)
        if(sum[i]%2)
        printf("%c",i+'a');
    
        for(i = 25; i>=0; i--)
        for(j = 1; j<=sum[i]/2; j++)
            printf("%c",i+'a');
        printf("
    ");
    }
    
    int main()
    {
        init();
        solve();
    }
  • 相关阅读:
    html-标题标签、水平线标签和特殊字符
    htm-文字标签和注释标签
    html-html简介
    scss-函数
    scss-!optional
    scss-@extend
    解决SecureCRT下spark-shell中scala无法删除问题
    Python list降序排序
    Python 字典的一键多值,即一个键对应多个值
    python 数组中如何根据值,获取索引,如何根据索引删除值 , 以及如何根据值删除值
  • 原文地址:https://www.cnblogs.com/Mingusu/p/11953821.html
Copyright © 2011-2022 走看看