-
Notifications
You must be signed in to change notification settings - Fork 2
/
72.cpp
77 lines (66 loc) · 1.73 KB
/
72.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
72
73
74
75
76
// Author : Accagain
// Date : 17/3/30
// Email : chenmaosen0@gmail.com
/***************************************************************************************
*
* Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2.
*
* (each operation is counted as 1 step.)
*
* You have the following 3 operations permitted on a word:
*
* a) Insert a character
*
* b) Delete a character
*
* c) Replace a character
*
* 做法:
*
* 时间复杂度:
*
*
****************************************************************************************/
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <vector>
#include <string>
#define INF 0x3fffffff
using namespace std;
class Solution {
public:
int minDistance(string word1, string word2) {
int dp[word1.size() + 5][word2.size() + 5];
memset(dp, 0, sizeof(dp));
for(int i=1; i<=word1.size(); i++)
dp[i][0] = i;
for(int i=1; i<=word2.size(); i++)
dp[0][i] = i;
for(int i=1; i<=word1.size(); i++)
{
for(int j=1; j<=word2.size(); j++)
{
if(word1[i-1] == word2[j-1])
{
dp[i][j] = dp[i-1][j-1];
}
else
{
dp[i][j] = min(dp[i-1][j] + 1, min(dp[i][j-1]+1, dp[i-1][j-1]+1));
}
}
}
return dp[word1.size()][word2.size()];
}
};
int main() {
Solution *test = new Solution();
int data[] = {};
vector<int> x(data, data + sizeof(data) / sizeof(data[0]));
printf("%d\n", test->minDistance("abc", "aaa"));
return 0;
}
//
// Created by cms on 17/3/30.
//