mirror of
https://gitcode.com/Zengtudor/alg2025.git
synced 2025-09-04 01:01:43 +00:00
update
This commit is contained in:
parent
704ffb6b91
commit
c200843ae3
@ -6,4 +6,5 @@
|
||||
## 区间dp
|
||||
1. 根据问题推出dp含义
|
||||
2. 根据规则写出dp的状态转移公式
|
||||
3. 处理边界问题
|
||||
3. 处理边界问题
|
||||
> dp[i][j], dp[0][0], dp[i][0], dp[0][j], dp[i][i], dp[j][j]
|
@ -11,9 +11,43 @@ dp[i][j]=min{
|
||||
dp[i-1][j-1]
|
||||
}+1
|
||||
|
||||
dp[i][j]=INT_MAX
|
||||
dp[0][0]=0
|
||||
dp[i][0]=i
|
||||
dp[0][j]=j
|
||||
*/
|
||||
|
||||
#include <algorithm>
|
||||
#include <climits>
|
||||
#include <cstdint>
|
||||
#include <iostream>
|
||||
#include <istream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
using ll = int64_t;
|
||||
|
||||
int main(){
|
||||
|
||||
std::iostream::sync_with_stdio(false);
|
||||
std::cin.tie(nullptr);
|
||||
std::string s1,s2;
|
||||
std::cin>>s1>>s2;
|
||||
ll n=s1.size(),m=s2.size();
|
||||
s1=' '+s1;
|
||||
s2=' '+s2;
|
||||
std::vector<std::vector<ll>> dp(s1.size(),std::vector<ll>(s2.size(),INT_MAX));
|
||||
for(ll i=0;i<dp.size();i++)dp[i][0]=i;
|
||||
for(ll j=0;j<dp[0].size();j++)dp[0][j]=j;
|
||||
for(ll i=1;i<=n;i++){
|
||||
for(ll j=1;j<=m;j++){
|
||||
if(s1[i]==s2[j]){
|
||||
dp[i][j]=dp[i-1][j-1];
|
||||
}else{
|
||||
dp[i][j]=std::min({
|
||||
dp[i-1][j],
|
||||
dp[i][j-1],
|
||||
dp[i-1][j-1]
|
||||
})+1;
|
||||
}
|
||||
}
|
||||
}
|
||||
std::cout<<dp[n][m]<<"\n";
|
||||
}
|
6
src/8/26/P7414.cpp
Normal file
6
src/8/26/P7414.cpp
Normal file
@ -0,0 +1,6 @@
|
||||
/*
|
||||
|
||||
dp[i][j]=从第i个到第j个涂成指定颜色需要的次数
|
||||
|
||||
|
||||
*/
|
Loading…
Reference in New Issue
Block a user