-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10739 String to Palindrome.cpp
More file actions
41 lines (35 loc) · 1013 Bytes
/
Copy path10739 String to Palindrome.cpp
File metadata and controls
41 lines (35 loc) · 1013 Bytes
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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define Set(v,d) memset(v, d, sizeof(v))
#define oo 100000003 //infinity
#define F first
#define S second
#define pb push_back
#define sz(x) (int)(x.size())
#define all(x) (x.begin()),(x.end())
#define rall(x) (x.rbegin()),(x.rend())
const int Max = 1003;
int T, dp[Max][Max], counter = 0; string s;
int StringToPalindrome(int i, int j){
if(j <= i) return 0;
if(dp[i][j] != -1) return dp[i][j];
if(s[i] == s[j]) return dp[i][j] = StringToPalindrome(i + 1, j - 1);
return dp[i][j] = min( StringToPalindrome(i + 1, j - 1) + 1, min( StringToPalindrome(i, j - 1) + 1, StringToPalindrome(i + 1, j) + 1) );
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
scanf("%d", &T);
cin.ignore();
while(T--){
Set(dp, -1);
getline(cin, s);
if(s == ""){ puts("0"); continue; }
int ans = StringToPalindrome(0, sz(s) - 1);
printf("Case %d: %d\n", ++counter, ans);
}
return 0;
}