সোমবার, ৩ জুন, ২০১৯

UVA 10739 - String to Palindrome

In this problem you are asked to convert a string into a palindrome with minimum number of operations. The operations are described below: Here you’d have the ultimate freedom. You are allowed to:
 • Add any character at any position
 • Remove any character from any position
• Replace any character at any position with another character
Print the minimum number of characters needed to turn the given string into a palindrome


INPUT:
6
tanbirahmed
shahriarmanzoor
monirulhasan
syedmonowarhossain
sadrulhabibchowdhury
mohammadsajjadhossain

OUTPUT:
Case 1: 5
Case 2: 7
Case 3: 6
Case 4: 8
Case 5: 8
Case 6: 8



#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>

using namespace std;

int table[1010][1010];
int main()
{
    int test,t,i,j,len;
    char x[1010],y[1010];
    scanf("%d",&test);
    t=1;
    while(t<=test)
    {
        scanf("%s",x);
        len=strlen(x);
        strcpy(y,x);
        reverse(y,y+len);
        for(i=0; i<=len; i++)
            table[i][0]=i;
        for(j=0; j<=len; j++)
            table[0][j]=j;
        for(i=1; i<=len; i++)
        {
            for(j=1; j<=len; j++)
            {
                if(x[i-1]==y[j-1])
                    table[i][j]=table[i-1][j-1];
                else
                    table[i][j]=min(table[i-1][j-1],min(table[i-1][j],table[i][j-1]))+1;
            }
        }
        printf("Case %d: %d\n",t++,table[len][len]/2);
    }
    return 0;
}

কোন মন্তব্য নেই:

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

Factory Pattern

Factory Method  is a creational design pattern that provides an interface for creating objects in a superclass but allows subclasses to alte...