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();
    }
  • 相关阅读:
    Bert及变种简述
    损失函数
    头条(三面)、新浪(一面)、快手(两面)、bigo(两面)面试
    使用curl出现,curl: /usr/local/lib/libssl.so.1.1: version `OPENSSL_1_1_1' not found (required by /usr/lib/x86_64-linux-gnu/libcurl.so.4)
    cmake 出现undefined reference to xxx 解决办法
    tensorflow2.0编程规范
    asp.net过滤HTML标签,只保留换行与空格
    除了IE浏览器能识别之外,其他浏览器都不能识别的html写法
    bootstrap按钮加载状态改变
    ASP.NET静态页生成
  • 原文地址:https://www.cnblogs.com/Mingusu/p/11953821.html
Copyright © 2011-2022 走看看