Pages

রবিবার, ১৮ আগস্ট, ২০১৩

Solution code of uva 10453(make palindrome)





#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
 int dp[1010][1010];
 char result[1010];
 int ptr=0;
   int mini_character(int i,int j,char data[])
  {
        if(i>j)return 0;
    if(data[i]==data[j])
    {
        dp[i][j]= mini_character(i+1,j-1,data);

    }
    if(dp[i][j]!=-1)
        return dp[i][j];
    else
    {
        dp[i][j]=min(mini_character(i,j-1,data),mini_character(i+1,j,data))+1;
    }
      return dp[i][j];
 }
void create_result(int i,int j,char data[])
  {
      if(i>j)return;
      if(i==j)
      {
         result[ptr++]=data[j];return;
      }
      else if(data[i]==data[j])
      {
          result[ptr++]=data[i];
          create_result(i+1,j-1,data);
          result[ptr++]=data[j];
      }
      else if(dp[i][j-1]<=dp[i+1][j])
      {
          result[ptr++]=data[j];
          create_result(i,j-1,data);
          result[ptr++]=data[j];
      }
      else
      {
          result[ptr++]=data[i];
          create_result(i+1,j,data);
          result[ptr++]=data[i];
      }

  }
int main()
{
    //freopen("c:\\in.txt","r",stdin);
    char data[1010];
    while(gets(data))
    {
          ptr=0;
          memset(dp,-1,sizeof dp);
           int t= mini_character(0,strlen(data)-1,data);
           cout<<dp[0][strlen(data)-1];
           create_result(0,strlen(data)-1,data);
           result[ptr]=0;
            cout<<" "<<result<<endl;
    }
    return 0;
}

0 comments:

একটি মন্তব্য পোস্ট করুন