1045. Favorite Color Stripe (30)
来源:互联网 发布:dagger 源码 编辑:程序博客网 时间:2024/05/16 09:20
这道题目有两种做法,第一种是求最长公共子序列,但是允许重复。第二种是求最长递增自序列。代码如下:
第一种:
#include<cstdio>using namespace std;#define N 201#define L 10001int len[N][L]={0};int fav[N]={0};int strip[L]={0};int main(){//freopen("1.txt","r",stdin); int n,m,l; int i,j,max; scanf("%d",&n); scanf("%d",&m); for(i=0;i<m;i++){ scanf("%d",&fav[i+1]); } scanf("%d",&l); for(i=0;i<l;i++){ scanf("%d",&strip[i+1]); } for(i=1;i<=m;i++){ for(j=1;j<=l;j++){ max = len[i-1][j-1]; if(max<len[i][j-1]) max = len[i][j-1]; if(max<len[i-1][j]) max = len[i-1][j]; if(fav[i]==strip[j]) len[i][j]=max+1; else{ len[i][j]=max; } } } printf("%d",len[m][l]);}
第二种:
#include<cstdio>using namespace std;#define N 200#define L 10001int dp[L]={0};int fav[N]={0};int strip[L]={0};int main(){ // freopen("1.txt","r",stdin); int n,m,l; int i,j,color; scanf("%d",&n); scanf("%d",&m); for(i=1;i<=m;i++){ scanf("%d",&color); fav[color]=i; } scanf("%d",&l); for(i=0;i<l;i++){ scanf("%d",&color); strip[i]=fav[color]; } for(i=0;i<l;i++){ if(strip[i]){ dp[i]=1; for(j=0;j<i;j++){ if(strip[j]<=strip[i]&&(dp[j]+1)>dp[i]) dp[i]=dp[j]+1; } } } printf("%d",dp[l-1]);}
0 0
- 1045. Favorite Color Stripe (30)
- 1045. Favorite Color Stripe (30)
- 1045. Favorite Color Stripe (30)
- 1045. Favorite Color Stripe (30)
- 1045. Favorite Color Stripe (30)
- 1045. Favorite Color Stripe (30)
- 1045. Favorite Color Stripe (30)
- 1045. Favorite Color Stripe (30)
- 1045. Favorite Color Stripe (30)
- 1045. Favorite Color Stripe (30)
- 1045. Favorite Color Stripe (30)
- 1045. Favorite Color Stripe (30)
- 1045. Favorite Color Stripe (30)
- 1045. Favorite Color Stripe (30)
- 1045. Favorite Color Stripe (30)
- 1045. Favorite Color Stripe (30)
- 1045. Favorite Color Stripe (30)
- 1045. Favorite Color Stripe (30)
- 警告: [SetContextPropertiesRule]{Context} Setting property 'source' to 'org.eclipse.jst.jee.server:spr
- Android事件分发机制完全解析,带你从源码的角度彻底理解(下)
- linux安装tomcat
- Service com.android.exchange.ExchangeService has leaked ServiceConnection
- 自动编译Java的Bat
- 1045. Favorite Color Stripe (30)
- 笑点集 4
- HDU 2546 饭卡
- 第十三周项目四:数组的排列(2)
- Shell中脚本变量和函数变量的作用域
- 第13周项目5-字符串操作(2.2)
- 二分查找
- iphone开发之UIImage应用与内存管理,UIImage加载图像方法
- Windows XP远程桌面控制图文教程