Skip to content

Instantly share code, notes, and snippets.

@mhmoodlan
Created August 8, 2017 13:03
Show Gist options
  • Save mhmoodlan/4d01b09b53278bef356ece211bace964 to your computer and use it in GitHub Desktop.
Save mhmoodlan/4d01b09b53278bef356ece211bace964 to your computer and use it in GitHub Desktop.
#include <bits/stdc++.h>
#define ll long long
#define sz(v) ((int) ((v).size()))
#define clr(v, d) memset(v, d, sizeof(v))
#define lp(i, n) for(int i = 0; i < (int)(n); ++i)
#define rep(i, v) for(int i = 0; i < sz(v); ++i)
using namespace std;
const int OO = 10000;
const int MAX = 1001;
int n;
string s;
int cache[MAX][MAX];
int minPale(int st, int en) {
if(st>=en)
return 0;
int &ret = cache[st][en];
if(ret != -1)
return ret;
ret = OO;
if(s[st] == s[en])
ret = minPale(st+1, en-1);
else {
ret = min(minPale(st+1, en)+1, min(minPale(st, en-1)+1, minPale(st+1, en-1)+1));
}
return ret;
}
int main() {
cin >>n;
lp(i, n) {
clr(cache, -1);
cin>>s;
cout<<"Case "<<i+1<<": "<<minPale(0, s.length()-1) << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment