博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 4323 bk树 编辑距离
阅读量:4154 次
发布时间:2019-05-25

本文共 6562 字,大约阅读时间需要 21 分钟。

除了字符串匹配、查找回文串、查找重复子串等经典问题以外,日常生活中我们还会遇到其它一些怪异的字符串问题。比如,有时我们需要知道给定的两个字符串“有多像”,换句话说两个字符串的相似度是多少。1965年,俄国科学家Vladimir Levenshtein给字符串相似度做出了一个明确的定义叫做Levenshtein距离,我们通常叫它“编辑距离”。字符串A到B的编辑距离是指,只用插入、删除和替换三种操作,最少需要多少步可以把A变成B。例如,从FAME到GATE需要两步(两次替换),从GAME到ACM则需要三步(删除G和E再添加C)。Levenshtein给出了编辑距离的一般求法,就是大家都非常熟悉的经典动态规划问题。
    在自然语言处理中,这个概念非常重要,例如我们可以根据这个定义开发出一套半自动的校对系统:查找出一篇文章里所有不在字典里的单词,然后对于每个单词,列出字典里与它的Levenshtein距离小于某个数n的单词,让用户选择正确的那一个。n通常取到2或者3,或者更好地,取该单词长度的1/4等等。这个想法倒不错,但算法的效率成了新的难题:查字典好办,建一个Trie树即可;但怎样才能快速在字典里找出最相近的单词呢?这个问题难就难在,Levenshtein的定义可以是单词任意位置上的操作,似乎不遍历字典是不可能完成的。现在很多软件都有拼写检查的功能,提出更正建议的速度是很快的。它们到底是怎么做的呢?1973年,Burkhard和Keller提出的BK树有效地解决了这个问题。这个数据结构强就强在,它初步解决了一个看似不可能的问题,而其原理非常简单。

    首先,我们观察Levenshtein距离的性质。令d(x,y)表示字符串x到y的Levenshtein距离,那么显然:

