this is my accepted code

#include<stdio.h>
int flag;
struct node
{
       char a;
       int end;
       struct node *next[10];
}*root[10];
struct node *makenode(char item,int b)
{
       int i,l;
     struct node *temp;
     temp=(struct node *)malloc(sizeof(struct node));
     temp->a=item;
//    printf("in makenode");
    //  scanf("%d",&l);
     temp->end=b;
      //scanf("%d",&l);
     for(i=0;i<10;i++)
     temp->next[i]=NULL;
      //scanf("%d",&l);
     return temp;
}
insert(char *s)
{
               int i;
               int b,l;
               struct node *temp,*p;
               p=root[s[0]-'0'];
            for(i=0;s[i]!='\0';i++)
              {
                               if(root[s[0]-'0']==NULL)
                               {
                                                       if(s[1]=='\0')
                                                       {
                                                                     b=1;
                                                       }
                                                       else
                                                       {
                                                           b=0;
                                                       }

        //
printf("\nfirst");

root[s[0]-'0']=makenode(s[0],b);
          //
printf("\nfirst  %c",root[s[0]-'0']->a);
            //                                               scanf("%d",&l);
                                                           p=root[s[0]-'0'];
                               }

                               else if(i!=0)
                               {
                                  // p=root[s[0]-'0'];

if(p->next[s[i]-'0']!=NULL&&p->next[s[i]-'0']->end==1)
                                                      {

flag=1;

p=p->next[s[i]-'0'];
                                                      }
                                                      else
if(p->next[s[i]-'0']!=NULL&&p->next[s[i]-'0']->end==0)
                                                      {


p=p->next[s[i]-'0'];
                                                            if(s[i+1]=='\0')
                                                            {

p->end=1;
                                                                     }
                                                            else
                                                            {
                                                            }
                                                      }
                                                       else
if(p->next[s[i]-'0']==NULL)
                                                       {

if(s[i+1]=='\0')
                                                       {
                                                                     b=1;
                                                       }
                                                       else
                                                       {
                                                           b=0;
                                                       }

temp=makenode(s[i],b);

p->next[s[i]-'0']=temp;

p=temp;
                                                       }
                                 }
                                 else if(i==0)
                                 {
                                      if(p->end==1&&s[1]!='\0')
                                      flag=1;
                                 }
                              //   printf("\n %c %d ",p->a,p->end);
              }
              for(i=0;i<10;i++)
              {
                               if(p->next[i]!=NULL)
                               {
                                                   flag=1;
                                                   break;
                               }
              }
}
main()
{
      int t,i,n;
      char s[20];
      scanf("%d",&t);

      while(t--)
      {
             for(i=0;i<=9;i++)
      {
                       root[i]=NULL;
      }
                flag=0;
                scanf("%d",&n);
                for(i=1;i<=n;i++)
                {
                               scanf("%s",s);
                               if(flag==0)
                               insert(s);

                }
                if(flag==1)
                {
                           printf("NO\n");
                }
                else
                {
                           printf("YES\n");
                }
      }
      return 0;
}



-- 
*UTKARSH SRIVASTAV
CSE-3
B-Tech 2nd Year
@MNNIT ALLAHABAD*

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to