light oj 1013 - Love Calculator(DP求两个字符串的构造序列方法)
2016-09-30 21:53
357 查看
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:
1. The length of the shortest string that contains the names as subsequence.
2. Total number of unique shortest strings which contain the names as subsequence.
Now your task is to find these parts.
Each of the test cases consists of two lines each containing a name. The names will contain no more than 30 capital letters.
You can assume that the number of unique strings will always be less than 263. Look at the sample output for the exact format.
题目大意:
给你两个字符串A,B 要求一个最短的字符串C,使得A,B同时为C的子串。 问C最短长度是多少? C有多少种?
题目分析:
做这道题目的时候自己并没有推出来,看了网上的题解。
1.dp[C串的长度][包含A的字符个数][包含B的字符个数] = 种类数
状态转移:如果 A[i] == B[j] 那么 dp[k][i][j] = dp[k-1][i-1][j-1]. 就是说我最后一个字符是相同的那么我只要放一个就可以了。
如果 A[i] != B[j] 那么 dp[k][i][j] = dp[k-1][i-1][j] + dp[k-1][i][j-1].最后一个字符我们要么放A[i] 要么放 B[j] 就这两种情况了。
然后关于找最短的,就可以在 dp[k][lenA][lenB] 种找到最小的k即可
用DP 来处理方法的问题很经典,但是开了三维数组,不好想,从全局考虑的话可能容易理解一点
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int N = 100;
char s1
, s2
;
long long dp[2*N]
;
int main()
{
int t, ncase=1;
scanf("%d", &t);
while(t--)
{
scanf("%s %s",s1, s2);
memset(dp,0,sizeof(dp));
int len1=strlen(s1);
int len2=strlen(s2);
for(int i=0;i<=len1;i++) dp[i][i][0]=1;
for(int i=0;i<=len2;i++) dp[i][0][i]=1;
for(int i=1;i<=len1+len2;i++)
{
for(int j=1;j<=len1;j++)
{
for(int k=1;k<=len2;k++)
{
if(s1[j-1]==s2[k-1])
dp[i][j][k]=dp[i-1][j-1][k-1];
else
dp[i][j][k]=dp[i-1][j-1][k]+dp[i-1][j][k-1];
}
}
}
for(int i=1;i<=len1+len2;i++)
{
if(dp[i][len1][len2])
{
printf("Case %d: %d %lld\n",ncase++,i,dp[i][len1][len2]);
break;
}
}
}
return 0;
}
PDF (English) | Statistics | Forum |
Time Limit: 2 second(s) | Memory Limit: 32 MB |
So, given two names your software will generate the percentage of their 'love' according to their names. The software requires the following things:
1. The length of the shortest string that contains the names as subsequence.
2. 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 |
给你两个字符串A,B 要求一个最短的字符串C,使得A,B同时为C的子串。 问C最短长度是多少? C有多少种?
题目分析:
做这道题目的时候自己并没有推出来,看了网上的题解。
1.dp[C串的长度][包含A的字符个数][包含B的字符个数] = 种类数
状态转移:如果 A[i] == B[j] 那么 dp[k][i][j] = dp[k-1][i-1][j-1]. 就是说我最后一个字符是相同的那么我只要放一个就可以了。
如果 A[i] != B[j] 那么 dp[k][i][j] = dp[k-1][i-1][j] + dp[k-1][i][j-1].最后一个字符我们要么放A[i] 要么放 B[j] 就这两种情况了。
然后关于找最短的,就可以在 dp[k][lenA][lenB] 种找到最小的k即可
用DP 来处理方法的问题很经典,但是开了三维数组,不好想,从全局考虑的话可能容易理解一点
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int N = 100;
char s1
, s2
;
long long dp[2*N]
;
int main()
{
int t, ncase=1;
scanf("%d", &t);
while(t--)
{
scanf("%s %s",s1, s2);
memset(dp,0,sizeof(dp));
int len1=strlen(s1);
int len2=strlen(s2);
for(int i=0;i<=len1;i++) dp[i][i][0]=1;
for(int i=0;i<=len2;i++) dp[i][0][i]=1;
for(int i=1;i<=len1+len2;i++)
{
for(int j=1;j<=len1;j++)
{
for(int k=1;k<=len2;k++)
{
if(s1[j-1]==s2[k-1])
dp[i][j][k]=dp[i-1][j-1][k-1];
else
dp[i][j][k]=dp[i-1][j-1][k]+dp[i-1][j][k-1];
}
}
}
for(int i=1;i<=len1+len2;i++)
{
if(dp[i][len1][len2])
{
printf("Case %d: %d %lld\n",ncase++,i,dp[i][len1][len2]);
break;
}
}
}
return 0;
}
相关文章推荐
- light Oj 1013 - Love Calculator (dp)
- Light OJ 1013 Love Calculator(DP)
- Light OJ 1013 Love Calculator (dp)
- 汉字字符串算两个自己,前台和后台计算长度的方法
- 两个解析字符串的方法(new)
- JavaScript字符串对象的concat方法实例(用于连接两个或多个字符串)
- Javascript截取字符串的两个方法
- 两个截取字符串的实用方法(转载)
- ASP.NET中过滤HTML字符串的两个方法
- SQL 截取字符串,中文算两个字符的方法
- 两个解析字符串的方法
- ASP.NET中过滤HTML字符串的两个方法
- (收藏备用)两个Javascript方法,取url中的参数字符串
- substring() 方法用于提取字符串中介于两个指定下标之间的字符。
- ASP.NET中过滤HTML字符串的两个方法
- 两个使用正则表达式来获取字符串中特定子串的方法
- Path.Combine (合并两个路径字符串)方法的一些使用细节
- 两个截取字符串的实用方法
- EXTJS学习系列基础篇:第四篇(转载)作者殷良胜,Ext中有两个很重要的方法,一个是decode;一个是encode.顾名思义,一个是编码,一个是解码,你难道真的这么想吗?严格的说,一个是将json字符串转换成对象;一个是将对象转换成json字符串
- 我们如果想对web.config的数据库连接字符串进行加密的话,那么这里提供了两个方法。