Skip to content

Commit

Permalink
Merge pull request #519 from Saurabhsahab/main
Browse files Browse the repository at this point in the history
The Cheapest Palindrome solution (#508)
  • Loading branch information
kanak22 authored Oct 26, 2021
2 parents e4c0266 + d8dd6b5 commit 4ef87bb
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions HackerEarth/The Cheapest Palindrome/The Cheapest Palindrome.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/***
⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⡖⠁⠀⠀⠀⠀⠀⠀⠈⢲⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⣼⡏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢹⣧⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⣸⣿⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⣿⣇⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⣿⣿⡇⠀⢀⣀⣤⣤⣤⣤⣀⡀⠀⢸⣿⣿⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⢻⣿⣿⣔⢿⡿⠟⠛⠛⠻⢿⡿⣢⣿⣿⡟⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⣀⣤⣶⣾⣿⣿⣿⣷⣤⣀⡀⢀⣀⣤⣾⣿⣿⣿⣷⣶⣤⡀⠀⠀⠀⠀
⠀⠀⢠⣾⣿⡿⠿⠿⠿⣿⣿⣿⣿⡿⠏⠻⢿⣿⣿⣿⣿⠿⠿⠿⢿⣿⣷⡀⠀⠀
⠀⢠⡿⠋⠁⠀⠀⢸⣿⡇⠉⠻⣿⠇⠀⠀⠸⣿⡿⠋⢰⣿⡇⠀⠀⠈⠙⢿⡄⠀
⠀⡿⠁⠀⠀⠀⠀⠘⣿⣷⡀⠀⠰⣿⣶⣶⣿⡎⠀⢀⣾⣿⠇⠀⠀⠀⠀⠈⢿⠀
⠀⡇⠀⠀⠀⠀⠀⠀⠹⣿⣷⣄⠀⣿⣿⣿⣿⠀⣠⣾⣿⠏⠀⠀⠀⠀⠀⠀⢸⠀
⠀⠁⠀⠀⠀⠀⠀⠀⠀⠈⠻⢿⢇⣿⣿⣿⣿⡸⣿⠟⠁⠀⠀⠀⠀⠀⠀⠀⠈⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣼⣿⣿⣿⣿⣧⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠐⢤⣀⣀⢀⣀⣠⣴⣿⣿⠿⠋⠙⠿⣿⣿⣦⣄⣀⠀⠀⣀⡠⠂⠀⠀⠀
⠀⠀⠀⠀⠀⠈⠉⠛⠛⠛⠛⠉⠀⠀⠀⠀⠀⠈⠉⠛⠛⠛⠛⠋⠁⠀⠀
* Author : SUARABH UPADHAYAY
* E-mail : usaurabh207@gmail.com
PRACTICE LIKE YOU NEVER WIN, PLAY LIKE YOU NEVER LOOSE
***/

#include<bits/stdc++.h>
using namespace std;
#define ll long long

int main(){

ios_base::sync_with_stdio(false);
cin.tie(0);

ll t;
cin>>t;
while(t--){

//Taking string as input

string s;
cin>>s;
ll p=s.length()-1,x,y;
ll q=s.length()/2;
ll ans=0,r=0;

//Assigning mapping values of costs
map<char,ll> m;
cin>>x>>y;
m['a']=x;
m['b']=y;
m['/']=0;

// Calculating ans

ll k=min(m['a'],m['b']);
for(int i=0;i<q;i++){
if(s[i]!=s[p-i]){
if(s[i]=='/'||s[p-i]=='/'){
ans=ans+m[s[i]]+m[s[p-i]];
}
else{
cout<<-1<<"\n";
r=1;
break;
}
}
else{
if(s[i]==s[p-i]&&s[p-i]=='/')
ans=ans+(2*k);

}
}
if(r==0)
cout<<ans<<"\n";
}
}

0 comments on commit 4ef87bb

Please sign in to comment.