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:
- 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
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\n",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 }