zoukankan      html  css  js  c++  java
  • 最长公共子序列变形

    Yes, you are developing a 'Love calculator'. The software would be quite complex such that nobody could crack the exact behavior of the software.

    So, given two names your software will generate the percentage of their 'love' according to their names. The software requires the following things:

    1. The length of the shortest string that contains the names as subsequence.
    2. Total number of unique shortest strings which contain the names as subsequence.

    Now your task is to find these parts.


    Input

    Input starts with an integer T (≤ 125), denoting the number of test cases.

    Each of the test cases consists of two lines each containing a name. The names will contain no more than 30 capital letters.

    Output

    For each of the test cases, you need to print one line of output. The output for each test case starts with the test case number, followed by the shortest length of the string and the number of unique strings that satisfies the given conditions.

    You can assume that the number of unique strings will always be less than 263. Look at the sample output for the exact format.

    Sample Input

    3

    USA

    USSR

    LAILI

    MAJNU

    SHAHJAHAN

    MOMTAJ

    Sample Output

    Case 1: 5 3

    Case 2: 9 40

    Case 3: 13 15

    题意 : 两个小问,第一问是求一个最短的长度构成的序列同时包含这两个序列,第二问是求构成这个最短序列的方案数

    思路分析 : 第一问就是用两个串的长度减去 lcs

          第二问定义 dp[i][j][k] 表示第一个串用了 i 个字符, 第二个串用了j 个字符,并且当前匹配用去了 k 个字符的方案数

        转移过程类似求 lcs 的过程

    代码示例:

    #define ll long long
    
    char a[100], b[100];
    ll dp[100][100][100];
    ll dp2[100][100];
    ll lena, lenb, num;
    
    void solve(){
        
        lena = strlen(a+1);
        lenb = strlen(b+1);
        memset(dp2, 0, sizeof(dp2)); 
        for(ll i = 1; i <= lena; i++){
            for(ll j = 1; j <= lenb; j++){
                if (a[i] == b[j]) dp2[i][j] = dp2[i-1][j-1]+1;
                else dp2[i][j] = max(dp2[i-1][j], dp2[i][j-1]);
            }
        }
        memset(dp, 0, sizeof(dp));
        dp[0][0][1] = 1;
        num = dp2[lena][lenb];
        for(ll i = 1; i <= lena; i++) dp[i][0][1] = 1;
        for(ll i = 1; i <= lenb; i++) dp[0][i][1] = 1;
         
        for(ll i = 1; i <= lena; i++){
            for(ll j = 1; j <= lenb; j++){
                for(ll k = 1; k <= num+1; k++){
                    if (a[i] == b[j]) dp[i][j][k] = dp[i-1][j-1][k-1];
                    else dp[i][j][k] = dp[i-1][j][k]+dp[i][j-1][k]; 
                }
            }
        }
     
    }
    
    int main() {
        //freopen("in.txt", "r", stdin);
        //freopen("out.txt", "w", stdout);
        ll t;
        ll cas = 1;
        
        cin >> t;
        while(t--){
            scanf("%s%s", a+1, b+1);    
            solve();
            printf("Case %lld: %lld %lld
    ", cas++, lena+lenb-num, dp[lena][lenb][num+1]);
        }
        return 0;
    }
    
  • 相关阅读:
    一种利用异常机制基于MVC过滤器的防止重复提交的机制分享
    泛型算法参考手册
    Http协议中get和post的区别----学习笔记
    Apache Commons工具类学习(一)-----CSV
    Appium 相关学习(三) 使用webdriver截图以及app点击事件
    Appium 相关学习(二) testng中的Assert类
    Appium 相关学习(一)
    Java 获取一个当前系统可以用的端口
    Java图片base64编码解码,接口使用
    myeclipse中运行maven web项目时tomcat启动报错——解决方法
  • 原文地址:https://www.cnblogs.com/ccut-ry/p/10296013.html
Copyright © 2011-2022 走看看