int main(){ int i,l,l1,l2,temp=0,ans=0; Trie T; char *s,*t,*f; scanf("%s\n",t1); scanf("%s\n",t2); T=initTrie(); l1 = strlen(t1); l2 = strlen(t2); if(l1>l2){ t=t2; l=l2; f=t1; } else{ t=t1; l=l1; f=t2; } for(i=l;i>=0;i--) addString(T,t+i); while(*f!='\0'){ temp = getLCS(T,f++); if (ans < temp) ans = temp; } printf("%d\n",ans); destroyTrie(T); }
lztrie buildLZTrie(byte *text, uint **ids, byte s) { trie T; uint n; uint *parent; byte *letters; lztrie LZT; unsigned long long aux; // first creates a full trie T #ifdef INDEXREPORT ticks= sysconf(_SC_CLK_TCK); times(&time); t1 = time.tms_utime; printf(" Building LZTrie...\n"); fflush(stdout); printf(" Building normal trie...\n"); fflush(stdout); #endif T = createTrie(); do { text = insertTrie(T,text); } while (text[-1] != s); // now compresses it #ifdef INDEXREPORT times(&time); t2 = time.tms_utime; printf(" User time: %f secs\n",(t2-t1)/(float)ticks); fflush(stdout); t1 = t2; printf(" Representing with parentheses, letters and ids...\n"); fflush(stdout); #endif n = T->nid; aux = (2*(unsigned long long)n+W-1)/W; parent = malloc(aux*sizeof(uint)); letters = malloc(n*sizeof(byte)); aux = (((unsigned long long)n)*bits(n-1)+W-1)/W; *ids = malloc(aux*sizeof(uint)); representTrie(T,parent,letters,*ids,NULL,bits(n-1)); #ifdef INDEXREPORT times(&time); t2 = time.tms_utime; printf(" User time: %f secs\n",(t2-t1)/(float)ticks); fflush(stdout); t1 = t2; printf(" Freing trie...\n"); fflush(stdout); #endif destroyTrie(T); #ifdef INDEXREPORT times(&time); t2 = time.tms_utime; printf(" User time: %f secs\n",(t2-t1)/(float)ticks); fflush(stdout); t1 = t2; printf(" Creating compressed trie...\n"); fflush(stdout); #endif LZT = createLZTrie(parent,letters,*ids,n); #ifdef INDEXREPORT times(&time); t2 = time.tms_utime; printf(" User time: %f secs\n",(t2-t1)/(float)ticks); fflush(stdout); t1 = t2; printf(" End of LZTrie\n"); fflush(stdout); #endif return LZT; }
revtrie buildRevTrie (lztrie T, nodemap M, uint maxdepth, uint **ids) { byte *str; uint n,depth,j; trieNode i; trie RT; uint *parent, *emptybmap; revtrie CRT; // first create a full trie RT #ifdef INDEXREPORT times(&time); t1 = time.tms_utime; printf (" Building RevTrie...\n"); fflush(stdout); printf (" Creating full trie...\n"); fflush(stdout); #endif str = malloc (maxdepth*sizeof(byte)); RT = createTrie(); i = ROOT; depth = 0; for (j=1;j<T->n;j++) { i = nextLZTrie (T,i,&depth); str[maxdepth-depth] = letterLZTrie (T,i); insertstringTrie (RT,str+maxdepth-depth,depth,idLZTrie(T,i)); } free (str); // now compresses it #ifdef INDEXREPORT times(&time); t2 = time.tms_utime; printf (" User time: %f secs\n",(t2-t1)/(float)ticks); fflush(stdout); t1 = t2; printf (" Representing with parentheses and ids...\n"); fflush(stdout); #endif n = RT->nid; parent = malloc (((2*n+W-1)/W)*sizeof(uint)); *ids = malloc (n*sizeof(uint)); representTrie (RT,parent,NULL,*ids); #ifdef INDEXREPORT times(&time); t2 = time.tms_utime; printf (" User time: %f secs\n",(t2-t1)/(float)ticks); fflush(stdout); t1 = t2; printf (" Freeing trie...\n"); fflush(stdout); #endif destroyTrie(RT); #ifdef INDEXREPORT times(&time); t2 = time.tms_utime; printf (" User time: %f secs\n",(t2-t1)/(float)ticks); fflush(stdout); t1 = t2; printf (" Creating compressed trie...\n"); fflush(stdout); #endif CRT = createRevTrie(parent,T,M,*ids,n); #ifdef INDEXREPORT times(&time); t2 = time.tms_utime; printf (" User time: %f secs\n",(t2-t1)/(float)ticks); fflush(stdout); t1 = t2; printf (" End of RevTrie...\n"); fflush(stdout); #endif return CRT; }
void destroyTrie(Trie t){ tnp temp; int i; temp=t; for(i=0;i<26;i++){ if(temp->edges[i]!=NULL) destroyTrie(temp->edges[i]); } free(temp); }
// recursively destroy the tree and all its children void destroyTrie(Trie trieToDestroy) { if(trieToDestroy != NULL) { // current trie isn't NULL; recurse further int i; for(i=0;i<NUM_OF_LETTERS;i++) { destroyTrie(trieToDestroy->children[i]); } free(trieToDestroy); } }
void destroySSL(struct SSLSupport *ssl) { if (ssl) { free(ssl->sniCertificatePattern); destroyTrie(&ssl->sniContexts); #if defined(HAVE_OPENSSL) if (ssl->sslContext) { dcheck(!ERR_peek_error()); SSL_CTX_free(ssl->sslContext); } #else check(!ssl->sslContext); #endif } }
int main(int argc, char* argv[]) { freopen("CON", "w", stdout); freopen("CON", "r", stdin); freopen("CON", "w", stderr); /*************************** *** Dico & Grid creation *** ***************************/ // Trie for dictionnary Trie *t = (Trie*)malloc(sizeof(Trie)); // Trie for grid Trie *tGrid = (Trie*)malloc(sizeof(Trie)); // Grid Cell grid[N][N]; // Create Trie with words from file createFullTrie(LOCATION_DICO, t); // Create Trie for grid createTrie(tGrid); // Create grid createFullGrid(LOCATION_GRID, grid); /**************************** ********** Display ********** ****************************/ const unsigned int SCREEN_WIDTH = 480; const unsigned int SCREEN_HEIGHT = 650; TTF_Init(); SDL_Surface* screen = NULL; // screen principal char continu = 1; int state = 0; // array for coord int coord[17][2] = {{0,0}}; int c = 0; int *cpt = &c; int scoreTotal = 0; int *pScore = malloc(sizeof(int)); pScore = &scoreTotal; if (init() != 0) { fprintf(stderr,"SDL init failed\n"); return -1; } screen = SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,32,SDL_SWSURFACE | SDL_DOUBLEBUF); SDL_WM_SetCaption("RUZZLE : Etude de Cas", NULL); if (screen == NULL) { fprintf(stderr,"Open Window Failed\n"); close(); return -2; } PrincipalWindow *principal = principal_window_create(); GridWindow *gride = NULL; ScoreWindow *score = NULL; SDL_Event event; // Boucle principale while (continu) { switch (state) { case 0: principal_window_draw(principal,screen); if (principal_window_load_window_grid(principal,event) != 0) { principal_window_destroy(principal); gride = grid_window_create(pScore, grid); grid_window_draw(gride,screen, event); letter_display(LOCATION_GRID, gride, screen); state = 1; } break; case 1: if(grid_window_draw_on_clic(gride, screen, event, t, tGrid, grid, coord, cpt, pScore) == 1){ continu = 0; } if (grid_window_update(gride) != 0) { grid_window_destroy(gride); score = score_window_create(); state = 2; } break; case 2: score_window_draw(score, screen); if (score_window_load(score, event) != 0) { score_window_destroy(score); destroyTrie(tGrid); principal = principal_window_create(); state = 0; } break; } //to close application on cross clic if (event.type == SDL_QUIT) { //leave the principal loop continu = 0; } SDL_PollEvent(&event); SDL_Delay(10); SDL_Flip(screen); } close(); grid_window_destroy(gride); destroyTrie(tGrid); destroyTrie(t); return 0; }
revtrie buildRevTrie(lztrie T, uint maxdepth, uint **ids) { byte *str; uint n,rn,depth,j; trieNode i; trie RT; uint *parent, *emptybmap, *inv_ids, nbits, pos; revtrie CRT; unsigned long long aux; // first create a full trie RT #ifdef INDEXREPORT times(&time); t1 = time.tms_utime; printf (" Building RevTrie...\n"); fflush(stdout); printf (" Creating full trie...\n"); fflush(stdout); #endif str = malloc(maxdepth*sizeof(byte)); RT = createTrie(); i = ROOT; depth = 0; for (j=1;j<T->n;j++) { i = nextLZTrie(T,i,&depth); str[maxdepth-depth] = letterLZTrie(T,i); pos = leftrankLZTrie(T, i); insertstringTrie(RT,str+maxdepth-depth,depth,rthLZTrie(T,pos)); } free(str); // now compresses it #ifdef INDEXREPORT times(&time); t2 = time.tms_utime; printf(" User time: %f secs\n",(t2-t1)/(float)ticks); fflush(stdout); t1 = t2; printf(" Representing with parentheses and ids...\n"); fflush(stdout); #endif n = T->n; rn = RT->nid; aux = (2*(unsigned long long)rn+W-1)/W; parent = malloc(aux*sizeof(uint)); // 2*rn bits emptybmap = calloc(((rn+W-1)/W),sizeof(uint)); // rn bits aux = (((unsigned long long)n)*bits(n-1)+W-1)/W; *ids = malloc(aux*sizeof(uint)); // the rids array has n entries // (only for the non-empty nodes) inv_ids = malloc(aux*sizeof(uint)); representTrie(RT,parent,NULL,*ids,emptybmap,bits(n-1)); #ifdef INDEXREPORT times(&time); t2 = time.tms_utime; printf(" User time: %f secs\n",(t2-t1)/(float)ticks); fflush(stdout); t1 = t2; printf(" Freeing trie...\n"); fflush(stdout); #endif destroyTrie(RT); #ifdef INDEXREPORT times(&time); t2 = time.tms_utime; printf(" User time: %f secs\n",(t2-t1)/(float)ticks); fflush(stdout); t1 = t2; printf(" Creating compressed trie...\n"); fflush(stdout); #endif nbits = bits(n-1); for (i = 0; i < n; i++) bitput(inv_ids,bitget(*ids, i*nbits, nbits)*nbits,nbits,i); free(*ids); CRT = createRevTrie(parent,inv_ids,T,emptybmap,rn); #ifdef INDEXREPORT times(&time); t2 = time.tms_utime; printf(" User time: %f secs\n",(t2-t1)/(float)ticks); fflush(stdout); t1 = t2; printf(" End of RevTrie...\n"); fflush(stdout); #endif return CRT; }