-
Notifications
You must be signed in to change notification settings - Fork 359
/
BK_Tree.cpp
71 lines (63 loc) · 1.34 KB
/
BK_Tree.cpp
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include<bits/stdc++.h>
using namespace std;
int distance(string s1,string s2)
{
int i,j,m=s1.size(), n=s2.size();
int A[m+1][n+1];
for(i=0;i<=n;i++) A[0][i]=i;
for(i=1;i<=m;i++) A[i][0]=i;
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
if(s1[i-1]==s2[j-1]) A[i][j]=A[i-1][j-1];
else A[i][j]=min(A[i][j-1],min(A[i-1][j-1],A[i-1][j])) +1;
}
}
return A[m][n];
}
typedef struct bknode
{
string key;
struct bknode *ptrs[10]={NULL};
}*bkptr;
void insert(bkptr &BK,string str)
{
if(BK==NULL)
{
BK=new(bknode);
BK->key=str;
return;
}
int dist=distance(BK->key,str);
insert(BK->ptrs[dist],str);
}
void find_keys_with_given_key(bkptr BK,string str,int n)
{
queue<bkptr>que;
que.push(BK);
while(!que.empty())
{
bkptr temp=que.front();que.pop();
int d=distance(temp->key,str);
if(d<=n) cout<<d<<" "<<temp->key<<" ";
for(int i=0;i<10;i++)
{
if(temp->ptrs[i]!=NULL && i>=d-n &&i<=d+n)
que.push(temp->ptrs[i]);
}
}
}
int main()
{
bkptr BK=NULL;
string str; cin>>str;
while(str!="#")
{
insert(BK,str);
cin>>str;
}
int n; cin>>str>>n;
find_keys_with_given_key(BK,str,n);
return 0;
}