forked from uditkumawat/Hacker-Earth-Problems-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
The_Cheapest_Palindrome.cpp
60 lines (60 loc) · 908 Bytes
/
The_Cheapest_Palindrome.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include<iostream>
#include<cstring>
#include<string.h>
int m(int a,int b)
{
return a>b?b:a;
}
using namespace std;
int main()
{
int tc=0;
cin>>tc;
while(tc-->0)
{
int min=0;
char minchar;
char s[10000];
cin>>s;
int len=strlen(s);
//cout<<s<<" "<<len<<endl;
int acost=0,bcost=0;
cin>>acost>>bcost;
if(acost>bcost)
{
min=bcost;
minchar='b';
}
else
{
min=acost;
minchar='a';
}
int sum=0;
int count=0;
for(int i=0;i<len/2;i++)
{
if(s[i]==s[len-1-i]||s[i]=='/'||s[len-1-i]=='/')
{
count++;
if((s[i]=='a'&&s[len-1-i]=='/')||(s[i]=='/'&&s[len-1-i]=='a'))
{
sum=sum+acost;
}
if((s[i]=='b'&&s[len-1-i]=='/')||(s[i]=='/'&&s[len-1-i]=='b'))
{
sum=sum+bcost;
}
if((s[i]=='/')&&(s[len-1-i]=='/'))
{
sum=sum+2*min;
}
}
}
if(count==len/2)
cout<<sum<<endl;
else
cout<<"-1"<<endl;
}
return 0;
}