#10 D. LCIS (dp+最长公共上升子序列)
2017-04-28 21:03
239 查看
题目链接:
点击打开链接
http://codeforces.com/contest/10/problem/D
题意:
求最长公共上升子序列。
题解:
假设dp[ i ]表示第二个串位置为 i 的时候与第一个串的的最长公共上升子序列是多少。
枚举第一个串,再枚举第二个串。
for(int i = 1; i<=n ; i++)
{
int pos=0;
for(int j=1; j<=m;j++)
{
if(a[i]==b[j])
{
dp[j]=dp[pos]+1;
}
else if (a[i] > b[j] && dp[j]>dp[pos])
{
pos=j;
}
}
}
AC代码:
点击打开链接
http://codeforces.com/contest/10/problem/D
题意:
求最长公共上升子序列。
题解:
假设dp[ i ]表示第二个串位置为 i 的时候与第一个串的的最长公共上升子序列是多少。
枚举第一个串,再枚举第二个串。
for(int i = 1; i<=n ; i++)
{
int pos=0;
for(int j=1; j<=m;j++)
{
if(a[i]==b[j])
{
dp[j]=dp[pos]+1;
}
else if (a[i] > b[j] && dp[j]>dp[pos])
{
pos=j;
}
}
}
AC代码:
#include<bits/stdc++.h> using namespace std; int dp[567],a[567],b[567],step[567]; int n,m; void print(int x) { if(x==0)return; print(step[x]); printf("%d ",b[x]); } int main() { cin>>n; for(int i=1;i<=n;i++){ cin>>a[i]; } cin>>m; for(int j=1;j<=m;j++){ cin>>b[j]; } for(int i=1;i<=n;i++) { int pos=0; for(int j=1;j<=m;j++) { if(a[i]==b[j]) { dp[j]=dp[pos]+1; step[j]=pos; } else if(a[i] > b[j] && dp[j] > dp[pos]) { pos=j; } } } // for(int i=1;i<=m;i++)cout<<dp[i]<<" ";cout<<endl; int x=0; int ans=0; for(int i=1;i<=m;i++) { if(dp[i]>ans) { ans=dp[i]; x=i; } } cout<<ans<<endl; print(x); return 0; }
相关文章推荐
- CODE[VS] 最长公共上升子序列(LCIS)(序列型DP)
- [DP]最长公共上升子序列LCIS
- 最长公共上升子序列LCIS
- HDOJ Greatest Common Increasing Subsequence(LCIS最长公共上升子序列)
- 最长公共上升子序列 LCIS
- 最长公共上升子序列(LCIS)学习小记 Hdu 1423 + Poj 2127 (LCIS路径输出)
- 最长公共上升子序列(LCIS)的O(n^2)算法
- zoj2432 hdoj1423 最长公共上升子序列(LCIS)
- P1071 LCIS 最长公共上升子序列
- [CodeVS 2185] 最长公共上升子序列:DP
- 最长公共上升子序列的DP解法及其优化
- 最长公共上升子序列的DP解法及其优化
- 【HDU1423】【TYVJ1071】LCIS 最长公共上升子序列
- 【hdu1423】【DP】最长公共上升子序列
- [2017纪中10-25]凤凰院修真 LCIS最长公共上升子序列
- HDOJ 1423 最长公共上升子序列(LCIS 动态规划)
- 最长公共上升子序列LCIS
- CSU 1120 病毒 最长公共上升子序列(LCIS)
- Codevs_P2185 最长公共上升子序列(LCIS)
- 最长公共上升子序列(LCIS)