CF Round #358 D
来源:互联网 发布:欧姬丝洗发水 知乎 编辑:程序博客网 时间:2024/06/05 04:10
题意:给出两个字符串,和一个正整数k,在每个串中找出k个子串,使得这k个字串按顺序对应相等。求满足以上条件的k个子串长度和的最大值。其中子串长不超过1000,k不超过10。
做法:dp
dp[i][x][y]表示a串第x结尾,b串第y结尾,i个子串的最大长度。
每做一次更新一遍pro[x][y],为(x,y)以前的最大值。
最后取pro[m][n]即可。
代码:
#include <cstdio>#include <iostream>#include <cstring>#include <queue>#include <algorithm>#include <cmath>using namespace std;int dp[11][1050][1050];int pro[1050][1050];char s[1050];char t[1050];int n,m,k;int main(){ scanf("%d%d%d",&n,&m,&k); scanf("%s",s+1); scanf("%s",t+1); memset(dp,0,sizeof(dp)); for(int x=1;x<=n;x++) for(int y=1;y<=m;y++) { if(s[x]==t[y])dp[1][x][y]=1+dp[1][x-1][y-1]; } memset(pro,0,sizeof(pro)); for(int x=1;x<=n;x++) for(int y=1;y<=m;y++) { pro[x][y]=dp[1][x][y]; if(pro[x][y]<pro[x-1][y])pro[x][y]=pro[x-1][y]; if(pro[x][y]<pro[x][y-1])pro[x][y]=pro[x][y-1]; } for(int i=2;i<=k;i++) { for(int x=1;x<=n;x++) for(int y=1;y<=m;y++) { int len=dp[1][x][y]; dp[i][x][y]=len+pro[x-len][y-len]; } memset(pro,0,sizeof(pro)); for(int x=1;x<=n;x++) for(int y=1;y<=m;y++) { pro[x][y]=max(dp[i][x][y],pro[x][y-1]); pro[x][y]=max(pro[x][y],pro[x-1][y]); } } printf("%d\n",pro[n][m]); return 0;}
0 0
- CF Round #358 D
- CF round#205 D
- CF - round # 280 - D
- CF round #79 div2 D bues
- CF Round 235 D 状态压缩 dp
- CF Round #240 (Div. 2) D
- CF初体验--Round #348,D
- cf Educational Codeforces Round 26 D. Round Subset
- cf-Round #216 (Div. 2) -D Valera and Fools-dfs
- CF MemSQL Start[c]UP 2.0 - Round 1 D
- cf Round #292 (Div. 2)D. Drazil and Tiles 构造
- cf Round #276 (Div. 2) D. Maximum Value
- Cf Round #301 (Div. 2) D. Bad Luck Island
- CF Round #324 (Div2) D. Dima and lisa
- CF Round #325 Div2 D. Phillip and Trains
- Cf Round #360 (Div. 1) 687D Dividing Kingdom II
- Cf Round #361 (Div. 2) 689D. Friends and Subsequences
- CF Round #362 (Div. 2) 697D. Puzzles
- leecode_357 Count Numbers with Unique Digits
- ruby编程中的异常处理
- Permutations
- 转自杨肉的演讲台
- 【翻译】Tweepy 3.5.0 Doc (3) Code Snippets
- CF Round #358 D
- leecode_324 Wiggle Sort II
- State bank of India Shimla Himachal Pradesh Ifsc Codes
- Android 播放视频并获取指定时间的帧画面
- 整理Ruby相关的各种概念(rvm, gem, bundle, rake, rails等)
- Caffe小玩意(2)-从caffemodel中导出参数
- 运维中的一些概念scale-in/scale-out/scale-up/scale-down/auto-scaling
- Maximum Product Subarray
- leetcode #105 in cpp