题意:给定两行字符串,让你找出一个最短的序列,使得这两个字符串是它的子串,并且求出有多少种。

析:这个题和LCS很像,我们就可以利用这个思想,首先是求最短的长度,不就是两个字符串长度之和再减去公共的么。那么有多少种呢?

同样也是分两种情况讨论,如果s1[i-1] == s2[j-1] 那么种类数肯定和 ans[i-1][j-1]一样了,没有变化,再就是如果不相等怎么算呢?

难道也是ans[i][j] = Max(ans[i-1][j], ans[i][j-1])吗,其实并不是,如果两种方法数相等呢?也就是说从ans[i-1][j]能得到答案,也能从ans[i][j-1]得到答案,

所以要加起来。再就是要注意的是,字符串可能为空串,用gets输入。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 30 + 5;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
char s1[maxn], s2[maxn];
int dp[maxn][maxn];
LL ans[maxn][maxn]; int main(){
int T; cin >> T;
getchar();
for(int kase = 1; kase <= T; ++kase){
gets(s1+1);
gets(s2+1);
int len1 = strlen(s1+1);
int len2 = strlen(s2+1);
for(int i = 0; i <= len1; ++i) ans[i][0] = 1;
for(int i = 0; i <= len2; ++i) ans[0][i] = 1; for(int i = 1; i <= len1; ++i){
for(int j = 1; j <= len2; ++j){
if(s1[i] == s2[j]){
dp[i][j] = dp[i-1][j-1] + 1;
ans[i][j] = ans[i-1][j-1];
}
else if(dp[i-1][j] > dp[i][j-1]){
dp[i][j] = dp[i-1][j];
ans[i][j] = ans[i-1][j];
}
else if(dp[i-1][j] < dp[i][j-1]){
dp[i][j] = dp[i][j-1];
ans[i][j] = ans[i][j-1];
}
else{
dp[i][j] = dp[i-1][j];
ans[i][j] = ans[i][j-1] + ans[i-1][j];
}
}
}
printf("Case #%d: %d %lld\n", kase, len1+len2-dp[len1][len2], ans[len1][len2]);
}
return 0;
}

UVa 10723 Cyborg Genes (LCS, DP)的更多相关文章

  1. 10723 Cyborg Genes (LCS + 记忆化搜索)

    Problem F Cyborg Genes Time Limit 1 Second September 11, 2132. This is the day that marks the beginn ...

  2. uva 10723 Cyborg Genes(LCS变形)

    题目:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=107450#problem/C 题意:输入两个字符串,找一个最短的串,使得输入的两个 ...

  3. UVA - 10723 Cyborg Genes (LCS)

    题目: 思路: 求两个串的最长公共子序列,则这个最短的串就是给出的两个串的长度和减去最长公共子序列的长度. 状态转移方程: 如果s[i-1]==t[j-1]就有dp[i][j] = dp[i-1][j ...

  4. UVA-10273 Cyborg Genes (DP)

    题目大意:给两个字符串a.b,找出一个最短的字符串c,使得这两个字符串都是c的子序列.只需找出p的最小长度和最小长度时的个数. 题目分析:与LCS问题类似.最小长度的状态转移方程,dp(i,j)=mi ...

  5. UVa 10723 LCS变形 Cyborg Genes

    题解转自: UVA 10723 Cyborg Genes - Staginner - 博客园 首先这个题目肯定是按最长公共子序列的形式进行dp的,因为只有保证消去的一部分是最长公共子序列才能保证最后生 ...

  6. uva 10723

      10723 - Cyborg Genes Time limit: 3.000 seconds Problem F Cyborg Genes Time Limit 1 Second Septembe ...

  7. UVA.10066 The Twin Towers (DP LCS)

    UVA.10066 The Twin Towers (DP LCS) 题意分析 有2座塔,分别由不同长度的石块组成.现在要求移走一些石块,使得这2座塔的高度相同,求高度最大是多少. 问题的实质可以转化 ...

  8. UVa10723 - Cyborg Genes

    这题我能想到的解决方法是: 最优解的长度好找,两串的长度和-LCS: 根据anslen,枚举出解的数目...但想不出简单有效的枚举方法,这种做法可能超时 网上看大神的博客后,发现大家都用的此方法: 最 ...

  9. UVA 10003 Cutting Sticks 区间DP+记忆化搜索

    UVA 10003 Cutting Sticks+区间DP 纵有疾风起 题目大意 有一个长为L的木棍,木棍中间有n个切点.每次切割的费用为当前木棍的长度.求切割木棍的最小费用 输入输出 第一行是木棍的 ...

随机推荐

  1. UVA 10042 Smith Numbers(数论)

    Smith Numbers Background While skimming his phone directory in 1982, Albert Wilansky, a mathematicia ...

  2. POJ 3335 Rotating Scoreboard(半平面交 多边形是否有核 模板)

    题目链接:http://poj.org/problem? id=3335 Description This year, ACM/ICPC World finals will be held in a ...

  3. cvpr2017年的所有论文下载

    wget -c -N  --no-clobber --convert-links --random-wait -r -p -E -e robots=off -U mozilla   http://op ...

  4. html中设置透明遮罩层的兼容性代码

    说明:下面遮罩层的height视实际情况自行修改,要求显示的div层的样式需加上position:relative,位于遮罩层层div的下面一行.<div id="ceng" ...

  5. (转)c#(wince)中使用多线程访问winform中控件的问题

    我们在做winform应用的时候,大部分情况下都会碰到使用多线程控制界面上控件信息的问题.然而我们并不能用传统方法来做这个问题,下面我将详细的介绍. 首先来看传统方法:  public partial ...

  6. 将css 中的16进制颜色, 转化为 rgb格式

    对dojo/_base/Color模块的注解. 源地址 https://github.com/robinxiong/dojo/blob/master/_base/Color.js function f ...

  7. VB.Command()的参数

    VB.Command()的参数的来源有三个地方 1配置文件2启动参数3框架

  8. Struts action

    <action name="KnowledgeBankManageAction_*" class="knowledgeBankManageAction" ...

  9. Windows窗口程序从创建到关闭产生的消息

    Windows是消息驱动的,理解消息机制及消息循环是特别重要.知道在什么情况下产生什么消息会让我们对程序有更好的控制.Windows给应用程序发消息,有些会加入应用程序的消息队列,也是就是队列消息.有 ...

  10. 关于ArcGIS动态图层空间内栅格数据,JS前端显示颜色不正确的解决方案

    ArcGIS的动态空间,可承载Table,Shp,Raster等数据. 我们的需求是,每天客户有新的卫星数据,但是不同类型,有多波段Landsat卫星数据,有Modis数据等.不定期更新到共享文件夹, ...