Codeforces Round #358 (Div. 2) D dp

本文介绍了一个关于寻找两个字符串中最大相似子串的问题。通过动态规划方法,实现了找到k个不相交子串,这些子串同时出现在两个字符串中,并保持相同的顺序,最终输出这些子串的最大总长度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



链接:戳这里


D. Alyona and Strings
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
After returned from forest, Alyona started reading a book. She noticed strings s and t, lengths of which are n and m respectively. As usual, reading bored Alyona and she decided to pay her attention to strings s and t, which she considered very similar.

Alyona has her favourite positive integer k and because she is too small, k does not exceed 10. The girl wants now to choose k disjoint non-empty substrings of string s such that these strings appear as disjoint substrings of string t and in the same order as they do in string s. She is also interested in that their length is maximum possible among all variants.

Formally, Alyona wants to find a sequence of k non-empty strings p1, p2, p3, ..., pk satisfying following conditions:

s can be represented as concatenation a1p1a2p2... akpkak + 1, where a1, a2, ..., ak + 1 is a sequence of arbitrary strings (some of them may be possibly empty);
t can be represented as concatenation b1p1b2p2... bkpkbk + 1, where b1, b2, ..., bk + 1 is a sequence of arbitrary strings (some of them may be possibly empty);
sum of the lengths of strings in sequence is maximum possible.
Please help Alyona solve this complicated problem and find at least the sum of the lengths of the strings in a desired sequence.

A substring of a string is a subsequence of consecutive characters of the string.

Input
In the first line of the input three integers n, m, k (1 ≤ n, m ≤ 1000, 1 ≤ k ≤ 10) are given — the length of the string s, the length of the string t and Alyona's favourite number respectively.

The second line of the input contains string s, consisting of lowercase English letters.

The third line of the input contains string t, consisting of lowercase English letters.

Output
In the only line print the only non-negative integer — the sum of the lengths of the strings in a desired sequence.

It is guaranteed, that at least one desired sequence exists.

Examples
input
3 2 2
abc
ab
output
2
input
9 12 4
bbaaababb
abbbabbaaaba
output
7
Note
The following image describes the answer for the second sample case:
题意:
给出s,p串,要求正好有k段在s,p中出现过,出现的顺序一样。输出最大的满足条件的k段长度

思路:
dp[i][j][k][0]表示当前为s[i],p[j]时,s[i]==p[j]正好分成了k段且第k段还没结束的答案
dp[i][j][k][1]表示当前s[i],p[j]时,结束了第k段的匹配,最优的答案
状态转移的时候,dp[i][j][k][0]=max(本身+1,dp[i-1][j-1][k][0]+1)
dp[i][j][k][1]=max(dp[i][j][k][0],dp[i-1][j][k][1],dp[i][j-1][k][1],dp[i-1][j-1][k][1])
类似于LCS的O(n*n)过程

代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
int n,m,K;
int dp[1010][1010][15][2];
char s[1010],p[1010];
int main(){
    scanf("%d%d%d",&n,&m,&K);
    scanf("%s",s+1);
    scanf("%s",p+1);
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(s[i]==p[j]){
                for(int k=1;k<=K;k++){
                    dp[i][j][k][0]=max(dp[i-1][j-1][k][0],dp[i-1][j-1][k-1][1])+1;
                }
            }
            for(int k=1;k<=K;k++){
                dp[i][j][k][1]=max(dp[i][j][k][1],dp[i-1][j][k][1]);
                dp[i][j][k][1]=max(dp[i][j][k][1],dp[i][j-1][k][1]);
                dp[i][j][k][1]=max(dp[i][j][k][1],dp[i-1][j-1][k][1]);
                dp[i][j][k][1]=max(dp[i][j][k][1],dp[i][j][k][0]);
            }
        }
    }
    printf("%d\n",dp[n][m][K][1]);
    return 0;
}





### 关于 Codeforces Round 997 Div. 2 的题目及解析 #### A. XOR Mixup 在这个问题中,给定了两个整数 \(a\) 和 \(b\) ,以及一个正整数 \(k\) 。目标是在不超过 \(k\) 步内通过交换 \(a\) 和 \(b\) 中任意一位来使得两者相等。如果可以在指定步数内完成,则返回 "YES";否则返回 "NO"[^1]。 对于这个问题的一个有效解决方案是计算不同位的数量并判断其是否小于等于两倍的 k 值加上 a 和 b 的二进制表示中最右边不同的位置索引之差。这是因为每一步最多能改变一对不匹配的位置状态。 ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while(t--) { long long a, b, k; cin >> a >> b >> k; bitset<32> ba(a), bb(b); int diff = 0; for(int i=0; i<32; ++i){ if(ba[i]!=bb[i])++diff; } cout << ((abs(__builtin_ctzll(a ^ b)) + 2 * k >= diff) ? "YES\n":"NO\n"); } } ``` #### B. Array Shrinking 此题描述了一个数组缩小的过程:允许选取连续子数组并将它们替换为其最大公约数值(GCD),直到整个数组变成单个元素为止。询问最终剩余的那个唯一数字是什么样的最小可能值[^2]? 解决方法涉及到动态规划的思想——维护一个二维表 dp[][],其中dp\[l\]\[r\] 表达的是区间 \([l,r]\) 能够被压缩成的最大 GCD 数字。转移方程基于枚举中间点 m 来分割原区间为更小子区间的组合方式实现更新。 ```cpp const int N = 2e5+7; long long gcd(long long a,long long b){return !b?a:gcd(b,a%b);} vector<int> v(N); unordered_map<long long,int> mp[N]; void solve(){ int n; scanf("%d",&n); for(int i=1;i<=n;++i)v[i]=rand()%N+1; memset(mp,0,sizeof(mp)); for(int len=1;len<=n;++len) for(int l=1;l+len-1<=n;++l){ int r=l+len-1; if(len==1)mp[l][v[l]]=1; else{ unordered_set<long long> st; for(auto &p : mp[l]) if(p.second>=len-1&&gcd(v[r],p.first)==v[r]){ printf("0");exit(0); }else{st.insert(gcd(v[r],p.first));} for(auto x:st)mp[l][x]++; } } puts(to_string(mp[1].begin()->first).c_str()); } signed main(){solve();} ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值