600C - Make Palindrome - CodeForces Solution


constructive algorithms greedy strings *1800

Please click on ads to support us..

Python Code:

def solution(s):
    d = dict()

    for ch in s:
        if ch in d:
            d[ch] += 1
        else:
            d[ch] = 1
    
    odd_letters = []
    for ch in d:
        if d[ch] % 2 != 0:
            odd_letters.append(ch)

        if len(odd_letters) == 0:
        pass
    else:
        odd_letters.sort(key=lambda ch : ord(ch))

        n = len(odd_letters)
        
        for i in range(n//2):
            d[odd_letters[i]] += 1
            d[odd_letters[n-i-1]] -= 1

                
    
    parts = []
    for ch in sorted(d.keys(), key=lambda ch: ord(ch)):
        parts.append(ch * (d[ch]//2))
    
    rparts = reversed(parts)

    for ch in d.keys():
        if d[ch] % 2 != 0:
            parts.append(ch)
            break

    parts.extend(rparts)
    return ''.join(parts)    


def test():
    pass


def main():
    test()
    s = input()
    print(solution(s))


main()

C++ Code:

 

 

#include<bits/stdc++.h>

using namespace std;

 

typedef long long int   ll;



#define pi(x) cout<<x;

#define ps(x) cout<<x<<" ";

#define pnl(x) cout<<x<<"\n";

#define for0(n) for(i=0;i<n;i++)

#define for1(n) for(i=1;i<=n;i++)

#define m(x) memset(x,0,sizeof x);

#define nl cout<<"\n";

#define mp make_pair

#define pb push_back

#define fr first

#define se second

#define Inf 1e16

#define MOD 1e9+7

vector<ll>a[100000];

ll dp[100000];

ll xr[100000];

ll countx=0,xx=0;

void dfs(ll x,ll p){

      xr[x]=dp[x];

      for(auto dd:a[x]){

          if(p!=dd){

              dfs(dd,x);

              xr[x]^=xr[dd];

          }

      }

      if(xr[x]==xx){

          xr[x]=0;

          countx+=(p!=0);

      }

}



int main(){

 ios_base::sync_with_stdio(false);

 cin.tie(NULL);

 cout.tie(NULL);

ll test,h,p,i,j,xy,flag=0,n,u,count,d,o1=0,o2=0,s,e,l,r,x,y,m,z,max1,x1,y1,k,x2,y2,z1,z2,sum,min1;



string a;

cin>>a;

ll dp[26];

ll od[26];

memset(dp,0,sizeof dp);

memset(od,0,sizeof od);

for(i=0;i<a.size();i++){

    dp[a[i]-'a']++;

    od[a[i]-'a']++;

    od[a[i]-'a']%=2;

}

for(i=0;i<26;i++){

    if(od[i]){

    for(j=25;j>i;j--){

        if(od[j]){

            dp[i]++;

            od[i]--;

            od[j]--;

            break;

        }

     }

    }

}

n=a.size();

count=0;

for(i=0;i<n-i-1;i++){

    while(count<26&&dp[count]<2){

        count++;

    }

    if(count>=26){

        break;

    }

    a[i]=(count+'a');

    a[n-i-1]=(count+'a');

    dp[count]-=2;

}

if(n%2){

    for(i=0;i<26;i++){

        if(od[i]){

            a[n/2]=(i+'a');

        }

    }

}

cout<<a<<"\n";

return 0;

}


Comments

Submit
0 Comments
More Questions

1264A - Beautiful Regional Contest
1695A - Subrectangle Guess
467B - Fedor and New Game
252C - Points on Line
735C - Tennis Championship
992A - Nastya and an Array
554A - Kyoya and Photobooks
79B - Colorful Field
265B - Roadside Trees (Simplified Edition)
1362C - Johnny and Another Rating Drop
1214C - Bad Sequence
1091B - New Year and the Treasure Geolocation
244A - Dividing Orange
1061C - Multiplicity
1312A - Two Regular Polygons
801A - Vicious Keyboard
510B - Fox And Two Dots
616D - Longest k-Good Segment
1604A - Era
555B - Case of Fugitive
551A - GukiZ and Contest
1399F - Yet Another Segments Subset
1371C - A Cookie for You
430B - Balls Game
1263A - Sweet Problem
1332B - Composite Coloring
254A - Cards with Numbers
215A - Bicycle Chain
1288B - Yet Another Meme Problem
1201C - Maximum Median