-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #519 from Saurabhsahab/main
The Cheapest Palindrome solution (#508)
- Loading branch information
Showing
1 changed file
with
77 additions
and
0 deletions.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
HackerEarth/The Cheapest Palindrome/The Cheapest Palindrome.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} |