-
Notifications
You must be signed in to change notification settings - Fork 2
/
10739.cpp
44 lines (43 loc) · 952 Bytes
/
10739.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
//BISMILLAHIRRAHMANIRRAHIM
/* Author :Shakil_AUST
problem: uva 10739 String to Palindrome
Type : DP
*/
#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std;
int cost[1002][1002];
string str;
int len;
int cal(int start,int end)
{ int x;
if(start>=end) return 0;
if(cost[start][end]) return cost[start][end];
if(str[start]==str[end]) cost[start][end]=cal(start+1,end-1);
else
{ cost[start][end]=cal(start,end-1)+1;
x=cal(start+1,end)+1;
if(x<cost[start][end]) cost[start][end]=x;
x=cal(start+1,end-1)+1;
if(x<cost[start][end]) cost[start][end]=x;
}
return cost[start][end];
}
void free()
{ for(int i=0;i<=len;i++)
for(int j=0;j<=len;j++) cost[i][j]=0;
}
int main()
{ int t;
scanf("%d",&t);
getchar();
for(int kase=1;kase<=t;kase++)
{
getline(cin,str);
len=str.size()-1;
printf("Case %d: %d\n",kase,cal(0,len));
if(kase<t) free();
}
return 0;
}