Saturday, 28 July 2012

UVa 11753 - Creating Palindrome Solution

#include<iostream>
#include<list>
#include<string>
#include<cstring>
#include<sstream>
#include<cctype>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<stack>
#include<fstream>
#include<cstdlib>
#include<vector>
#include<map>
#include<set>
#include<utility>
#include<iomanip>
#include<queue>

using namespace std;

#define INF (1<<29)
#define SET(a) memset(a,-1,sizeof(a))
#define ALL(a) a.begin(),a.end()
#define CLR(a) memset(a,0,sizeof(a))
#define FILL(a,v) memset(a,v,sizeof(a))
#define PB push_back
#define FOR(i,n) for(int i = 0;i<n;i++)
#define PI acos(-1.0)
#define EPS 1e-9
#define MP(a,b) make_pair(a,b)
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
#define LL long long
#define MX 10010
#define MOD 1000000007

LL arr[MX],k;
//LL memo[MX][MX][MX];

int func(int i,int j, int cnt)
{
    if(cnt>k)   return cnt;
    if(i>j)
        return cnt;
    if(arr[i]==arr[j])
        return func(i+1,j-1,cnt);
    else
        return min(func(i+1,j,cnt+1),func(i,j-1,cnt+1));
}

main()
{
    LL t,kk=1,n,i,j,cnt;
    cin>>t;
    while(t--)
    {
        cin>>n>>k;
        for(i=0;i<n;i++)
            cin>>arr[i];

        i=0;
        j=n-1;
        //cnt=0;

        cnt=func(i,j,0);

        /*while(i<j)
        {
            if(arr[i]==arr[j])
            {
                i++;
                j--;
            }
            else
            {
                if(arr[i+1]==arr[j])
                {
                    cnt++;
                    j--;
                    i+=2;
                }
                else if(arr[i]==arr[j-1])
                {
                    cnt++;
                    i++;
                    j-=2;
                }
                else
                {
                    cnt+=2;
                    i++;
                    j--;
                }
            }
        }*/


    cout<<"Case "<<kk++<<": ";
    if(cnt>k)
        cout<<"Too difficult";
    else if(cnt==0)
        cout<<"Too easy";
    else
        cout<<cnt;
    cout<<endl;
    }
}

No comments:

Post a Comment