[light oj 1013] Love Calculator
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 long ll 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; }