Skip to content

Commit 384c451

Browse files
committed
update codeforces/1446.md
1 parent bebbf64 commit 384c451

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

codeforces/DP/1446.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
本质上还是LCS,但是相对最初的LCS而言,状态转换不同。
2+
所求的`4LCS(a,b) - len(a) - len(b)`,我们很显然需要想办法记录产生某个序列产生的贡献,剔除不产生贡献的串,一开始的方案是先求出LCS,
3+
然后O(n^3)的DP来做,在CF的机器上是可行的,但是,需要枚举不产生贡献的串,本身也是个时间复杂度可能接近O(n)的操作,
4+
后续:
5+
需要考虑状态转移方程
6+
![](../images/1146状态转移方程.png)
7+
对于一个位置当前参与了形成LCS的话,显然可以得到`4*1-1-1`的贡献,否则的话,加上不形成序列的贡献,就是`-1`
8+
9+
```cpp
10+
void solve(){
11+
int n, m;
12+
cin >> n >> m;
13+
string a, b;
14+
cin >> a >> b;
15+
vector <vector<int>> f(n + 1, vector <int>(m + 1));
16+
f[0][0] = 0;
17+
int ans = 0;
18+
for (int i = 1; i <= n; i++) {
19+
for (int j = 1; j <= m; j++) {
20+
if(a[i - 1] == b[j - 1]) chkmax(f[i][j], f[i - 1][j - 1] + 2);
21+
else {
22+
chkmax(f[i][j], f[i - 1][j] - 1);
23+
chkmax(f[i][j], f[i][j - 1] - 1);
24+
}
25+
chkmax(ans, f[i][j]);
26+
}
27+
}
28+
cout << ans << endl;
29+
}
30+
```

images/1146状态转移方程.png

3.38 KB
Loading

0 commit comments

Comments
 (0)