Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- char** A;
- int elem=0;
- struct nodo{
- char *s;
- struct nodo* sx;
- struct nodo* dx;
- };
- struct nodo* InsBin(struct nodo* t, char* w){
- if(t==NULL){
- struct nodo* new=(struct nodo*)malloc(sizeof(struct nodo));
- new->s=w;
- new->sx=NULL;
- new->dx=NULL;
- return new;
- }
- if ((strcmp(t->s,w))<0)
- t->dx=InsBin(t->dx,w);
- else t->sx=InsBin(t->sx,w);
- return t;
- }
- struct nodo* cerca(struct nodo* t, char* w){
- if(t!=NULL){
- if(!(strcmp(t->s,w))) return t;
- else if((strcmp(t->s,w))<0)
- cerca(t->dx,w);
- else cerca(t->sx,w);
- }
- else return NULL;
- }
- void cpystring(struct nodo* t){
- if(t!=NULL){
- cpystring(t->sx);
- A[elem]=t->s;
- elem++;
- cpystring(t->dx);
- }
- }
- int main(){
- int n,i,l,ris,stop;
- char word[1000];
- char* temp;
- char* u;
- char* v;
- struct nodo* t=NULL;
- struct nodo* t1=NULL;
- scanf("%d",&n);
- A=(char**)malloc(n*sizeof(char*));
- for(i=0;i<n;i++) A[i]=NULL;
- elem=0;
- for(i=0;i<n;i++){
- scanf("%s",word);
- temp=(char*)malloc(1000*sizeof(char));
- strcpy(temp,word);
- t=InsBin(t,temp);
- }
- scanf("%s",word);
- t1=cerca(t,word);
- if(t1==NULL) ris=-1;
- else{
- cpystring(t1);
- if(A[1]==NULL) ris=strlen(A[0]);
- else{
- i=0;
- stop=0;
- while(i<n&&(!stop)){
- if(A[i]!=NULL)i++;
- else stop=1;
- }
- l=i;
- u=(char*)malloc(1000*sizeof(char));
- v=(char*)malloc(1000*sizeof(char));
- strcpy(u,A[0]);
- strcpy(v,A[l-1]);
- if(strlen(u)<=strlen(v))l=strlen(u);
- else l=strlen(v);
- i=0;
- while(i<l&&(u[i]==v[i]))
- i++;
- ris=i;
- }
- }
- printf("%d\n",ris);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment