Edit Distance (Levenshtein Distance)
来源:互联网 发布:2016淘宝店卖什么好卖 编辑:程序博客网 时间:2024/05/04 10:47
Edit Distance (Levenshtein Distance)
Find the edit distance between given two words s1 and s2.
The disntace is the minimum number of single-character edits required to change one word into the other. The edits including the following operations:
insertion: Insert a character at a particular position.
deletion: Delete a character at a particular position.
substitution: Change the character at a particular position to a different character
Input
s1
s2
Two words s1 and s2 are given in the first line and the second line respectively. The words will consist of lower case characters.
Output
Print the edit distance in a line.
Constraints
1 ≤ length of s1 ≤ 1000
1 ≤ length of s2 ≤ 1000
Sample Input 1
acac
acm
Sample Output 1
2
Sample Input 2
icpc
icpc
Sample Output 2
0
解题思路:
找一遍LCS,状态转移方程改一下,改成相同时不加,不同时再加。
if(s1[i-1] == s2[j-1])f[i][j] = f[i-1][j-1];
else f[i][j] = min(min(f[i-1][j],f[i][j-1]),f[i-1][j-1]) + 1;
#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int maxn = 1000 + 5;char s1[maxn],s2[maxn];int f[maxn][maxn],len1,len2;int main(){ scanf("%s%s",s1,s2); len1 = strlen(s1); len2 = strlen(s2); for(int i = 0;i <= len1; i++)f[i][0] = i; for(int j = 0;j <= len2; j++)f[0][j] = j; for(int i = 1;i <= len1; i++) { for(int j = 1;j <= len2; j++) { if(s1[i-1] == s2[j-1])f[i][j] = f[i-1][j-1]; else f[i][j] = min(min(f[i-1][j],f[i][j-1]),f[i-1][j-1]) + 1; } } printf("%d\n",f[len1][len2]);}
- Edit Distance (Levenshtein Distance)
- Levenshtein distance(Edit distance)
- Levenshtein Edit distance
- Levenshtein距离,Edit Distance
- Edit Distance(Levenshtein Distance) in Java
- 编辑距离(Edit Distance | Levenshtein距离)
- Levenshtein Distance
- Levenshtein Distance
- Levenshtein distance
- Levenshtein distance
- Levenshtein distance
- levenshtein distance
- Levenshtein distance
- Minimum edit distance(levenshtein distance)(最小编辑距离)初探
- edit distance
- Edit Distance
- edit distance
- Edit Distance
- 史上最简单的SpringCloud教程 | 第三篇: 服务消费者(Feign)
- 关于死锁
- 2017 蓝桥杯JavaB组省赛 分巧克力
- 第八届蓝桥杯参赛心得
- Matlab 数值计算----斯特芬森加速迭代法
- Edit Distance (Levenshtein Distance)
- vs编译安装protobuf
- 大小端
- ssm ssh xml文件的约束文件
- c++内存分配(堆栈 new malloc)和内存常见错误
- ArcGIS Desktop 10.2 完全破解安装教程(含win7 32/64位+下载地址+亲测可用)
- 正则表达式学习
- 移动应用设计:应用内搜索和业务转化
- 五、网络进阶