Guest User

Untitled

a guest
Jan 22nd, 2014
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.67 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4.  
  5. char** A;
  6. int elem=0;
  7.  
  8.  
  9. struct nodo{
  10.    
  11.     char *s;
  12.     struct nodo* sx;
  13.     struct nodo* dx;
  14.  
  15. };
  16.  
  17. struct nodo* InsBin(struct nodo* t, char* w){
  18.  
  19.     if(t==NULL){
  20.         struct nodo* new=(struct nodo*)malloc(sizeof(struct nodo));
  21.         new->s=w;
  22.         new->sx=NULL;
  23.         new->dx=NULL;
  24.         return new;
  25.     }
  26.    
  27.     if ((strcmp(t->s,w))<0)
  28.         t->dx=InsBin(t->dx,w);
  29.    
  30.     else t->sx=InsBin(t->sx,w);
  31.    
  32. return t;
  33. }
  34.  
  35. struct nodo* cerca(struct nodo* t, char* w){
  36.    
  37.     if(t!=NULL){
  38.         if(!(strcmp(t->s,w))) return t;
  39.         else if((strcmp(t->s,w))<0)
  40.             cerca(t->dx,w);
  41.             else cerca(t->sx,w);
  42.     }
  43.     else return NULL;
  44. }
  45.  
  46. void cpystring(struct nodo* t){
  47.  
  48.     if(t!=NULL){
  49.         cpystring(t->sx);
  50.         A[elem]=t->s;
  51.         elem++;
  52.         cpystring(t->dx);
  53.     }
  54. }
  55.  
  56. int main(){
  57.  
  58.     int n,i,l,ris,stop;
  59.     char word[1000];
  60.     char* temp;
  61.     char* u;
  62.     char* v;
  63.     struct nodo* t=NULL;   
  64.     struct nodo* t1=NULL;  
  65.     scanf("%d",&n);
  66.     A=(char**)malloc(n*sizeof(char*));
  67.     for(i=0;i<n;i++) A[i]=NULL;
  68.     elem=0;
  69.     for(i=0;i<n;i++){
  70.         scanf("%s",word);
  71.         temp=(char*)malloc(1000*sizeof(char));
  72.         strcpy(temp,word);
  73.         t=InsBin(t,temp);
  74.     }
  75.     scanf("%s",word);
  76.     t1=cerca(t,word);
  77.     if(t1==NULL) ris=-1;
  78.     else{
  79.         cpystring(t1);
  80.         if(A[1]==NULL) ris=strlen(A[0]);
  81.         else{
  82.             i=0;
  83.             stop=0;
  84.             while(i<n&&(!stop)){
  85.                 if(A[i]!=NULL)i++;
  86.                 else stop=1;
  87.             }
  88.             l=i;
  89.             u=(char*)malloc(1000*sizeof(char));
  90.             v=(char*)malloc(1000*sizeof(char));
  91.             strcpy(u,A[0]);
  92.             strcpy(v,A[l-1]);
  93.             if(strlen(u)<=strlen(v))l=strlen(u);
  94.                 else l=strlen(v);
  95.             i=0;
  96.             while(i<l&&(u[i]==v[i]))
  97.                     i++;
  98.             ris=i;
  99.         }
  100.     }
  101.     printf("%d\n",ris);
  102.     return 0;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment