poj 1204

练习 trie

 1 #include <cstdio>
 2 #include <cstring>
 3 #define maxn 1000000
 4
 5 struct node {
 6     int v;
 7     char *str;
 8     node *son[26];
 9 }pool[maxn], *tail=pool, *null=pool;
10 struct Trie {
11     node *root;
12     node *newnode( int v, char *value ) {
13         node *nd=++tail;
14         nd->v = v;
15         nd->str = value;
16         for( int s=0; s<26; s++ )
17             nd->son[s] = null;
18         return nd;
19     }
20     void init() {
21         root = newnode(-1,0);
22     }
23     void insert( char *key, char *value ) {
24         node *nd=root;
25         while(1) {
26             if( *key ) {
27                 if( nd->son[*key-‘a‘]==null ) nd->son[*key-‘a‘] = newnode(*key,0);
28                 nd = nd->son[*key-‘a‘];
29                 key++;
30             } else {
31                 nd->str = value;
32                 return;
33             }
34         }
35     }
36     const char *find( char *key ) {
37         node *nd=root;
38         while(1) {
39             if( *key ) {
40                 if( nd->son[*key-‘a‘]==null ) return "eh";
41                 nd=nd->son[*key-‘a‘];
42                 key++;
43             } else {
44                 if( nd->str ) return nd->str;
45                 else return "eh";
46             }
47         }
48     }
49 }T;
50
51 char line[100];
52 char aa[100010][33], bb[33];
53
54 int main() {
55     T.init();
56     for( int i=0; ; i++ ) {
57         gets(line);
58         if( strlen(line)==0 ) break;
59         sscanf( line, "%s%s", aa[i], bb );
60         T.insert( bb, aa[i] );
61     }
62     while(1) {
63         if( gets(line)==0  ) break;
64         printf( "%s\n", T.find(line) );
65     }
66
67 }

时间: 2024-10-10 07:05:25

poj 1204的相关文章

[POJ 1204]Word Puzzles(Trie树暴搜)

Description Word puzzles are usually simple and very entertaining for all ages. They are so entertaining that Pizza-Hut company started using table covers with word puzzles printed on them, possibly with the intent to minimise their client's percepti

POJ 1204 Word Puzzles AC自动机题解

AC自动机的灵活运用,本题关键是灵活二字. 因为数据不是很大,时间要求也不高的缘故,所以本题有人使用暴力法也过了,有人使用Trie也过了. 当然有人使用AC自动机没AC的,在讨论区里喊AC自动机超时的,那是因为不会灵活运用,或者是硬套模板的,AC了速度也不会快. 给出本人的算法思路: 1 把需要查找的关键字建立Trie, 然后构造AC自动机 2 查找的时候分八个方向查找,比如棋盘是board[N][M],那么就可以循环i(0->N-1),然后每次把board[i]当做一个文本,做过HDU的key

poj 1204 Word Puzzles 静态trie数解决多模式串匹配问题

题意: 给一个二维字符数组和w个模式串,求这w个模式串在二维字符数组的位置. 分析: 静态trie树. 代码: //poj 1204 //sep9 #include <iostream> using namespace std; const int maxN=1024; const int maxM=270*maxN; char str[maxN][maxN]; char s[maxN]; int vis[27],query_id[maxM],ans[maxN][3]; int num,x,y

poj 1204 Word Puzzles(字典树)

题目链接:http://poj.org/problem?id=1204 思路分析:由于题目数据较弱,使用暴力搜索:对于所有查找的单词建立一棵字典树,在图中的每个坐标,往8个方向搜索查找即可: 需要注意的是查找时不能匹配了一个单词就不在继续往该方向查找,因为在某个坐标的某个方向上可能会匹配多个单词,所以需要一直 查找直到查找到该方向上最后一个坐标: 代码如下: #include <cstdio> #include <cstring> #include <iostream>

POJ 1204 Word Puzzles (AC自动机)

Word Puzzles Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 9926   Accepted: 3711   Special Judge Description Word puzzles are usually simple and very entertaining for all ages. They are so entertaining that Pizza-Hut company started us

(字典树+暴搜) poj 1204

Word Puzzles Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 9948   Accepted: 3726   Special Judge Description Word puzzles are usually simple and very entertaining for all ages. They are so entertaining that Pizza-Hut company started us

【 POJ - 1204 Word Puzzles】(Trie+爆搜)

Word Puzzles Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 10782 Accepted: 4076 Special Judge Description Word puzzles are usually simple and very entertaining for all ages. They are so entertaining that Pizza-Hut company started using t

[POJ 1204]Word Puzzles(Trie树暴搜&amp;amp;AC自己主动机)

Description Word puzzles are usually simple and very entertaining for all ages. They are so entertaining that Pizza-Hut company started using table covers with word puzzles printed on them, possibly with the intent to minimise their client's percepti

POJ 1204 Word Puzzles AC自己主动机题解

AC自己主动机的灵活运用,本题关键是灵活二字. 由于数据不是非常大.时间要求也不高的缘故.所以本题有人使用暴力法也过了.有人使用Trie.然后枚举八个方向,也过了.只是速度非常慢. 当然有人使用AC自己主动机没AC的,在讨论区里喊AC自己主动机超时的,那是由于不会灵活运用.或者是硬套模板的,AC了速度也不会快. 给出本人的算法思路: 1 把须要查找的keyword建立Trie, 然后构造AC自己主动机 2 查找的时候分八个方向查找,比方棋盘是board[N][M].那么就能够循环i(0->N-1