zoukankan      html  css  js  c++  java
  • (最长公共子序列+推导)Love Calculator (lightOJ 1013)

     

    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

    Output for Sample Input

    3

    USA

    USSR

    LAILI

    MAJNU

    SHAHJAHAN

    MOMTAJ

    Case 1: 5 3

    Case 2: 9 40

    Case 3: 13 15


    题目大意:
    给你两个串, 让你求出这两个串能组成的最短串的长度,以及最短串多少种不同的串(第三个串中保证有最长公共字串)
     
    最长公共子序列, 在求最长公共子序列的过程中加上每个串有多少种组成方法
     
    dp[i][j] 代表第一个串的前i个和第二个串的前j个的最长公共子序列
    num[i][j]代表第一个串的前i个和第二个串的钱j个能组成多少种不同的串
     
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <queue>
    #include <algorithm>
    
    using namespace std;
    
    #define N 1100
    
    #define met(a,b) (memset(a,b,sizeof(a)))
    typedef long long LL;
    
    int a[20][20], dp[50][50];
    LL num[50][50];
    
    int main()
    {
        int T, iCase=1;
    
        scanf("%d", &T);
    
        while(T--)
        {
            char s1[50], s2[50];
            int i, j, len1, len2;
    
            met(dp, 0);
            met(num, 0);
            scanf("%s%s", s1, s2);
            len1 = strlen(s1);
            len2 = strlen(s2);
    
    
            for(i=0; i<=len1; i++)
                num[i][0] = 1;
            for(i=0; i<=len2; i++)
                num[0][i] = 1;
    
            for(i=1; i<=len1; i++)
            for(j=1; j<=len2; j++)
            {
                if(s1[i-1]==s2[j-1])
                {
                    dp[i][j] = dp[i-1][j-1] + 1;
                    num[i][j] += num[i-1][j-1];
                }
                else
                {
                    if(dp[i-1][j]>dp[i][j-1])
                    {
                        dp[i][j] = dp[i-1][j];
                        num[i][j] = num[i-1][j];
                    }
                    else if(dp[i-1][j]<dp[i][j-1])
                    {
                        dp[i][j] = dp[i][j-1];
                        num[i][j] = num[i][j-1];
                    }
                    else
                    {
                        dp[i][j] = dp[i-1][j];
                        num[i][j] = num[i-1][j] + num[i][j-1];
                    }
                }
            }
    
            printf("Case %d: %d %lld
    ", iCase++, len1+len2-dp[len1][len2], num[len1][len2]);
        }
        return 0;
    }
    
    /**
    3
    USA
    USSR
    LAILI
    MAJNU
    SHAHJAHAN
    MOMTAJ
    */

    自己写完后, 搜题解看到的另一种写法, 由于dp刚入门, 就学习一下思想

    dp[C串的长度][包含A的字符个数][包含B的字符个数] = 种类数

    状态转移:如果 A[i] == B[j] 那么 dp[k][i][j] = dp[k-1][i-1][j-1]. 就是说我最后一个字符是相同的那么我只要放一个就可以了。

         如果 A[i] !=  B[j] 那么 dp[k][i][j] = dp[k-1][i-1][j] + dp[k-1][i][j-1].最后一个字符我们要么放A[i] 要么放 B[j] 就这两种情况了。

    然后关于找最短的,就可以在 dp[k][lenA][lenB] 种找到最小的k即可。

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <queue>
    #include <algorithm>
    
    using namespace std;
    
    #define N 1100
    
    #define met(a,b) (memset(a,b,sizeof(a)))
    typedef long long LL;
    
    LL dp[100][50][50];
    
    int main()
    {
        int T, iCase=1;
    
        scanf("%d", &T);
    
        while(T--)
        {
            char s1[50], s2[50];
            int i, j, k, len1, len2;
    
            met(dp, 0);
            scanf("%s%s", s1, s2);
            len1 = strlen(s1);
            len2 = strlen(s2);
    
    
            for(i=0; i<=len1; i++)
                dp[i][i][0] = 1;
            for(i=0; i<=len2; i++)
                dp[i][0][i] = 1;
    
            for(i=1; i<=len1+len2; i++)
            {
                for(j=1; j<=len1; j++)
                for(k=1; k<=len2; k++)
                {
                    if(s1[j-1]==s2[k-1])
                        dp[i][j][k] = dp[i-1][j-1][k-1];
                    else
                        dp[i][j][k] = dp[i-1][j-1][k] + dp[i-1][j][k-1];
                }
            }
    
            for(k=1; k<=len1+len2; k++)
                if(dp[k][len1][len2]) break;
    
            printf("Case %d: %d %lld
    ", iCase++, k, dp[k][len1][len2]);
        }
        return 0;
    }
    
    /**
    3
    USA
    USSR
    LAILI
    MAJNU
    SHAHJAHAN
    MOMTAJ
    */
  • 相关阅读:
    Bellman-Ford算法
    POJ3468--A Simple Problem with Integers(Splay Tree)
    【组队训练】2014鞍山区域赛
    Educational Codeforces Round 85 (Rated for Div. 2)
    HDU6061 RXD and functions【NTT】
    HDU6434 Count【欧拉函数 线性筛】
    2017-2018 ACM-ICPC German Collegiate Programming Contest (GCPC 2017)(9/11)
    2015 German Collegiate Programming Contest (GCPC 15) + POI 10-T3(12/13)
    CodeCraft-20 (Div. 2)
    图论题集
  • 原文地址:https://www.cnblogs.com/YY56/p/5528391.html
Copyright © 2011-2022 走看看