Problem Type :DP (Levenshtein distance Algorithm)
Code :
#include<stdio.h>
#include<algorithm>
#include<string.h>
#define mx 1070
using namespace std;
char ch[mx],des[mx];
int arr[mx][mx],cou,len;
void init()
{
for(int i=0; i<len; i++)
{
arr[i][0]=i;
}
for(int i=0; i<len; i++)
{
arr[0][i]=i;
}
return;
}
void lav()
{
int i,j;
for(i=1; i<len; i++)
{
for(j=1; j<len; j++)
{
if(ch[j]==des[i])
{
arr[i][j]= arr[i-1][j-1];
}
else
{
int p = min(arr[i-1][j],arr[i][j-1]);
arr[i][j]=min(p,arr[i-1][j-1]);
arr[i][j]++;
}
}
}
}
int main()
{
int i,j,test;
scanf("%d",&test);
getchar();
int cas = 0;
while(test--)
{
cas++;
scanf("%s",ch);
len = strlen(ch);
for(i=len; i>=1; i--)
{
ch[i]=ch[i-1];
}
len++;
ch[len]='\0';
des[0]='k';
j=1;
for(i=len-1; i>=1; i--)
{
des[j]=ch[i];
j++;
}
des[len]='\0';
cou=0;
init();
lav();
printf("Case %d: %d\n",cas,arr[len-1][len-1]/2);
memset(arr,0,sizeof arr);
}
return 0;
}
No comments:
Post a Comment