Trie
Description
Encounter the word do not know how to do? Dictionary ah, there are n known dictionary words, assuming that words are in lowercase letters. M existing words do not know, ask the m word appears in the dictionary.
Input
Contain multiple sets of test cases.
The first input line n, m (n> = 0 && n <= 100000 && m> = 0 && m <= 100000) are present in the dictionary and n words of m words to the query.
Followed by n lines, represents the presence of the word in the dictionary.
Then the line m, m to query words
n = 0 && m = 0 the routine ends
To ensure that all data words are all lowercase letters, and a length of not more than 10
Output
If there is output Yes, there is no output No.
Sample
Input
3 2 aab aa ad ac ad 0 0 |
Output
No Yes |
Such as free practice and then add the list
#include <bits / STDC ++ H.> the using namespace STD; char S [ 20 is ]; int Trie [ 500.01 thousand ] [ 26 is ]; // array is open metaphysical, bigger TLE smaller RTE int CC [ 500.01 thousand ]; // It used to prove the word exists int F = . 1 ; void in_sert () { int K = strlen (S); int P = 0 ; for ( int I = 0 ; I <K; I ++ ) { int C = S [I] - ' A ' ; IF (! Trie [the p-] [c]) { memset (Trie [f], 0 , sizeof (Trie [f])); // can be used to re-initialize the time, should be able to save some time? Trie [p] [C] = F; F ++ ; } p = Trie [p] [C]; // next letter p points to a storage location } CC [p] = . 1 ; // tag word at the ending } BOOL se_arch () { int K = strlen (S); int P = 0 ; for ( int= I 0 ; I <K; I ++ ) { int C = S [I] - ' A ' ; IF (Trie [P] [C] == 0 ) return 0 ; // described next letter is not a tree, the end of = P Trie [P] [C]; } IF (CC [P]!) return 0 ; // in the tree is not a complete word, for example, AAA tree saved, the query AA the else return . 1 ; } int main () { int n-, m; the while (~ Scanf ( " % D% D " , & n-, & m) && (n-||m)) { getchar (); Memset (Trie [ 0 ], 0 , the sizeof (Trie [ 0 ])); Memset (CC, 0 , the sizeof (CC)); F = . 1 ; // for indicating letters stored the position of the while (N-- ) { Scanf ( " % S " , S); in_sert (); } the while (M-- ) { Scanf ( " % S " , S); int T = se_arch (); if(t) printf("Yes\n"); else printf("No\n"); } } return 0; }
#include <bits / STDC ++ H.>
the using namespace STD; char S [20 is]; int Trie [500.01 thousand] [26 is]; // open array is metaphysical, bigger TLE smaller RTEint cc [500010]; // with to prove the word exists int F =. 1;
void in_sert () {int K = strlen (S); int P = 0; for (int I = 0; I <K; I ++) {int C = S [I] - ' a '; if (trie [p ] [c]!) {memset (trie [f], 0, sizeof (trie [f])); // can be used to re-initialize the time, should be able to save some time? trie [p] [c] = f; f ++;} p = trie [p] [c]; // p points to the storage location of a letter} cc [p] = 1; // tag word at the ending}
BOOL se_arch () {int k = strlen ( s); int p = 0; for (int i = 0; i <k; i ++) {int c = s [i] - 'a'; if (trie [p] [c ] == 0) return 0; // not be described next letter in the tree, the end of p = trie [p] [c ];} if (cc [p]) return 0;! // in the tree is not a complete word , such as trees stored AAA, query aa else return 1;}
int main(){ int n, m; while(~scanf("%d%d", &n, &m)&&(n||m)){ getchar(); memset(trie[0], 0, sizeof(trie[0])); memset(cc, 0, sizeof(cc)); f = 1;//用来指示字母保存的位置 while(n--){ scanf("%s", s); in_sert(); } while(m--){ scanf("%s", s); int t = se_arch(); if(t) printf("Yes\n"); else printf("No\n"); } } return 0;}