LightOJ1013 --- Love Calculator (dp)

Source: Internet
Author: User

LightOJ1013 --- Love Calculator (dp)

Yes, you are developing a 'love calculator'. The software wocould be quite complex such that nobody cocould 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:

The length of the shortest string that contains the names as subsequence.

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

F [I] [j] indicates that the first string matches I, and the second string matches the shortest length required by j.
Dp [I] [j] indicates the number of solutions under the length of f [I] [j]
Transfer is simple
Note the following:
If A [I] = B [j] When calculating the number of solutions F [I? 1] [j? 1] If F [I? 1] [j] f [I] [j? 1] Transfer, repeated

/*************************************** * *********************************> File Name: lightOJ1013.cpp> Author: ALex> Mail: zchao1995@gmail.com> Created Time: october 21 ******************************** **************************************** /# include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
        # Include
       
         # Include
        
          # Include
         
           # Include
          
            # Include
           # Include
            
              # Include
             
               # Include
              
                Using namespace std; const double pi = acos (-1.0); const int inf = 0x3f3f3f; const double eps = 1e-15; typedef long LL; typedef pair
               
                 PLL; string A, B; LL dp [50] [50]; int f [50] [50]; int main () {int t, icase = 1; scanf ("% d", & t); while (t --) {cin> A> B; int n =. length (); int m = B. length (); memset (dp, 0, sizeof (dp); dp [0] [0] = 1; memset (f, inf, sizeof (f )); f [0] [0] = 0; for (int I = 1; I <= n; ++ I) {f [I] [0] = I; dp [I] [0] = 1 ;}for (int j = 1; j <= m; ++ j) {f [0] [j] = j; dp [0] [j] = 1 ;}for (int I = 1; I <= n; ++ I) {for (int J = 1; j <= m; ++ j) {if (f [I] [j]> f [I-1] [j] + 1) {f [I] [j] = f [I-1] [j] + 1 ;} if (f [I] [j]> f [I] [j-1] + 1) {f [I] [j] = f [I] [j-1] + 1;} if (A [I-1] = B [j-1]) {if (f [I] [j]> f [I-1] [j-1] + 1) {f [I] [j] = f [I-1] [j-1] + 1 ;}} if (f [I] [j] = f [I-1] [j] + 1 & A [I-1]! = B [j-1]) {dp [I] [j] + = dp [I-1] [j];} if (f [I] [j] = f [I] [j-1] + 1 & A [I-1]! = B [j-1]) {dp [I] [j] + = dp [I] [j-1];} if (A [I-1] = B [j-1] & f [I] [j] = f [I-1] [j-1] + 1) {dp [I] [j] + = dp [I-1] [j-1] ;}} printf ("Case % d: % d % lld \ n ", icase ++, f [n] [m], dp [n] [m]);}
               
              
             
            
          
         
        
       
      
     
    
   
  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.