zoukankan      html  css  js  c++  java
  • Make Palindrome CodeForces

    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

    就是用最小的改变次数把这个字符串 转换成回文串
    可以移动字符的位置 不计入总次数
    如果好几个次数相同的 取字典序最小的
    统计每个单词的个数 从前 后分别 找到个数为奇数的单词 前面的++ 后边的--
    如果只有一个 放到中间输出即可
    原字符串长度为偶数的序列 不存在奇数个 个数奇数个的单词
    #include <bits/stdc++.h>
    using namespace std;
    string str;
    int num[28];
    vector<char> v;
    int main()
    {
        cin>> str;
        int len = str.size();
        for(int i=0; i<len; i++)
            num[str[i]-'a']++;
        int j = 25;
        int flag;
        for(int i=0; i<26; i++)
        {
            if(num[i] & 1)
            {
                while(!(num[j] & 1) && j >= i) j--;
                if((num[j] & 1) && j > i)
                {
                    num[j]--;
                    num[i]++;
                }
                if(i == j)
                    flag = i;
            }
            if(num[i])
                for(int k=0; k<num[i]/2; k++)
                {
                    char tmp = 'a' + i;
                    printf("%c", tmp);
                    v.push_back(tmp);
                }
        }
        if(len & 1)
            printf("%c", 'a'+flag);
        for(int i=v.size()-1; i>=0; i--)
            cout<< v[i]; 
        cout<< endl;
        
        return 0;
    } 
    自己选择的路,跪着也要走完。朋友们,虽然这个世界日益浮躁起来,只要能够为了当时纯粹的梦想和感动坚持努力下去,不管其它人怎么样,我们也能够保持自己的本色走下去。
  • 相关阅读:
    RAID技术
    Mysql的用户基本操作
    LNMP之Php的安装配置
    java 实现图片拼接
    java 实现Serv-U FTP 和 SFTP 上传 下载
    Image合并添加文字内容
    AOP切面用于系统日志
    网页评论实现
    java web 实体类生成
    java接口调试思想
  • 原文地址:https://www.cnblogs.com/WTSRUVF/p/9603757.html
Copyright © 2011-2022 走看看