zoukankan      html  css  js  c++  java
  • Light OJ 1013 Love Calculator

    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

    详解:http://www.cnblogs.com/chenchengxun/p/4903430.html

    好难啊,DP。

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<iostream>
     4 #include<algorithm>
     5 using namespace std;
     6 typedef long long ll;
     7 
     8 int n,m;
     9 ll dp[65][35][35];
    10 char s1[50],s2[50];
    11 
    12 void solve(int t){
    13     memset(dp,0,sizeof(dp));
    14     for(int i=0;i<=n;i++) dp[i][i][0]=1;
    15     for(int i=0;i<=m;i++) dp[i][0][i]=1;
    16     for(int l=0;l<n+m;l++){
    17         for(int i=0;i<n;i++){
    18             for(int j=0;j<m;j++){
    19                 if(s1[i]==s2[j]) dp[l+1][i+1][j+1]+=dp[l][i][j];            //因为相同,所以放一个字母就行
    20                 else dp[l+1][i+1][j+1]+=dp[l][i+1][j]+dp[l][i][j+1];        //要么放s1[i],要么放s2[j]
    21             }
    22         }
    23     }
    24     for(int i=1;i<=n+m;i++) if(dp[i][n][m]) { printf("Case %d: %d %lld
    ",t,i,dp[i][n][m]); break; }
    25 }
    26 
    27 int main()
    28 {   int kase;
    29     cin>>kase;
    30     for(int t=1;t<=kase;t++){
    31         scanf("%s%s",s1,s2);
    32         n=strlen(s1),m=strlen(s2);
    33         solve(t);
    34     }
    35     return 0;
    36 }
  • 相关阅读:
    【剑指offer】不使用新变量,交换两个变量的值,C++实现
    【剑指offer】不用加减乘除做加法,C++实现
    【剑指offer】求1+2+…+n,C++实现
    【剑指offer】左旋转字符串,C+实现
    给记事本添加接口,并通过菜单来执行自定义的功能
    修改PE文件的入口函数OEP
    360搜集隐私程序员级分析,供方舟子及大众参考
    Android窃取用户信息新思路
    如何整治那些敢偷用你Wi-Fi的人
    保护WIFI无线网络的安全
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/7306793.html
Copyright © 2011-2022 走看看