標籤:
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 |
Output for Sample Input |
|
3
USA
USSR
LAILI
MAJNU
SHAHJAHAN
MOMTAJ |
Case 1: 5 3
Case 2: 9 40
Case 3: 13 15 |
做了這個題、只剩下淚了。
最開始記憶化搜尋,不造哪裡錯了Wa。
然後DP、然後把l2寫成了12,恰好範例又過了,Wa,各種測試,還以為編譯器出問題了
然後改之,由於最開始把數組開大了,超記憶體,然後改小,然後不造怎麼的,數組一變小就輸出一串莫名其妙的數字,Ac後才發現好像是中途數組下標為-1越界的原因。
然後改之,由於初始化看錯了,過了討論裡所有資料,Wa在第一組,輸出6 2。
最後改之,Ac。
受不了,- -
我是二貨!
#include<iostream>#include<cstdio>#include<cstring>using namespace std;#define INF 0x7fffffff#define ll long longll l1,l2;char s1[33];char s2[33];ll dp[33][33][66]; //dp[i][j][k]表示當長度為k時,包含第1個串中前i個字元,第2個串中前j個字元的方案數。int main(){ ll i,j,k,T,iCase=1; scanf("%lld",&T); while(T--) { memset(dp,0,sizeof(dp)); scanf("%s%s",s1+1,s2+1); l1=strlen(s1+1); l2=strlen(s2+1); for(i=0;i<=l1;i++) dp[i][0][i]=1; for(j=0;j<=l2;j++) dp[0][j][j]=1; //注意初始化細節 for(k=1;k<=l1+l2;k++) { for(i=1;i<=l1;i++) { for(j=1;j<=l2;j++) { if(s1[i]==s2[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]; } } } } for(k=1;k<=l1+l2;k++) { if(dp[l1][l2][k]) { break; } } printf("Case %lld: %lld %lld\n",iCase++,k,dp[l1][l2][k]); } return 0;}
DP [light oj 1013] Love Calculator