编程练习
来源:互联网 发布:java程序员面试问题 编辑:程序博客网 时间:2024/05/22 08:08
// test.cpp : 定义控制台应用程序的入口点。
//
#include<iostream>
#include <vector>
#include <string>
using namespace std;
//-----------------动态规划编辑距离问题-------------------------------------------------------------------
int min(int a, int b, int c) {
int tmp = a > b ? b : a;
return tmp > c ? c : tmp;
}
int edit(string str1, string str2) {
int max1 = str1.size();
int max2 = str2.size();
int **result = new int*[max1 + 1];
for (int i = 0; i < max1 + 1; i++) {
result[i] = new int[max2 + 1];
}
for (int i = 0; i < max1 + 1; i++) {
result[i][0] = i;
}
for (int i = 0; i < max2 + 1; i++) {
result[0][i] = i;
}
for (int i = 1; i < max1 + 1; i++) {
for (int j = 1; j < max2 + 1; j++) {
int distance;
if (str1[i - 1] == str2[j - 1])
distance = 0;
else
distance = 1;
result[i][j] = min(result[i - 1][j] + 1, result[i][j - 1]+1, result[i - 1][j - 1] + distance);
}
}
for (int i = 0; i < max1+1; i++) {
for (int j = 0; j < max2 + 1; j++) {
cout << result[i][j] << " ";
}
cout << endl;
}
return result[max1][max2];
}
int main() {
string str1 = "sailn";
string str2 = "failing";
int result = edit(str1, str2);
cout << result << endl;
return 0;
}
//
#include<iostream>
#include <vector>
#include <string>
using namespace std;
//-----------------动态规划编辑距离问题-------------------------------------------------------------------
int min(int a, int b, int c) {
int tmp = a > b ? b : a;
return tmp > c ? c : tmp;
}
int edit(string str1, string str2) {
int max1 = str1.size();
int max2 = str2.size();
int **result = new int*[max1 + 1];
for (int i = 0; i < max1 + 1; i++) {
result[i] = new int[max2 + 1];
}
for (int i = 0; i < max1 + 1; i++) {
result[i][0] = i;
}
for (int i = 0; i < max2 + 1; i++) {
result[0][i] = i;
}
for (int i = 1; i < max1 + 1; i++) {
for (int j = 1; j < max2 + 1; j++) {
int distance;
if (str1[i - 1] == str2[j - 1])
distance = 0;
else
distance = 1;
result[i][j] = min(result[i - 1][j] + 1, result[i][j - 1]+1, result[i - 1][j - 1] + distance);
}
}
for (int i = 0; i < max1+1; i++) {
for (int j = 0; j < max2 + 1; j++) {
cout << result[i][j] << " ";
}
cout << endl;
}
return result[max1][max2];
}
int main() {
string str1 = "sailn";
string str2 = "failing";
int result = edit(str1, str2);
cout << result << endl;
return 0;
}
0 0
- 编程练习
- 编程练习
- 编程练习
- 编程练习
- 编程练习
- 编程练习
- 编程练习
- 编程练习
- 编程练习
- 编程练习
- 编程练习
- 编程练习
- 编程练习
- 编程练习
- 编程练习
- 编程练习
- 编程练习
- 编程练习
- UIScrollView的delegate方法妙用之让UICollectionView滑动到某个你想要的位置
- 【sql查询与优化】2.给查询结果排序
- 为你的tableView的自定义cell加上圆角
- poj2752 kmp 模板题
- GDB 引起Illegal instruction: 4 错误解决办法
- 编程练习
- Linux设备模型(6)_Bus
- Fragment动态加载
- android6.0权限管理
- SyntaxHighlighter行号显示错误问题解决方案
- LeetCode 1 Two Sum
- C#调用C++的dll及MATLAB的dll的方法(二)
- Ros kinect点云数据
- 封装的不错的解析http 命令参数的c++ 源码