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

析:这个题和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. kubernetes故障现场一之Orphaned pod

    系列目录 问题描述:周五写字楼整体停电,周一再来的时候发现很多pod的状态都是Terminating,经排查是因为测试环境kubernetes集群中的有些节点是PC机,停电后需要手动开机才能起来.起来 ...

  2. IOS 为UILabel添加长按复制功能

    IOS 为UILabel添加长按复制功能 在iOS中下面三个控件,自身就有复制-粘贴的功能: 1.UITextView 2.UITextField 3.UIWebView UIKit framewor ...

  3. java 最长回文字串

      package string.string1_6; public class LongestPalidrome { /** * 使用常规方法, 以字符串的每一个字符作为中心进行判断, 包括奇数和偶 ...

  4. MySQL-怎样使update操作sleep一段时间

    )) a on mytest.id=a.id set mytest.name='xiaowang';

  5. iOS进程间通信之CFMessagePort

    本文转载至 http://www.cocoachina.com/industry/20140606/8701.html iOS系统是出了名的封闭,每个应用的活动范围被严格地限制在各自的沙盒中.尽管如此 ...

  6. 【BZOJ2521】[Shoi2010]最小生成树 最小割

    [BZOJ2521][Shoi2010]最小生成树 Description Secsa最近对最小生成树问题特别感兴趣.他已经知道如果要去求出一个n个点.m条边的无向图的最小生成树有一个Krustal算 ...

  7. Interface AutoCloseable

    https://docs.oracle.com/javase/tutorial/essential/exceptions/try.html https://docs.oracle.com/javase ...

  8. ABAP- INCLUDE Zxxx IF FOUND.

    大顾代码: INCLUDE zinc_ca_0002 IF FOUND. - 这肯定是大顾问写出来的 - 一般都不会加东西啊 -加了 IF FOUND 不知道啥意思.  古道无仙(173120830) ...

  9. 负载均衡LVS

    可参考:http://ixdba.blog.51cto.com/2895551/555738 http://os.51cto.com/art/201202/319979.html 也可以参考官网:ht ...

  10. FusionCharts Free 甘特图

    用FusionCharts做甘特图 1.同步方式(用xml格式字符) 前台aspx代码 <!DOCTYPE html> <html xmlns="http://www.w3 ...