zoukankan      html  css  js  c++  java
  • UVAlive 6763 Modified LCS

    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<bits/stdc++.h>
    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
    ",ans);
        }
        return 0;
    }
    only strive for your goal , can you make your dream come true ?
  • 相关阅读:
    11. Container With Most Water(装最多的水 双指针)
    64. Minimum Path Sum(最小走棋盘 动态规划)
    数组相关
    88. Merge Sorted Array(从后向前复制)
    京东AI平台 春招实习生面试--NLP(offer)
    54. Spiral Matrix(矩阵,旋转打印)
    48. Rotate Image(旋转矩阵)
    春招实习--阿里 蚂蚁金服 支付宝 机器学习面试
    26. Remove Duplicates from Sorted Array(删除排序数组中的重复元素,利用排序的特性,比较大小)
    7. Reverse Integer(翻转整数)
  • 原文地址:https://www.cnblogs.com/hlmark/p/3928296.html
Copyright © 2011-2022 走看看