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;
    } 
    自己选择的路,跪着也要走完。朋友们,虽然这个世界日益浮躁起来,只要能够为了当时纯粹的梦想和感动坚持努力下去,不管其它人怎么样,我们也能够保持自己的本色走下去。
  • 相关阅读:
    数据包构造分析工具Hping3常用命令集合大学霸IT达人
    C语言结构联合位字段知识体系总结大学霸IT达人
    基于ARP的网络扫描工具netdiscover常用命令集合
    C语言指针总结大学霸IT达人
    批量探测工具fpingping常用命令集合大学霸IT达人
    App数据分析小心预加载机制
    C语言数组知识体系整理大学霸IT达人
    ARP探测目标工具arping常用命令集合大学霸IT达人
    C语言函数知识体系大学霸IT达人
    启动sql server的服务时,window 不能在本地计算机启动Sql Server(MSSQLSERVER)问题
  • 原文地址:https://www.cnblogs.com/WTSRUVF/p/9603757.html
Copyright © 2011-2022 走看看