博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
1045 Favorite Color Stripe (最长不下降子序列 LIS 或最长公共子序列 LCS)
阅读量:4658 次
发布时间:2019-06-09

本文共 4253 字,大约阅读时间需要 14 分钟。

Eva is trying to make her own color stripe out of a given one. She would like to keep only her favorite colors in her favorite order by cutting off those unwanted pieces and sewing the remaining parts together to form her favorite color stripe.

It is said that a normal human eye can distinguish about less than 200 different colors, so Eva's favorite colors are limited. However the original stripe could be very long, and Eva would like to have the remaining favorite stripe with the maximum length. So she needs your help to find her the best result.

Note that the solution might not be unique, but you only have to tell her the maximum length. For example, given a stripe of colors {2 2 4 1 5 5 6 3 1 1 5 6}. If Eva's favorite colors are given in her favorite order as {2 3 1 5 6}, then she has 4 possible best solutions {2 2 1 1 1 5 6}, {2 2 1 5 5 5 6}, {2 2 1 5 5 6 6}, and {2 2 3 1 1 5 6}.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (≤) which is the total number of colors involved (and hence the colors are numbered from 1 to N). Then the next line starts with a positive integer M(≤) followed by M Eva's favorite color numbers given in her favorite order. Finally the third line starts with a positive integer L (≤) which is the length of the given stripe, followed by L colors on the stripe. All the numbers in a line a separated by a space.

Output Specification:

For each test case, simply print in a line the maximum length of Eva's favorite stripe.

Sample Input:

65 2 3 1 5 612 2 2 4 1 5 5 6 3 1 1 5 6

Sample Output:

7

  

一开始没审清题意,这里喜欢的颜色序列是可以不全部出现的,所以只有27/30分:

#include
#include
using namespace std;struct node{ int index=1; int data=1;}c[10010];int a[220];int b[10010];int main(){ int n,m,k; scanf("%d",&n); scanf("%d",&m); for(int i=1;i<=m;i++) scanf("%d",&a[i]); scanf("%d",&k); for(int i=1;i<=k;i++) scanf("%d",&b[i]); int maxx=-1,data,index,i,temp; for(i=1;i<=k;i++){ if(b[i]==a[1]){ temp=i; break; } } for(i=temp+1;i<=k;i++){ for(int j=temp;j
c[i].data)){ c[i].data=data+ 1; c[i].index=index; } }else if(b[i]==a[index]){ if(data+1>c[i].data){ c[i].data=data+1; c[i].index=index; } }else if(b[i]==a[index+1]){ if(data+1>c[i].data){ c[i].data=data+1; c[i].index=index+1; } } if(c[i].data>maxx) maxx=c[i].data; } } printf("%d",maxx); return 0;}

 

而且这种思路较为复杂,可以将喜欢的颜色通过hash按顺序映射到一个递增序列,即可方便转化为LIS问题:

#include
#include
using namespace std;int dp[10010];int b[10010];int hash[220]={
0};int main(){ int n,m,k,temp,now=1,now1=1,Max=-1; scanf("%d%d",&n,&m); for(int i=1;i<=m;i++){ scanf("%d",&temp); hash[temp]=now++; } scanf("%d",&k); for(int i=1;i<=k;i++){ scanf("%d",&temp); if(hash[temp]!=0){ b[now1++]=temp; } } for(int i=1;i
dp[i]){ dp[i]=dp[j]+1; } if(dp[i]>Max) Max=dp[i]; } printf("%d",Max); return 0;}

 

考虑使用LCS,这里的特殊点是公共部分可以存在重复元素,故对原模型进行修改

得出递推式

dp[i][j]=max(dp[i][j-1],dp[i-1][j])+1            如果a[i]等于b[j]的话

dp[i][j]=max(dp[i][j-1],dp[i-1][j])                  a[i]!=b[j]的话

#include
#include
using namespace std;int a[220];int b[10010];int dp[220][10010];int main(){ int n,lenA,lenB,temp; scanf("%d%d",&n,&lenA); for(int i=1;i<=lenA;i++) scanf("%d",&a[i]); scanf("%d",&lenB); for(int i=1;i<=lenB;i++) scanf("%d",&b[i]); for(int i=0;i<=lenA;i++) dp[i][0]=0; for(int j=0;j<=lenB;j++) dp[0][j]=0; for(int i=1;i<=lenA;i++){ for(int j=1;j<=lenB;j++){ if(a[i]==b[j]) dp[i][j]=max(dp[i-1][j],dp[i][j-1])+1; else dp[i][j]=max(dp[i-1][j],dp[i][j-1]); } } printf("%d",dp[lenA][lenB]); return 0;}

 

 

转载于:https://www.cnblogs.com/exciting/p/10438330.html

你可能感兴趣的文章
表格-鼠标经过单元格变色(暂不支持IE6)
查看>>
【每日一学】pandas_透视表函数&交叉表函数
查看>>
实时读取日志文件
查看>>
【寒假集训系列2.12】
查看>>
2018牛客多校第六场 I.Team Rocket
查看>>
Vuex了解
查看>>
c++初始化函数列表
查看>>
JS的this总结(上)-call()和apply()
查看>>
ADO.net 增删改查小练习
查看>>
HDU5795A Simple Nim SG定理
查看>>
2018.10.30 NOIp模拟赛 T1 改造二叉树
查看>>
九度oj 题目1074:对称平方数
查看>>
Zookeeper原理 二
查看>>
android之APP+JNI+Drv框架
查看>>
三阶魔方公式
查看>>
BP算法
查看>>
P1855 榨取kkksc03
查看>>
JAVA运行时动态加载类
查看>>
linux ifconfig -a
查看>>
MySql通过数据库文件恢复数据库
查看>>