Dev Skill Make It Palindrome
#include <bits/stdc++.h>
using namespace std;
#define sf scanf
#define pf printf
const int mx = 1005;
char s[mx];
int dp[1005][1005];
int rec(int i, int j)
{
if(i >= j) return 0;
if(dp[i][j] >= 0) return dp[i][j];
if(s[i] == s[j]) dp[i][j] = rec(i+1, j-1);
else return dp[i][j] = 1+min(rec(i+1, j) , rec(i, j-1));
return dp[i][j];
}
int main()
{
int t, ans, tc = 0, n;
sf("%d", &t);
while(t--)
{
sf("%s", &s);
memset( dp, -1, sizeof (dp) );
n = strlen(s);
ans = rec(0, n-1);
pf("Case %d: %d\n", ++tc, ans);
}
return 0;
}
using namespace std;
#define sf scanf
#define pf printf
const int mx = 1005;
char s[mx];
int dp[1005][1005];
int rec(int i, int j)
{
if(i >= j) return 0;
if(dp[i][j] >= 0) return dp[i][j];
if(s[i] == s[j]) dp[i][j] = rec(i+1, j-1);
else return dp[i][j] = 1+min(rec(i+1, j) , rec(i, j-1));
return dp[i][j];
}
int main()
{
int t, ans, tc = 0, n;
sf("%d", &t);
while(t--)
{
sf("%s", &s);
memset( dp, -1, sizeof (dp) );
n = strlen(s);
ans = rec(0, n-1);
pf("Case %d: %d\n", ++tc, ans);
}
return 0;
}
Comments
Post a Comment