This commit is contained in:
Zengtudor 2024-10-18 21:32:20 +08:00
parent a4290931ad
commit 91475db2a6
5 changed files with 172 additions and 0 deletions

44
src/P2758/P2758.cpp Normal file
View File

@ -0,0 +1,44 @@
#include <algorithm>
#include <bits/stdc++.h>
#include <limits>
using ll = long long;
auto &is = std::cin;auto &os = std::cout;
const ll max_n {2000+5}, ll_max{std::numeric_limits<decltype(ll_max)>::max()};
ll dp[max_n][max_n]{};
std::string a,b;
/*
dp[i][j]i个字符转化为j需要的最短编辑距离
*/
int main(){
is>>a>>b;
const ll a_size{(ll)a.size()}, b_size{(ll)b.size()};
a=' '+a,b=' '+b;
// for(ll i{0};i<max_n;i++)
// for(ll j{0};j<max_n;j++)
// dp[i][j]=ll_max;
for(ll i{0};i<std::max({a.size(),b.size()});i++){
dp[0][i]=i;
dp[i][0]=i;
}
for(ll i{1};i<=a_size;i++){
for(ll j{1};j<=b_size;j++){
if(a[i]==b[j]){
dp[i][j]=dp[i-1][j-1];
}else{
dp[i][j] = std::min({
dp[i-1][j]+1, //删除
dp[i][j-1]+1, //增加
dp[i-1][j-1]+1//修改
});
}
}
}
os<<dp[a_size][b_size]<<'\n';
}

23
src/P2758/P2758.py Normal file
View File

@ -0,0 +1,23 @@
max_n:int = 2000+5
a:str='\0'+input().strip()
b:str='\0'+input().strip()
dp:list[list[int]] = [[max_n]*(len(b)+1) for _ in range(len(a)+1)]
# print(dp)
for i in range(0,len(a)+1):
dp[i][0]=i
for i in range(0,len(b)+1):
dp[0][i]=i
for i in range(1,len(a)):
for j in range(1,len(b)):
if a[i]==b[j]:
dp[i][j] = dp[i-1][j-1]
continue
dp[i][j]=min(dp[i-1][j],dp[i][j-1],dp[i-1][j-1])+1
print(dp[len(a)-1][len(b)-1])

View File

@ -0,0 +1,49 @@
#include <bits/stdc++.h>
#include <limits>
// using namespace std;
using ll = long long;
const ll max_n = 400+5;
ll n,k;
ll a[max_n];
ll dp[max_n][max_n], waste[max_n][max_n];
/*
dp[i][j] i条蛇使j次调整大小的浪费空间的最小值
i,j,k
k分为两段1~k,k+1~i
dp[i][j] = min(dp[i][j], dp[k][j-1]+waste[k+1][i])
waste[i][j] i~j浪费的数量
*/
const ll ll_max{std::numeric_limits<ll>::max()};
int main(){
std::cin>>n>>k;
for(ll i{1};i<=n;i++)std::cin>>a[i];
for(ll i{0};i<max_n;i++)
for(ll j{0};j<max_n;j++)
dp[i][j]=ll_max;
for(ll i{1};i<n;i++){
ll max_ai{a[i]},sum{a[i]};
// std::cout<<waste[i][i]<<' ';
for(ll j{i+1};j<=n;j++){
max_ai=std::max(a[j],max_ai);
sum+=a[j];
waste[i][j] = max_ai*(j-i+1)-sum;
// std::cout<<waste[i][j]<<' ';
}
dp[i][0] = waste[1][i];
// std::cout<<'\n';
}
for(ll j{1};j<=k;j++){
for(ll i{j};i<=n;i++){
for(ll k{1};k<i;k++){
if(dp[k][j-1]==ll_max)continue;
dp[i][j] = std::min(dp[i][j], dp[k][j-1]+waste[k+1][i]);
}
}
}
std::cout<<dp[n][k]<<'\n';
}

31
src/oj1808/oj1808.cpp Normal file
View File

@ -0,0 +1,31 @@
#include <algorithm>
#include <iostream>
#include <string>
using ll = long long;
auto &is = std::cin;
auto &os = std::cout;
const ll max_n{200+5};
std::string a,b;
ll dp[max_n][max_n];
int main(){
while (is>>a>>b) {
a=' '+a;
b=' '+b;
for(ll i{1};i<a.size();i++){
for(ll j{1};j<b.size();j++){
if(a[i]==b[j]){
dp[i][j]=dp[i-1][j-1]+1;
}else{
dp[i][j]=std::max({dp[i][j-1],dp[i-1][j]});
}
}
}
os<<dp[a.size()-1][b.size()-1]<<'\n';
}
}

25
src/oj1808/oj1808.py Normal file
View File

@ -0,0 +1,25 @@
import re
from typing import List
max_n = 200+5
dp:List[List[int]] = [[0]*max_n for _ in range(max_n)]
while True:
try:
raw_str = input().strip()
a, b = map(lambda x:" "+x, re.split(r"\s+",raw_str,maxsplit=1))
except EOFError:
break
for i in range(1,len(a)):
for j in range(1,len(b)):
if a[i] == b[j]:
# print(i,j,"ok")
dp[i][j] = dp[i-1][j-1] + 1
else:
dp[i][j] = max(dp[i-1][j], dp[i][j-1])
# print(dp,repr(a),repr(b))
print(dp[len(a)-1][len(b)-1])