1. DP calculates the editing distance
2. Find the word d in the BK tree.
View code
# Include <stdio. h> # include <stdlib. h> # include <string. h> # include <iostream> # include <vector> # include <string> # include <math. h> # include <map> # include <set> # include <stack> # include <queue> # include <algorithm> using namespace STD; int DP [40] [40]; char S1 [100], S2 [100], St [10010] [30]; const int INF = 0x7f7f7f7f; // data structure definition struct node {char word [30]; // current node value: node * Next [30];} root; node P [100000]; int num, flag, vnum, fuck; MA P <string, int> MP; int f [100000]; void Init () {for (INT I = 0; I <40; I ++) for (Int J = 0; j <40; j ++) DP [I] [J] = inf;} int diff (char * S1, char * S2) {Init (); int x = strlen (S1 + 1); int y = strlen (s2 + 1); For (INT I = 0; I <= X; I ++) DP [I] [0] = I; for (Int J = 0; j <= y; j ++) DP [0] [J] = J; for (INT I = 1; I <= x; I ++) {for (Int J = 1; j <= y; j ++) {DP [I] [J] = min (DP [I-1] [J] + 1, DP [I] [J-1] + 1), DP [I-1] [J-1] +! (S1 [I] = S2 [J]);} return DP [x] [Y];} // create void insert (node * q, char * Str) {node * l = Q; while (l) {int Dis = diff (L-> word, STR); If (! L-> next [dis]) {L-> next [dis] = & P [num ++]; strcpy (L-> next [dis]-> word + 1, STR + 1); break;} l = L-> next [dis];} // find the word void Sfind (node * Q, char * STR, int d) {If (FLAG) return; node * l = Q; If (L = NULL) return; int Dis = diff (STR, l-> word); If (DIS <= d) {fuck ++;} For (INT x = dis-D; x <= DIS + D; X ++) {If (x> = 0 & x <= 20 & L-> next [x]) Sfind (L-> next [X], STR, d );}} int main () {int n, m, D, CNT, T, abc = 1; char STR [1000]; scanf ("% d", & T ); while (t --) {scanf ("% d", & N, & M); memset (p, 0, sizeof (p )); for (INT I = 0; I <30; I ++) root. next [I] = NULL; num = 0; int cnum = 1; strcpy (ST [0] + 1, Root. word + 1); For (INT I = 1; I <= N; I ++) {scanf ("% s", St [I] + 1 ); insert (& root, St [I]);} d = 1; printf ("case # % d: \ n", ABC ++); For (INT I = 1; I <= m; I ++) {vnum = 0; flag = 0; Fuck = 0; scanf ("% S % d", STR + 1, & D ); sfind (& root, STR, d); printf ("% d \ n", fuck) ;}} return 0 ;}