博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UVAlive 6763 Modified LCS
阅读量:7122 次
发布时间:2019-06-28

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

LCS stands for longest common subsequence, and it is a well known problem. A sequence in this

problem means a list of integers, and a sequence X is considered a subsequence of another sequence Y ,
when the sequence X can be obtained by deleting zero or more elements from the sequence Y without
changing the order of the remaining elements.
In this problem you are given two sequences and your task is to nd the length of the longest
sequence which is a subsequence of both the given sequences.
You are not given the sequences themselves. For each sequence you are given three integers N, F
and D, where N is the length of the sequence, F is the rst element in the sequence. Each element
except the rst element is greater than the element before it by D.
For example N = 5, F = 3 and D = 4 represents the following sequence: [3, 7, 11, 15, 19].
There will be at least one integer which belongs to both sequences and it is not greater than
1,000,000.
Input
Your program will be tested on one or more test cases. The rst line of the input will be a single integer
T, the number of test cases (1 T 100). Followed by the test cases, each test case is described in one
line which contains 6 integers separated by a single space N1 F1 D1 N2 F2 D2 (1 N1;N2 1018)
and (1 F1;D1; F2;D2 109) representing the length of the rst sequence, the rst element in the
rst sequence, the incremental value of the rst sequence, the length of the second sequence, the rst
element in the second sequence and the incremental value of the second sequence, respectively.
Output
For each test case, print a single line which contains a single integer representing the length of the
longest common subsequence between the given two sequences.
Sample Input
3
5 3 4 15 3 1
10 2 2 7 3 3
100 1 1 100 1 2
Sample Output
4
3

 

这条题是组队排位的中下题。小邪卡了很久。

解法其实就是用扩展欧几里得算法求出第一个出现的相等的位置。

f1  + (x-1)*d1 = f2 + (y1 -1 )*d2;

除去前面的位置,接下来就是算两个等差序列有多少段出现LCS,然后取较小那个就可以了

 

 

#include
using namespace std;typedef long long LL;void e_gcd(LL a,LL b,LL &x,LL &y,LL &d){ if( !b ){ d = a,x = 1 ,y = 0; return ;} e_gcd(b,a%b,y,x,d); y -= x * (a / b);}int main(){ int _; LL k1,k2,c; LL x,y,L1,L2,R1,R2,ML,MR,d; LL f1,f2,d1,d2,n1,n2; scanf("%d",&_); while(_--){ LL ans=0; scanf("%lld%lld%lld%lld%lld%lld",&n1,&f1,&d1,&n2,&f2,&d2); c = f1 -f2; d = __gcd(d1,d2); if( c % d ){puts("0");continue;} e_gcd(d1,d2,x,y,d); k1 = -x * (c/d); k2 = y * (c/d); d1 /= d , d2 /= d; L1 = ceil( (-k1*1.0)/d2 ); L2 = ceil( (-k2*1.0)/d1 ); R1 = floor( (n1-k1) * 1.0 / d2); R2 = floor( (n2-k2) * 1.0 / d1); if( (n1 - k1) %d2 ==0 )R1 --; if( (n2 - k2) %d1 ==0 )R2 --; ML = max(L1,L2); MR = min(R1,R2); ans = max(0LL,MR - ML +1 ); printf("%lld\n",ans); } return 0;}

 

转载于:https://www.cnblogs.com/hlmark/p/3928296.html

你可能感兴趣的文章
db2 数据库基本操作
查看>>
JSONObject与JSONArray的使用(详细)
查看>>
Nginx+Tomcat动静分离及Nginx优化
查看>>
heroku全体验
查看>>
MySQL高级查询
查看>>
安装Oracle,Enterprise Manger配置失败
查看>>
zuul源码分析--Filter注册
查看>>
jquery mobile + sae开发手记
查看>>
利用函数的惰性载入提高 javascript 代码性能
查看>>
PHP乱码
查看>>
iOS label中间加横线的实现
查看>>
golang调用ping命令出现too many open files
查看>>
【批处理】批处理中的一些特殊技巧汇总(2015-6-26)
查看>>
js从前端访问到springMVC后台处理返回页面的过程
查看>>
课程第七天内容《基础交换七》
查看>>
python用profile、hotshot、timeit协助程序性能优化
查看>>
Redis集群方案(codis)
查看>>
Spring整合MyBatis的几种方式
查看>>
Linux服务器开发常用的命令以及遇到的问题
查看>>
Welcome to WANGFRAME
查看>>