Ultraviolet A 1462-fuzzy Google suggest
Question Link
To simulate Google's fuzzy search, you must first have some text, and then enter a word for query each time. This word can be operated up to Ti times. Each operation can delete one character and modify one character, or add a character and ask how many prefixes can this word match at most.
Idea: Build a dictionary tree, store the number of times each node passes, and perform DFS on the dictionary tree for each query. Mark the node found as 2 and the path as 1, then, use this tag to perform the DFS again, locate the 2 node, and terminate it, and add the number of times for the location to the answer. Because the node has 300 W, the array cannot be cleared directly, so we need to repeat the DFS clear array again.
Code:
# Include <cstdio> # include <cstring> using namespace STD; const int maxnode = 3000005; const int sigma_size = 26; int n, m, Ti; char STR [15]; struct trie {int ch [maxnode] [sigma_size]; int Val [maxnode]; int vis [maxnode]; int SZ; void Init () {SZ = 1; memset (CH [0], 0, sizeof (CH [0]);} int idx (char c) {return C-'A ';} void insert (char * STR, int v) {int n = strlen (STR); int u = 0; For (INT I = 0; I <n; I ++) {Int c = idx (STR [I]); If (! Ch [u] [c]) {memset (CH [SZ], 0, sizeof (CH [SZ]); Val [SZ] = 0; ch [u] [c] = SZ ++;} u = CH [u] [c]; Val [u] + = V ;}} void DFS (int u, int Len, int use) {If (LEN = N) {vis [u] = 2; return;} If (vis [u] = 0) vis [u] = 1; if (use <Ti) DFS (u, Len + 1, use + 1); For (INT I = 0; I <sigma_size; I ++) {If (! Ch [u] [I]) continue; If (idx (STR [Len]) = I) DFS (CH [u] [I], Len + 1, use ); else if (use <Ti) DFS (CH [u] [I], Len + 1, use + 1); If (use <Ti) DFS (CH [u] [I], Len, use + 1) ;}} int CAL (INT U) {If (vis [u] = 2) return Val [u]; int ans = 0; For (INT I = 0; I <sigma_size; I ++) {If (! Ch [u] [I]) continue; If (vis [CH [u] [I]) ans + = CAL (CH [u] [I]);} return ans;} void CLR (int u) {for (INT I = 0; I <sigma_size; I ++) {If (! Ch [u] [I]) continue; If (vis [CH [u] [I]) CLR (CH [u] [I]);} vis [u] = 0;} void solve () {Init (); For (INT I = 0; I <n; I ++) {scanf ("% s ", str); insert (STR, 1) ;}scanf ("% d", & M); For (INT I = 0; I <m; I ++) {scanf ("% S % d", STR, & Ti); n = strlen (STR); DFS (0, 0, 0 ); printf ("% d \ n", Cal (0); CLR (0) ;}} Gao; int main () {While (~ Scanf ("% d", & N) {Gao. Solve () ;}return 0 ;}
Ultraviolet A 1462-fuzzy Google suggest (Dictionary tree + DFS)