Yes, you are developing a 'Love calculator'. The software would be quite complex such that nobody could crack the exact behavior of the software.api
So, given two names your software will generate the percentage of their 'love' according to their names. The software requires the following things:less
Now your task is to find these parts.ui
Input starts with an integer T (≤ 125), denoting the number of test cases.orm
Each of the test cases consists of two lines each containing a name. The names will contain no more than 30 capital letters.blog
OutputFor 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.ci
You can assume that the number of unique strings will always be less than 263. Look at the sample output for the exact format.string
Sample Input3it
USAio
USSRform
LAILI
MAJNU
SHAHJAHAN
MOMTAJ
Sample OutputCase 1: 5 3
Case 2: 9 40
Case 3: 13 15
題意 : 兩個小問,第一問是求一個最短的長度構成的序列同時包含這兩個序列,第二問是求構成這個最短序列的方案數
思路分析 : 第一問就是用兩個串的長度減去 lcs
第二問定義 dp[i][j][k] 表示第一個串用了 i 個字符, 第二個串用了j 個字符,而且當前匹配用去了 k 個字符的方案數
轉移過程相似求 lcs 的過程
代碼示例:
#define ll long long char a[100], b[100]; ll dp[100][100][100]; ll dp2[100][100]; ll lena, lenb, num; void solve(){ lena = strlen(a+1); lenb = strlen(b+1); memset(dp2, 0, sizeof(dp2)); for(ll i = 1; i <= lena; i++){ for(ll j = 1; j <= lenb; j++){ if (a[i] == b[j]) dp2[i][j] = dp2[i-1][j-1]+1; else dp2[i][j] = max(dp2[i-1][j], dp2[i][j-1]); } } memset(dp, 0, sizeof(dp)); dp[0][0][1] = 1; num = dp2[lena][lenb]; for(ll i = 1; i <= lena; i++) dp[i][0][1] = 1; for(ll i = 1; i <= lenb; i++) dp[0][i][1] = 1; for(ll i = 1; i <= lena; i++){ for(ll j = 1; j <= lenb; j++){ for(ll k = 1; k <= num+1; k++){ if (a[i] == b[j]) dp[i][j][k] = dp[i-1][j-1][k-1]; else dp[i][j][k] = dp[i-1][j][k]+dp[i][j-1][k]; } } } } int main() { //freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); ll t; ll cas = 1; cin >> t; while(t--){ scanf("%s%s", a+1, b+1); solve(); printf("Case %lld: %lld %lld\n", cas++, lena+lenb-num, dp[lena][lenb][num+1]); } return 0; }