Love Calculator
Time Limit: 2000msMemory Limit: 32768KB 64-bit integer IO format: %lld Java class name: Main
Submit Status
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
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
先跑一遍LCS,再DP求出总的方案数。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define ll long long
using namespace std;
const int maxn = 35;
char str1[maxn],str2[maxn];
int f[maxn][maxn];
ll dp[maxn][maxn][maxn*2];
int main()
{
//freopen("in.txt","r",stdin);
int T;
scanf("%d",&T);
int cas = 1;
while( T-- )
{
scanf("%s %s",str1+1,str2+1);
int len1 = strlen( str1+1 );
int len2 = strlen( str2+1 );
memset(f,0,sizeof(f));
for( int i = 1; i <= len1; i++ )
for( int j = 1; j <= len2; j++ )
{
if( str1[i] == str2[j] )
f[i][j] = f[i-1][j-1] + 1;
else
f[i][j] = max( f[i-1][j],f[i][j-1] );
}
int res = len1+len2-f[len1][len2];
for( int i = 0; i <= len1; i++ )
dp[i][0][i] = 1;
for( int i = 0; i <= len2; i++ )
dp[0][i][i] = 1;
for( int k = 1; k <= res; k++ )
for( int i = 1; i <= len1; i++ )
for( int j = 1; j <= len2; j++ )
{
if( str1[i] == str2[j] )
dp[i][j][k] = dp[i-1][j-1][k-1];
else
dp[i][j][k] = dp[i-1][j][k-1] + dp[i][j-1][k-1];
}
printf("Case %d: %d %lld\n",cas++,res,dp[len1][len2][res]);
}
return 0;
}