Order of the four railroad cars

To find the order of the four railroad cars moving-in and moving out on the tracks numbered 1-4.

Algorithm

Input	Order of car.
Output	Order is possible or not
Complexity	O(c).

Algo_car()
1.	i=j=k=flag=0 and top=-1   //Initialize segment variables;
2.	For  i=0 to 3.
3.	      do  perm[i]   //input the order in which railroad car enters. 
4.	Initialize  i=0;
5.	Repeat  while(input[j]!='\0')
	  if(perm[i]==input[j]) then :
		Set out[k]=input[j];
        k++,j++,i++;
      Else
		top++;
		Set stack[top]:=input[j].
		J++;
5.	Repeat while(top>0 && flag==0)    
      If (stack[top]==perm[i]) then :
		Set out[k]:=stack[top];
		Top--;
	    I++,k++;.
      Else  set  flag := 1.   	
6.	If (flag) then print “Output Order Not possible!!!".
7.	Else print "Output Order is Possible.".
8.	Exit


Algorithm Description


C Code


//WAP to Transform one string into another String using DP.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void showseq(char[20][20],int,int);
int temp[20][20];
int main()
{
	int c[20][20],cost[6],i,j,m,n;
	char op[20][20],x[20],x1[20],y[20],y1[20];
	printf("Enter cost of Delete,Insert,Copy,Replace,Twiddle,Kill\n");
	scanf("%d%d%d%d%d%d",&cost[0],&cost[1],&cost[2],&cost[3],&cost[4],&cost[5]);
	printf("Enter first string : ");
	fflush(stdin);
	gets(x1);
	printf("Enter second string : ");
	fflush(stdin);
	gets(y1);
	m=strlen(x1);
	n=strlen(y1);
	for(i=1;i<=m;i++)
	{
		x[i]=x1[i-1];
	}
	for(j=1;j<=n;j++)
	{
		y[j]=y1[j-1];
	}
	c[0][0]=0;
	op[0][0]='N';
	for(i=1;i<=m;i++)
	{
		c[i][0]=i*cost[0];
		op[i][0]='D';
	}
	for(j=1;j<=n;j++)
	{
		c[0][j]=j*cost[1];
		op[0][j]='I';
	}
	for(i=1;i<=m;i++)
	{
		for(j=1;j<=n;j++)
		{
			c[i][j]=9999;	
			if(x[i]==y[j])
			{
				c[i][j]=c[i-1][j-1]+cost[2];
				op[i][j]='C';	
			}
			if(x[i]!=y[j]&&(c[i-1][j-1]+cost[3]<c[i][j]))
			{
				c[i][j]=c[i-1][j-1]+cost[3];
				op[i][j]='R';	
			}
		if((i>=2&&j>=2)&&(x[i]==y[j-1])&&(x[i-1]==y[j])&&(c[i-2][j-2]+cost[4]<c[i][j]))
		{
			c[i][j]=c[i-2][j-2]+cost[4];
			op[i][j]='T';	
		}
			if((c[i-1][j]+cost[0]<c[i][j]))
			{
				c[i][j]=c[i-1][j]+cost[0];
				op[i][j]='D';
			}
			if((c[i][j-1]+cost[1]<c[i][j]))
			{
				c[i][j]=c[i][j-1]+cost[1];
				op[i][j]='I';
			}
		}
	}
	for(i=0;i<m;i++)
	{
		if(c[i][n]+cost[5]<c[m][n])
		{
		c[m][n]=c[i][n]+cost[5];
		op[m][n]='K';	
		temp[m][n]=i;
		}
	}
	for(i=0;i<=m;i++)
	{
		for(j=0;j<=n;j++)
		{
			printf("%d %c\t",c[i][j],op[i][j]);
		}
		printf("\n");
	}
	printf("\nTransformation followed is :\n");
	showseq(op,m,n);
getch();
}

void showseq(char op[][20],int i,int j)
{
	int k,r;
	if(i==0&&j==0)
	return;
	if(op[i][j]=='C'||op[i][j]=='R')
	{
		k=i-1;
		r=j-1;
	}
	else if(op[i][j]=='T')
	{
		k=i-2;
		r=j-2;
	}
	else if(op[i][j]=='D')
	{
		k=i-1;
		r=j;
	}
	else if(op[i][j]=='I')
	{
		k=i;
		r=j-1;
	}
	else if(op[i][j]=='K')
	{
		k=temp[i][j];
		r=j;
	}
	showseq(op,k,r);
	printf("%c\n",op[i][j]);
}