当前位置:网站首页>583. 两个字符串的删除操作-动态规划
583. 两个字符串的删除操作-动态规划
2022-06-30 05:59:00 【Mr Gao】
583. 两个字符串的删除操作
给定两个单词 word1 和 word2 ,返回使得 word1 和 word2 相同所需的最小步数。
每步 可以删除任意一个字符串中的一个字符。
示例 1:
输入: word1 = “sea”, word2 = “eat”
输出: 2
解释: 第一步将 “sea” 变为 “ea” ,第二步将 "eat "变为 “ea”
示例 2:
输入:word1 = “leetcode”, word2 = “etco”
输出:4
这题可以转化为求解最长公共子序列,使用动态规划算法,比较合适:
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];
}
边栏推荐
- At the beginning of 2022, people who are ready to change jobs should pay attention to
- How to create a CSR (certificate signing request) file?
- You don't know how to deduce the location where HashSet stores elements?
- [GPU] basic operation
- Create priority queue
- UE4_ Editor development: highlight the UI making method according to the assets dragged by the mouse (1)
- Set of XXL job principles
- Finally someone can make the server so straightforward
- Ultra simple STM32 RTC alarm clock configuration
- [ansible series] fundamentals -01
猜你喜欢

UE4_ Editor UMG close window cannot destroy UMG immediately

Sword finger offer 18 Delete the node of the linked list

如何制作CSR(Certificate Signing Request)文件?

At the age of 32, I fell into a middle-aged crisis and finally quit naked...

Official win 10 image download

Zibll子比主题V6.4.1wordpress 开心版源码下载_破解原版/直接使用/无需教程

MySQL高级SQL语句

Do you know how to show the health code in only 2 steps

Implementation of property management system with ssm+ wechat applet

飞升:基于中文分词器IK-2种自定义热词分词器构建方式showcase & 排坑showtime
随机推荐
Transfer the token on the matic-erc20 network to the matic polygon
【板栗糖GIS】global mapper—如何把栅格的高程值赋予给点
InputStream转InputStreamSource
雲服務器部署 Web 項目
token 过期后,如何自动续期?
About modifying dual system default startup item settings
Learning automation ppt
MySQL advanced (Advanced SQL statement)
English grammar_ Adjective / adverb Level 3 - superlative
飞升:基于中文分词器IK-2种自定义热词分词器构建方式showcase & 排坑showtime
电脑查看WiFi使用密码
Solidity - 安全 - 重入攻击(Reentrancy)
PC viewing WiFi password
数据读写:Unity中基于C#脚本实现数据读写功能
MySQL transaction
OpenCL线程代数库ViennaCL的使用
Create uiactionsheet [duplicate] - creating uiactionsheet [duplicate]
Acwing winter vacation daily question 2022 punch in day 11
UE4_ Editor UMG close window cannot destroy UMG immediately
Using lazy < t > in C # to realize singleton mode in WPF