-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCheapestPalindrome.cpp
More file actions
38 lines (36 loc) · 1.43 KB
/
CheapestPalindrome.cpp
File metadata and controls
38 lines (36 loc) · 1.43 KB
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
#include<cstdio>
#include<cstring>
int main(){
int t,a,b,cntr,flag,j,i;
char str[11000];
scanf("%d",&t);
while(t--){
cntr=0;flag=0;
scanf("%s",&str);
scanf("%d%d",&a,&b);
i=strlen(str)-1;
for(j=0;j<=(i>>1);j++){
if(str[j]!='/'&&str[j]==str[i-j])continue;
if(str[j]!='/'&&str[i-j]!='/'&&str[j]!=str[i-j]){flag=1;break;}
else if(str[j]=='/'&&str[i-j]=='/'){
cntr=cntr+2*(a>b?b:a);
str[j]=str[i-j]='a';
}
else if(str[j]=='/'){
if(str[i-j]=='a')cntr+=a;
else cntr+=b;
str[j]=str[i-j];
}
else{
if(str[j]=='a')cntr+=a;
else cntr+=b;
str[i-j]=str[j];
}
//printf("%d\t%d\n",cntr,j);
}
if(flag)
printf("-1\n");
else printf("%d\n",cntr);
}
return 0;
}