Edit Distance
来源:互联网 发布:金山数据恢复人电话 编辑:程序博客网 时间:2024/06/04 00:30
DP:
if (word1[i]==word2[j]) res[i][j] = res[i-1][j-1];
else res[i][j] = min(res[i-1][j-1],res[i-1][j],res[i][j-1])+1;
class Solution {public: int minDistance(string word1, string word2) { int m=word1.size(), n=word2.size(); if (m==0 && n==0) return 0; vector<int> t(n+1,0); vector<vector<int>> res(m+1,t); for (int i=0; i<=n; i++) { res[0][i]=i; } for (int j=0; j<=m; j++) { res[j][0]=j; } for (int i=1; i<=m; i++) { for (int j=1; j<=n; j++) { if (word1[i-1]==word2[j-1]) res[i][j]=res[i-1][j-1]; else { if (res[i][j-1]<=res[i-1][j] && res[i][j-1]<=res[i-1][j-1]) res[i][j]=res[i][j-1]+1; else if (res[i-1][j-1]<=res[i-1][j] && res[i-1][j-1]<=res[i][j-1]) res[i][j]=res[i-1][j-1]+1; else res[i][j]=res[i-1][j]+1; } } } return res[m][n]; }};
0 0
- edit distance
- Edit Distance
- edit distance
- Edit Distance
- Edit Distance
- Edit Distance
- Edit Distance
- Edit Distance
- Edit Distance
- Edit Distance
- Edit Distance
- Edit Distance
- Edit Distance
- Edit Distance
- Edit Distance
- Edit Distance
- Edit Distance
- Edit Distance
- Struts2教程1:第一个Struts2程序
- 关于性能 分析工具
- Hive 安装及使用攻略
- 用php实现js中escape和unescape
- cf——B. Mashmokh and Tokens
- Edit Distance
- 深入理解HBase Memstore
- SurfaceView 模板
- OCP-1Z0-051 第10题
- ctrl-F 速查
- HDFS通过java api进行访问
- why software are important
- 8由空格和字母组成的字符串,输出最长单词
- 观察者模式