- #include <bits/stdc++.h>
- using namespace std;
- typedef long long LL;
- const int S = 100003;
- const int MOD = 1000000007;
- int _I(){int x; scanf("%d", &x); return x;}
- LL _LL(){LL x; scanf("%lld", &x); return x;}
- int dirX[]={1, 0, -1, 0, 1, -1, 1, -1};
- int dirY[]={0, 1, 0, -1, 1, -1, -1, 1};
- int rX[] = {1, 1, 2, 2, -1, -1, -2, -2};
- int rY[] = {2, -2, 1, -1, 2, -2, 1, -1};
- int sset(int N, int pos){return N=N|(1<<pos);}
- bool check(int N, int pos){return (bool)(N&(1<<pos));}
- int reset(int N, int pos){return N=N&~(1<<pos);}
- ///...............Code Starts From Here...............///
- char palin[101];
- int dp[101][101];
- int generatePalin(int low, int high){
- if(low >= high)return 0;
- if(dp[low][high] != -1)return dp[low][high];
- dp[low][high] = 0;
- if(palin[low] == palin[high])dp[low][high] = generatePalin(low+1, high-1);
- else dp[low][high] = min(1+generatePalin(low+1, high), 1+generatePalin(low, high-1));
- return dp[low][high];
- }
- void Do(){
- int t = _I(), cs = 0;
- while(t--){
- scanf("%s", palin);
- memset(dp, -1, sizeof(dp));
- int sz = strlen(palin)-1;
- int ans = generatePalin(0, sz);
- printf("Case %d: %d\n", ++cs, ans);
- }
- }
- int main(){
- Do();
- return 0;
- }
Monday, July 18, 2016
LightOJ - 1033 - Generating Palindromes
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment