I did the dictionary tree a month ago. I still remember that he was the first dictionary tree in my AC. Hey.
ACCode:
View code
# Include <algorithm> # Include <Iostream> # Include <Cstdlib> # Include <Cstdio> # Include <Cstring> # Include < String > # Include < Set > Using Namespace STD; Const Int Maxn = 26 ; Const Int Maxlen = 100 ; Char SS1 [ 50005 ] [Maxlen], ss2 [maxlen], ss3 [maxlen]; Struct Node { Bool Flg; Node * Next [maxn]; node () {flg = False ; For ( Int I = 0 ; I <maxn; I ++ ) Next [I] = NULL ;} ~ Node (){ For ( Int I = 0 ; I <maxn; I ++) If (Next [I]! = NULL) next [I] = Null ;}}; Class Trie { Public : Node * Root; trie () {Root = NULL ;} Void Insert ( String Str ){ If (! Root) Root =New Node (); Node * Location = Root; For ( Int I = 0 ; I <Str. Length (); I ++ ){ Int Num = STR [I]- ' A ' ; If (Location-> next [num] = Null) Location -> Next [num] = New Node (); Location = Location-> Next [num];} location -> Flg = True ;} Bool Search ( String Str) {Node * Location = Root; For ( Int I = 0 ; I <Str. Length (); I ++){ Int Num = STR [I]- ' A ' ; If (Location-> next [num] = Null) Return False ; Location = Location-> Next [num];} Return Location-> Flg ;}} T; Int Main (){ String Str1, str2; Int Line = 0 ; Memset (SS1, ' ' , Sizeof (SS1 )); While (CIN. Getline (SS1 [Line], maxlen) {str1 = SS1 [Line ++ ]; T. insert (str1 );} For ( Int I = 0 ; I <line; I ++ ){ For ( Int J = 1 ; J <strlen (SS1 [I]); j ++ ) {Str1 = Str2 = "" ; Memset (ss2, ' ' , Sizeof (Ss2); memset (ss3, ' ' , Sizeof (Ss3); strncpy (ss2, SS1 [I], J); ss2 [J] = ' \ 0 ' ; Strncpy (ss3, SS1 [I] + J, strlen (SS1 [I])- J); ss3 [strlen (SS1 [I]) -J] = ' \ 0 ' ; // Cout <ss2 <''<ss3 <Endl; Str1 = ss2, str2 = Ss3; If (T. Search (str1 )&& T. Search (str2) {cout <SS1 [I] < Endl; // System ("pause "); Break ;}}} Return 0 ;}
Thank you for your criticism. Thank you!