-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4.cpp
51 lines (48 loc) · 1.11 KB
/
4.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
//
// main.cpp
// 字符串最小操作
//
// Created by Allen on 2019/4/18.
// Copyright ? 2019 Allen. All rights reserved.
//
#include <iostream>
#include <string>
using namespace std;
int count(){
string stringA,stringB;
cout<<"请输入第一个字符串:";
cin>>stringA;
cout<<"请输入第二个字符串:";
cin>>stringB;
int Alength = stringA.length()+1;
int Blength = stringB.length()+1;
int dp[Alength][Blength],i,j,temp;
for (i=0; i<Alength; i++) {//对列初始化
dp[i][0]=i;
}
for (i=0; i<Blength; i++) {//对行初始化
dp[0][i]=i;
}
for (i=1; i<Alength; i++) {
for (j=1; j<Blength; j++) {
//核心代码部分
temp = min(dp[i][j-1]+1, dp[i-1][j]+1);
dp[i][j]=min(temp,dp[i-1][j-1]+(stringA[i-1]==stringB[j-1]?0:1));
}
}
for (i=0; i<Alength; i++) {
for (j=0; j<Blength; j++) {
cout<<dp[i][j]<<" ";
}
cout<<endl;
}
cout<<"最少操作次数为:"<<dp[Alength-1][Blength-1]<<endl;
return 0;
}
int main() {
int n ;
while (cin>>n&&n!=0) {
count();
}
return 0;
}