当前位置:网站首页>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];
}
边栏推荐
- UE4_ Editor development: highlight the UI making method according to the assets dragged by the mouse (1)
- 数据读写:Unity中基于C#脚本实现数据读写功能
- Finally someone can make the server so straightforward
- Xi'an Jiaotong 21st autumn online expansion resources of online trade and marketing (II)
- SparseArray
- Solidy - fallback function - 2 trigger execution modes
- [road of system analyst] collection of wrong topics in Project Management Chapter
- Basic operations of C language
- Xi'an Jiaotong 21st autumn "computerized accounting" online homework answer sheet (I) [standard answer]
- leetcode763. Divide letter interval
猜你喜欢

Tornado frame foundation

飞升:基于中文分词器IK-2种自定义热词分词器构建方式showcase & 排坑showtime

Huxiaochun came to fengshu electronics to sign a strategic cooperation agreement with Zoomlion

声网,站在物联网的“土壤”里

UE4_ Editor development: highlight the UI making method according to the assets dragged by the mouse (1)

UE4_ Editor UMG close window cannot destroy UMG immediately

强烈推荐十几款IDEA开发必备的插件

At the beginning of 2022, people who are ready to change jobs should pay attention to

MySQL storage system

Summation of basic exercise sequence of test questions
随机推荐
Acwing winter vacation daily question 2022 punch in day 11
Transfer the token on the matic-erc20 network to the matic polygon
[regular expression series] greedy and non greedy patterns
Qt之QListView的简单使用(含源码+注释)
Intelligent deodorizer embedded development
Attempt to redefine 'timeout' at line 2 solution
二十四、输入输出设备模型(串口/键盘/磁盘/打印机/总线/中断控制器/DMA和GPU)
How to create a CSR (certificate signing request) file?
Implementation of property management system with ssm+ wechat applet
hashlips_ art_ Engine-1.0.6 usage
MySQL事物
The average salary of software testing in 2022 has been released. Have you been averaged?
Turn off automatic outlining in Visual Studio - turning off automatic outlining in Visual Studio
Xi'an Jiaotong 21st autumn economics online homework answer sheet (III) [standard answer]
VLAN access mode
Balanced binary tree judgment of Li Kou 110 -- classic problems
What are membrane stress and membrane strain
Detailed explanation of issues related to SSL certificate renewal
Xctf attack and defense world crypto advanced area
STM32F103 series controlled OLED IIC 4-pin