当前位置:网站首页>583. deleting two strings - Dynamic Planning
583. deleting two strings - Dynamic Planning
2022-06-30 06:04:00 【Mr Gao】
583. Deletion of two strings
Give two words word1 and word2 , Return makes word1 and word2 The same minimum number of steps required .
Every step You can delete a character from any string .
Example 1:
Input : word1 = “sea”, word2 = “eat”
Output : 2
explain : The first step will be “sea” Turn into “ea” , The second step will be "eat " Turn into “ea”
Example 2:
Input :word1 = “leetcode”, word2 = “etco”
Output :4
This problem can be transformed into solving the longest common subsequence , Using dynamic programming algorithms , More appropriate :
int minDistance(char * word1, char * word2){
int n=strlen(word1),m=strlen(word2);
int dp[n+1][m+1];
int i,j;
for(i=0;i<=m;i++){
dp[0][i]=0;
}
for(i=0;i<=n;i++){
dp[i][0]=0;
}
for(i=1;i<=n;i++){
char ch1=word1[i-1];
for(j=1;j<=m;j++){
char ch2=word2[j-1];
if(ch2==ch1){
dp[i][j]=dp[i-1][j-1]+1;
}
else{
if(dp[i-1][j]>dp[i][j-1]){
dp[i][j]=dp[i-1][j];
}
else{
dp[i][j]=dp[i][j-1];
}
}
// printf("%d index %d %d ",dp[i][j],i,j);
}
}
return n-dp[n][m]+m-dp[n][m];
}
边栏推荐
- Several commands not commonly used in MySQL
- Balanced binary tree judgment of Li Kou 110 -- classic problems
- Cisco VXLAN配置
- Tornado frame foundation
- 【数据库】事务
- [deep learning] data segmentation
- There is a group of students' score {99, 85, 82, 63, 60}. To add a student's score, insert it into the score sequence and keep the descending order
- luoguP2756 飞行员配对方案问题(最大流)
- Base64详解:玩转图片Base64编码
- Master slave synchronization of MySQL database to realize read-write separation
猜你喜欢
Configure the user to log in to the device through telnet -- AAA local authentication
SparseArray
Intelligent question - horse racing question
[deep learning] data segmentation
从零开发 stylelint规则(插件)
inno setup 最简单的自定义界面效果
MySQL storage system
Using lazy < t > in C # to realize singleton mode in WPF
Finally someone can make the server so straightforward
MySQL数据库用户管理
随机推荐
1380. lucky numbers in matrices
文件操作IO-Part1
Dynamic programming -- gliding wing of the strange thief Kidd
Decompilation normal decompilation problems. Solve them yourself
583. 两个字符串的删除操作-动态规划
SHELL
多线程进阶篇
10-【istio】istio Sidecar
MySQL 索引
Record a problem tracking of excessive load
Intelligent deodorizer embedded development
[secretly kill little partner pytorch20 days] - [day4] - [example of time series data modeling process]
JS prototype chain object function relationship
二十四、输入输出设备模型(串口/键盘/磁盘/打印机/总线/中断控制器/DMA和GPU)
MySQL transaction
Codeforces C. Andrew and Stones
Detailed explanation of issues related to SSL certificate renewal
ES6数组遍历与ES5数组遍历
UE4_ Editor development: highlight the UI making method according to the assets dragged by the mouse (1)
网络基础知识