1. d(x,y) = 0 当且仅当 x=y  (Levenshtein距离为0 <==> 字符串相等)
2. d(x,y) = d(y,x)     (从x变到y的最少步数就是从y变到x的最少步数)
3. d(x,y) + d(y,z) >= d(x,z)  (从x变到z所需的步数不会超过x先变成y再变成z的步数)

    最后这一个性质叫做三角形不等式。就好像一个三角形一样,两边之和必然大于第三边。给某个集合内的元素定义一个二元的“距离函数”,如果这个距离函数同时满足上面说的三个性质,我们就称它为“度量空间”。我们的三维空间就是一个典型的度量空间,它的距离函数就是点对的直线距离。度量空间还有很多,比如Manhattan距离,图论中的最短路,当然还有这里提到的Levenshtein距离。就好像并查集对所有等价关系都适用一样,BK树可以用于任何一个度量空间。

    建树的过程有些类似于Trie。首先我们随便找一个单词作为根(比如GAME)。以后插入一个单词时首先计算单词与根的Levenshtein距离:如果这个距离值是该节点处头一次出现,建立一个新的儿子节点;否则沿着对应的边递归下去。例如,我们插入单词FAME,它与GAME的距离为1,于是新建一个儿子,连一条标号为1的边;下一次插入GAIN,算得它与GAME的距离为2,于是放在编号为2的边下。再下次我们插入GATE,它与GAME距离为1,于是沿着那条编号为1的边下去,递归地插入到FAME所在子树;GATE与FAME的距离为2,于是把GATE放在FAME节点下,边的编号为2。
      
    查询操作异常方便。如果我们需要返回与错误单词距离不超过n的单词,这个错误单词与树根所对应的单词距离为d,那么接下来我们只需要递归地考虑编号在d-n到d+n范围内的边所连接的子树。由于n通常很小,因此每次与某个节点进行比较时都可以排除很多子树。
    举个例子,假如我们输入一个GAIE,程序发现它不在字典中。现在,我们想返回字典中所有与GAIE距离为1的单词。我们首先将GAIE与树根进行比较,得到的距离d=1。由于Levenshtein距离满足三角形不等式,因此现在所有离GAME距离超过2的单词全部可以排除了。比如,以AIM为根的子树到GAME的距离都是3,而GAME和GAIE之间的距离是1,那么AIM及其子树到GAIE的距离至少都是2。于是,现在程序只需要沿着标号范围在1-1到1+1里的边继续走下去。我们继续计算GAIE和FAME的距离,发现它为2,于是继续沿标号在1和3之间的边前进。遍历结束后回到GAME的第二个节点,发现GAIE和GAIN距离为1,输出GAIN并继续沿编号为1或2的边递归下去(那条编号为4的边连接的子树又被排除掉了)……
    实践表明,一次查询所遍历的节点不会超过所有节点的5%到8%,两次查询则一般不会17-25%,效率远远超过暴力枚举。适当进行缓存,减小Levenshtein距离常数n可以使算法效率更高。

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;int dp[40][40];char s1[100], s2[100], st[10010][30];const int inf = 0x7f7f7f7f;//数据结构定义struct node{ char word[30]; //当前结点值 node *next[30];}root;node p[100000];int num, flag, vnum, fuck;map
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(min(dp[i-1][j]+1, dp[i][j-1]+1), dp[i-1][j-1]+ !(s1[i]==s2[j]) ); } } return dp[x][y]; } //建树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]; } }//查找与单词相差不大于d的单词 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%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;}
自己写的版本,比较容易理解

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;#define MAXEDIT 15class node {public: string word; node *next[MAXEDIT]; node() { memset(next, 0, sizeof(next)); }};string split(const string& str) { size_t pos = str.find(" ||| "); return str.substr(0, pos);}bool isalpha(const string& str) { for (int i = 0; i < str.size(); ++i) { if (!(str[i]>='a' && str[i] <='z' || str[i]>='A' && str[i] <='Z' )) return false; } return true;}int minTri(int a, int b, int c) { int rst = a; if (rst > b) rst = b; if (rst > c) rst = c; return rst;}int editDist(const string &str1, const string &str2) { vector
> mat(str1.size() + 1, vector
(str2.size() +1, 0)); for (int i = 1; i < str1.size(); ++i) mat[i][0] = i; for (int i = 1; i < str2.size(); ++i) mat[0][i] = i; for (int i = 1; i <= str1.size(); ++i) { for (int j = 1; j <= str2.size(); ++j) { int cost = 1; if (str1[i-1] == str2[j-1]) cost = 0; mat[i][j] = minTri(mat[i-1][j-1]+cost, mat[i-1][j] + 1, mat[i][j-1] + 1); } } return mat[str1.size()][str2.size()];}void insert(node* head, const string& str) { node *tmp = head; while (tmp) { int dis = editDist(tmp->word, str); if (dis == 0 || dis >= MAXEDIT) return; if (tmp->next[dis]) tmp = tmp->next[dis]; else { tmp->next[dis] = new node(); tmp->next[dis]->word = str; break; } } }void buildKDTree(node *head, const vector
& ls) { for (int i = 0; i < ls.size(); ++i) { insert(head, ls[i]); }}void freeKDTree(node* head) { for (int i = 0; i < MAXEDIT; ++i) { if (head->next[i]) { freeKDTree(head->next[i]); delete head->next[i]; head->next[i] = NULL; } }}void findN(node *head, const string & str,vector
>& rst, int n) { int d = editDist(head->word, str); if (d <= n && d != 0) { rst.push_back(make_pair(head->word,d)); } int minR = max(1, d - n); int maxR = min(MAXEDIT-1, d + n); for (int i = minR; i <= maxR; ++i) { if (head->next[i]) { findN(head->next[i], str, rst, n); } }}bool Cmp(const pair
& p1, const pair
&p2) { return p1.second < p2.second;}int main(int argc, char *argv[]) { if (argc != 3) { cout << "input output"<
st; while(getline(fin, line)) { string word = split(line); if (isalpha(word) && word.size() > 1) st.insert(word); } vector
ls(st.size()); set
::iterator it = st.begin(); int i = 0; for(; it != st.end(); ++it) ls[i++] = *it; node head; head.word = ls[0]; buildKDTree(&head, ls); for (i = 0; i < ls.size();++i) { if ((i+1)%5000 ==0) cout << i+1<
> rst; int dist = min((int)ls[i].size()/2, 3); findN(&head, ls[i], rst, dist); ostringstream ostr; ostr<
<<"\t"; sort(rst.begin(), rst.end(), Cmp); for (int j = 0; j < rst.size(); ++j) { ostr<
<<" "; } fo<
<

实际效果比之前写的多线程暴力慢多了.......

转载地址:http://dueti.baihongyu.com/

你可能感兴趣的文章
android中SharedPreferences的简单例子
查看>>
android中使用TextView来显示某个网址的内容,使用<ScrollView>来生成下拉列表框
查看>>
andorid里关于wifi的分析
查看>>
Spring MVC和Struts2的比较
查看>>
Hibernate和IBatis对比
查看>>
Spring MVC 教程,快速入门,深入分析
查看>>
Android 的source (需安装 git repo)
查看>>
Commit our mod to our own repo server
查看>>
LOCAL_PRELINK_MODULE和prelink-linux-arm.map
查看>>
Simple Guide to use the gdb tool in Android environment
查看>>
Netconsole to capture the log
查看>>
Build GingerBread on 32 bit machine.
查看>>
How to make SD Card world wide writable
查看>>
Detecting Memory Leaks in Kernel
查看>>
Linux initial RAM disk (initrd) overview
查看>>
Timestamping Linux kernel printk output in dmesg for fun and profit
查看>>
There's Much More than Intel/AMD Inside
查看>>
CentOS7 安装MySQL 5.6.43
查看>>
使用Java 导入/导出 Excel ----Jakarta POI
查看>>
本地tomcat 服务器内存不足
查看>>