zoukankan      html  css  js  c++  java
  • codeforces Educational Codeforces Round 2 C Make Palindrome

    C. Make Palindrome

     
     

    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.

    Sample test(s)
    Input
    aabc
    Output
    abba
    Input
    aabcd
    Output
    abcba
     1 #include<cstdio>
     2 #include<iostream>
     3 #include<vector>
     4 #include<cmath>
     5 #include<queue>
     6 #include<string>
     7 #include<map>
     8 #include<cstring>
     9 #include<algorithm>
    10 using namespace std;
    11 typedef long long ll;
    12 typedef unsigned long long ull;
    13 const int maxn=2e5+5;
    14 int cnt[27];
    15 int main()
    16 {
    17     char s[maxn];
    18     int flag=-1;
    19     scanf("%s",s);
    20     cnt[26]=0;
    21     int len=strlen(s);
    22     for(int i=0;i<len;i++)cnt[s[i]-'a']++;
    23     for(int i=0;i<26;i++)
    24     {
    25         if(cnt[i]&1)
    26             for(int j=25;j>i;j--)
    27                 if(cnt[j]&1)
    28                 {
    29                     cnt[i]++;
    30                     cnt[j]--;
    31                     break;
    32                 }
    33     }
    34     for(int i=0;i<26;i++)
    35     {
    36         if(cnt[i]&1)flag=i;
    37         for(int j=0;j<cnt[i]/2;j++)
    38             printf("%c",i+'a');
    39     }
    40     if(flag>=0)
    41         printf("%c",flag+'a');
    42     for(int i=25;i>=0;i--)
    43             for(int j=0;j<cnt[i]/2;j++)
    44                 printf("%c",i+'a');
    45     puts("");
    46     return 0;
    47 }
  • 相关阅读:
    你的C/C++程序为什么无法运行?揭秘Segmentation fault (2)
    亲,这就是遗传算法
    我们为什么需要Map-Reduce?
    搜索引擎-架构概述(2)
    搜索引擎-架构概述(1)
    单源最短路径-迪杰斯特拉算法(Dijkstra's algorithm)
    最小生成树-普利姆算法eager实现
    最小生成树-普利姆算法lazy实现
    最小生成树-克鲁斯卡尔算法(kruskal's algorithm)实现
    索引式优先队列(indexed priority queue)
  • 原文地址:https://www.cnblogs.com/homura/p/5002164.html
Copyright © 2011-2022 走看看