字典树

(1)hdu 1075 What Are You Talking About 【stl or 字典树】

   动态建立字典树

View Code
  1 #include<stdio.h>
  2 #include<string.h>
  3 #include<stdlib.h>
  4 
  5 typedef struct node
  6 {
  7     node *child[26];
  8     char str[12];
  9     char flag;
 10 }node, *Node;
 11 
 12 Node root;
 13 
 14 void build_trie(Node *root)
 15 {
 16     if(((*root)=(Node) calloc(1,sizeof(node)))==NULL)
 17     {
 18         printf("error\n");
 19         exit(-1);
 20     }
 21 }
 22 
 23 void insert(char *mar,char *eng)
 24 {
 25     int i,index,len;
 26     Node current=NULL,newnode =NULL;
 27     len=strlen(mar);
 28     if(len==0)
 29         return ;
 30     current=root;
 31     for(i=0;i<len;i++)
 32     {
 33         index=mar[i]-'a';
 34         if(current ->child[index]!=NULL)
 35             current =current->child[index];
 36         else
 37         {
 38             if((newnode=(Node) calloc(1,sizeof(node)))==NULL)
 39             {
 40                 printf("error\n");
 41                 exit(-1);
 42             }
 43             current->child[index] = newnode;
 44             current=newnode;
 45         }
 46     }
 47     current->flag='0';
 48     strcpy(current->str,eng);
 49 }
 50 
 51 int find(char *str)
 52 {
 53     int i, index, len;
 54     Node current = NULL;
 55     len = strlen(str);
 56     if(0 == len)
 57         return 0;
 58     current = root;               // 每次查找都从要根结点开始
 59     for (i = 0; i < len; i++)
 60     {
 61         index = str[i] -'a';
 62         if (NULL != current->child[index]) // 当前字符存在字典树中
 63         {
 64             current = current->child[index];  //修改当前位置
 65         }
 66         else
 67         {
 68             return 0;
 69         }
 70     }
 71     if (current->flag == '0')     //存在此单词
 72     {
 73         printf("%s", current->str);
 74         return 1;
 75     }
 76     return 0;
 77 }
 78 
 79 void translate()
 80 {
 81     char tmp[12];
 82     char c;
 83     scanf("%s",&tmp);
 84     strcpy(tmp,"");
 85     c=getchar();
 86     int n=0;
 87     while(c=getchar())
 88     {
 89         if((c<'a'||c>'z')&&(c<'A'||c>'Z'))
 90         {
 91             tmp[n]='\0';
 92             if(strcmp(tmp,"END")==0)
 93                 break;
 94             if(find(tmp)==0)
 95                 printf("%s",tmp);
 96             printf("%c",c);
 97             n=0;
 98       //      strcpy(tmp,"");
 99         }
100         else
101         {
102             tmp[n++]=c;
103         }
104     }
105 }
106 int main()
107 {
108     char mar[12],eng[12];
109 //    freopen("test","r",stdin);
110     scanf("%s",&mar);
111     build_trie(&root);         //建立火星文字典树
112     while(scanf("%s",&eng)!=EOF && strcmp(eng,"END")!=0)
113     {
114         scanf("%s",&mar);
115  //       printf("%s %s\n",eng,mar);
116         insert(mar,eng);
117     }
118     translate();
119     return 0;
120 }

(2)spoj  Hacking

静态建立字典树,有三个大减枝

View Code
 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<stdlib.h>
 4 
 5 char s[10010],ans[105];
 6 int flag,m,k,t;
 7 int avail = 1;
 8 const int dict=26;
 9 const int size=100*10010;
10 typedef struct node
11 {
12     int child[26];
13     int cas[26];
14 }node;
15 node tree[size];
16 int len;
17 void insert(int beg)
18 {
19     int i,root,index;
20     i = 0;
21     root = 0;
22     for(i=0;i<m;i++)
23     {
24         if(i+beg>=len)return;
25         index = s[i+beg]-'a';
26         if(index>=k)
27             return ;
28         if (tree[root].child[index]==0 || tree[root].cas[index]!=t)
29         {
30             tree[root].child[index] = avail++;
31             tree[root].cas[index]=t;
32         }
33         root = tree[root].child[index];
34     }
35 }
36 
37 void dfs(int d,int current)
38 {
39     int i;
40     if(flag==0)
41         return ;
42     if(d>=m)
43         return ;
44     for(i=0;i<k;i++)
45     {
46         if(flag==0)
47             return ;
48         if(tree[current].child[i]==0|| tree[current].cas[i]!=t)
49         {
50             flag=0;
51             ans[d]=i+'a';
52             ans[d+1]='\0';
53  //           printf("%d %d\n",i,d);
54             printf("%s\n",ans);
55             return ;
56         }
57         else
58         {
59             ans[d]=i+'a';
60             dfs(d+1,tree[current].child[i]);
61         }
62     }
63 }
64 
65 int main()
66 {
67     int n;
68 //    freopen("test","r",stdin);
69     scanf("%d",&t);
70  //   memset(tree,0,sizeof(tree));
71     while(t--)
72     {
73         avail=1;
74         flag=1;
75         scanf("%d%d%d",&n,&m,&k);
76         scanf("%s",s);
77         len=strlen(s);
78         if(k == 1)
79         {
80              for(int i=0; i<m; i++)
81                 printf("a");
82             printf("\n");
83             continue;
84          }
85         for(int i=0;i<len;i++)
86         {
87             insert(i);
88         }
89         dfs(0,0);
90     }
91     return 0;
92 }

 

 

posted @ 2012-10-07 13:25  feng_linxu  Views(165)  Comments(0)    收藏  举报