1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
| #include<stdio.h> #include<stdlib.h> #include<string.h> #define max 50000 char s[10]={'\0'}; int father_num=0; int base[10]; int h[max+5]={0}; struct node{ char s[10]; int father,rank; }; struct node opt[max+5]; FILE *fin,*fout; int ord(char c){ if(c>='A'&&c<='Z')return(c-'A'); if(c>='a'&&c<='z')return(c-'a'+26); return -1; } int hash(char s[]){ int i,t=0; for(i=5;i>=0;i--) t+=base[5-i+1]*ord(s[i])%max; t%=max; return t; } int find(char s[]){ int t=h[hash(s)]; int root=t,k,i; int path[max+5]; k=0; while(root!=opt[root].father){ k++; path[k]=root; root=opt[root].father; } for(i=1;i<=k-1;i++) opt[path[i]].father=root; return root; } void ask(char s[]){ int t=h[hash(s)]; int root=t,k,i; int path[max+5]; fprintf(fout,"%s",s); fprintf(fout," "); k=0; while(root!=opt[root].father){ k++; path[k]=root; root=opt[root].father; } for(i=1;i<=k-1;i++)opt[path[i]].father=root; fprintf(fout,"%s",opt[root].s); fprintf(fout,"\n"); } int main(){ fin=fopen("gen.in","r"); fout=fopen("gen.out","w"); int i; base[1]=1; for(i=2;i<6;i++) base[i]=base[i-1]*51%max; int n=0,t,pf,pr; char c; c=getc(fin); while(c!='?'){ fscanf(fin,"%s",s); n++; t=hash(s); if(c=='#'){ if(h[t]==0){ h[t]=n; opt[n].father=n; opt[n].rank=1; strcpy(opt[n].s,s); pf=n; } else{ n--; pr=find(s); pf=pr; } } else{ if(h[t]==0){ h[t]=n; opt[n].father=pf; opt[n].rank=opt[pf].rank+1; strcpy(opt[n].s,s); } else{ n--; opt[h[t]].father=pf; opt[h[t]].rank=opt[pf].rank+1; } } c=getc(fin); c=getc(fin); } while(c!='$'){ fscanf(fin,"%s",s); ask(s); c=getc(fin); c=getc(fin); } fclose(fin); fclose(fout); return 0; }
|