Original question=> http://www.codechef.com/problems/WORDS1
code=>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//#include<conio.h>
int compare(const void *a,const void *b)
{
const char *a_=*(const char **)a;
const char *b_=*(const char **)b;
return(strcmp((char *)a_,(char *)b_));
}
int main()
{
int t,n,i,j,k,sts=0;
char **A;
char Bi[1000];
scanf("%d",&t); // input number of test cases
while(t--)
{
scanf("%d",&n); //input number of strings for each
test case
A=new char*[n];
for(i=0;i<n;++i)
{
scanf("%s",Bi); //input the strings
int l=strlen(Bi);
A[i]=new char[l];
strcpy(A[i],Bi);
}
// delete Bi;
qsort(A,n,sizeof(char*),compare); //sorting the
strings
bool B[n];
for(i=0;i<n;++i)
B[i]=true;
int f=1;
char s,e;
for(j=0;j<n;++j)
{
s=A[j][0];
B[j]=false;
e=A[j][strlen(A[j])-1];
for(i=0;i<n;++i)
{
if(B[i])
{
if(A[i][0]==e)
{
B[i]=false;
e=A[i][strlen(A[i])-1];
++f;
i=0;
}
}
}
if(f==n)
{
printf("Ordering is possible.\n");
sts=1;
goto outside;
}
for(k=0;k<n;++k)
B[k]=true;
f=1;
}
// delete []B;
outside:
if(sts==0)
printf("The door cannot be opened.\n");
}
}
TEST CASES
1) 1
2
ok
ok
output=>The door cannot be opened.
(so the code works fine for this case.)
2) 2
2
directi
codechef
3
skenzo
logicboxes
orderbox
output=>The door cannot be opened.
Ordering is possible.
(so the code works fine for this case too.)
3)3
2
directi
codechef
3
skenzo
logicboxes
orderbox
2
ok
ok
the code doesnt give output and stops responding as soon as 3 case
starts processing..
Plz...explain why is this happening and how to rectify this code...
Plz..reply asap.
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.