第一次码字典树,各种辛苦……
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 |
#include <cstdio> #include <cstring> using
namespace std; struct
node{ int
son[26]; char
hash[12];}trie[1000000]; int cnt,n; char
s[100000],s1[12],ans[12]; void
insert( char
*s, char
*s1){ for ( int
l= strlen (s),x=0,i=0;i<l;i++){ if (!trie[x].son[s[i]- ‘a‘ ])trie[x].son[s[i]- ‘a‘ ]=++cnt; x=trie[x].son[s[i]- ‘a‘ ]; if (i==l-1) strcpy (trie[x].hash,s1); } } int
find( char
*s){ for ( int
l= strlen (s),x=0,i=0;i<l;i++){ if (!trie[x].son[s[i]- ‘a‘ ]) return
0; x=trie[x].son[s[i]- ‘a‘ ]; if (i==l-1){ strcpy (ans,trie[x].hash); return
strlen (ans)?1:0; } } } int
main(){ while (~ scanf ( "%s" ,&s)){ if ( strcmp (s, "START" )==0) continue ; if ( strcmp (s, "END" )==0) break ; scanf ( "%s" ,&s1); insert(s1,s); } gets (s); while ( gets (s)){ if ( strcmp (s, "START" )==0) continue ; if ( strcmp (s, "END" )==0) break ; int
i=0,j=0,len= strlen (s); while (i<len){ if (s[i]< ‘a‘ ||s[i]> ‘z‘ ){ if (find(s1)) printf ( "%s" ,ans); else
printf ( "%s" ,s1); printf ( "%c" ,s[i++]); j=0; memset (s1,0, sizeof
s1); memset (ans,0, sizeof
ans); } else
s1[j++]=s[i++]; } printf ( "\n" ); } return
0; } |
HDU 1075 What Are You Talking About
原文:http://www.cnblogs.com/forever97/p/3556552.html