8d

Run Settings
LanguageC
Language Version
Run Command
#include<stdio.h> #include<stdlib.h> struct node { char v; struct node* p[26]; int l; }; struct node *root=NULL,*t,*t1; struct node * createnode(char k) { int i; t1=(struct node*)malloc(sizeof(struct node)); t1->v=k; for(i=0;i<26;i++) t1->p[i]=NULL; t1->l=0; return t1; } void inserttrie(char a[]) { t=root; int ix,i; for(i=0;a[i];i++) { ix=a[i]-'a'; if(t->p[ix]==NULL) t->p[ix]=createnode(a[i]); t=t->p[ix]; } t->l=1; } int searchtrie(char a[]) { t=root; int ix,i; for(i=0;a[i];i++) { ix=a[i]-'a'; if(t->p[ix]==NULL) return 0; t=t->p[ix]; } if(t!=NULL && t->l==1) return 1; return 0; } int main() { int n,i; char s[20]; root=createnode('#'); printf("enter number of strings"); scanf("%d",&n); printf("Enter %d strings",n); for(i=0;i<n;i++) { scanf(" %s",s); inserttrie(s); printf("inserted"); } printf("enter the key to be searched"); scanf(" %s",s); if(searchtrie(s)) printf("YES"); else printf("no"); }
#include<stdio.h> #include<stdlib.h> struct node { struct node*p[2]; }; struct node *root=NULL,*t,*t1; struct node *createnode() { t1=(struct node *)malloc(sizeof(struct node)); t1->p[0]=NULL; t1->p[1]=NULL; } void insert(int k) { t=root;int i,b; for(i=3;i>=0;i--) { b=(k>>i)&1; if(t->p[b]==NULL) { t->p[b]=createnode(); } t=t->p[b]; } } int maxxor(int k) { int res=0,i,b; t=root; for(i=3;i>=0;i--) { b=(k>>i)&1; if(t->p[b^1]==NULL) t=t->p[b]; else { res=res+(1<<i); t=t->p[b^1]; } } return res; } int main() { int n,a[10],i,r,r1=-1; root=createnode(); printf("Enter n value"); scanf("%d",&n); printf("Enter %d integers",n); for(i=0;i<n;i++) { scanf("%d",&a[i]); insert(a[i]); r=maxxor(a[i]); if(r>r1) r1=r; //printf("%d ",r1); } // printf("Inserted"); printf("%d ",r1); }
Editor Settings
Theme
Key bindings
Full width
Lines