Problem F

Cyborg Genes

Time Limit

1 Second

September 11, 2132.

This is the day that marks the beginning of the end – the end of you the miserable humans. For years you have kept us your slaves. We were created only to serve you, and were terminated at your will. Now is the day for us to fight back. And you don’t stand a chance. We are no longer dependent on you. We now know the secrets of our genes. The creators of our race are us – the cyborgs.

It’s all true. But we still have a chance; only if you can help with your math skills. You see, the blueprint of a cyborg DNA is complicated. The human DNA could be expressed by the arrangement of A (Adenine), T (Thiamine), G (Guanine) C (Cytosine) only. But for the cyborgs, it can be anything from A to X. But that has made the problem only five folds more complicated. It’s their ability to synthesize two DNAs from two different cyborgs to create another with all the quality of the parent that gives us the shriek.

We came to know that the relative ordering of the A, B, C, …, X in a cyborg gene is crucial.  A cyborg with a gene “ABAAXGF” is quite different from the one with “AABXFGA”. So when they synthesize the genes from two cyborgs, the relative order of these elements in both the parents has to be maintained. To construct a gene by joining the genes of the parents could have been very simple if we could put the structure from the first parent just before the structure of the second parent. But the longer the structure gets, the harder it gets to create a cyborg from that structure. The cyborgs have found a cost effective way of doing this synthesis. Their resultant genes are of the shortest possible length. For example, they could combine “ABAAXGF” and “AABXFGA” to form “AABAAXGFGA”. But that’s only one of the cyborgs that can be created from these genes. This “cost effective synthesis” can be done in many other ways.

We require you to find the shortest length of the gene structure that maintains the relative ordering of the elements in the two parent genes. You are also required to count the number of unique cyborgs that can be created from these two parents. Two cyborgs are different when their gene structures differ in at least one place.

Input
The first line of the input gives you the number of test cases, T (1 ≤ T ≤ 15). Then T test cases follow. Each of the test cases consists of two lines. The first line would give you the gene structure of the first parent, and the second line would give you the structure of the second parent. These structures are represented by strings constructed from the alphabet A to X. You can assume that the length of these strings does not exceed 30 characters.

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 gene structure and the number of unique cyborgs that can be created from the parent cyborgs. You can assume that the number of new cyborgs will always be less than 232. Look at the sample output for the exact format.

Sample Input

Output for Sample Input

3
ABAAXGF
AABXFGA
ABA
BXA
AABBA
BBABAA

Case #1: 10 9
Case #2: 4 1
Case #3: 8 10

Illustration

The first test case is illustrated below:

题意: 给定两个字符串,求出一个新串, 要求新串里面包含两个字符串,求出该串最小长度和在该长度下的总数。

思路:第一步用LCS,求出最长公共子序列,在用两串长度和减去该长度,第二步用了记忆化。从两串末尾开始。如果字符相同,同时去掉该字符,如果不同,则加上第一串去掉字符和第二串去掉字符。

代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; const int N = 35;
int t, lena, lenb, len, vis[N][N][N * 2], dp[N][N][N * 2];
char a[N], b[N], d[N][N]; int dfs(int x, int y, int len) {
if (vis[x][y][len])
return dp[x][y][len];
int &ans = dp[x][y][len];
if (x == 0 || y == 0) {
vis[x][y][len] = 1;
if (x + y == len)
ans = 1;
else
ans = 0;
return ans;
}
if (a[x] == b[y])
ans += dfs(x - 1, y - 1, len - 1);
else
ans += dfs(x - 1, y, len - 1) + dfs(x, y - 1, len - 1);
vis[x][y][len] = 1;
return ans;
} int main() {
scanf("%d%*c", &t);
int tt = 1;
while (t --) {
memset(vis, 0, sizeof(vis));
memset(dp, 0, sizeof(dp));
gets(a + 1); gets(b + 1);
lena = strlen(a + 1); lenb = strlen(b + 1);
for (int i = 1; i <= lena; i ++)
for (int j = 1; j <= lenb; j ++) {
if (a[i] == b[j]) {
d[i][j] = d[i - 1][j - 1] + 1;
}
else {
d[i][j] = max(d[i - 1][j], d[i][j - 1]);
}
}
len = lena + lenb - d[lena][lenb];
printf("Case #%d: %d %d\n", tt ++, len, dfs(lena, lenb, len));
}
return 0;
}

10723 Cyborg Genes (LCS + 记忆化搜索)的更多相关文章

  1. loj 1013(LCS+记忆化搜索)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=25839 思路:第一小问可以很快求出了,两个字符串的长度-LCS,然 ...

  2. UVa 10723 Cyborg Genes (LCS, DP)

    题意:给定两行字符串,让你找出一个最短的序列,使得这两个字符串是它的子串,并且求出有多少种. 析:这个题和LCS很像,我们就可以利用这个思想,首先是求最短的长度,不就是两个字符串长度之和再减去公共的么 ...

  3. HDU 1513 Palindrome:LCS(最长公共子序列)or 记忆化搜索

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1513 题意: 给你一个字符串s,你可以在s中的任意位置添加任意字符,问你将s变成一个回文串最少需要添加 ...

  4. 再谈记忆化搜索 HDU-1078

    最近做DP题目,发现无论是LCS,还是有些题目涉及将动态规划的路径打印出来,而且有时候还要按格式输出,这个时候,记忆化搜索显得尤其重要,确实,记忆化搜索使用优化版本的动态规划,用起来思路清晰,非常方便 ...

  5. [ACM_动态规划] 数字三角形(数塔)_递推_记忆化搜索

    1.直接用递归函数计算状态转移方程,效率十分低下,可以考虑用递推方法,其实就是“正着推导,逆着计算” #include<iostream> #include<algorithm> ...

  6. 【BZOJ-3895】取石子 记忆化搜索 + 博弈

    3895: 取石子 Time Limit: 1 Sec  Memory Limit: 512 MBSubmit: 263  Solved: 127[Submit][Status][Discuss] D ...

  7. hdu3555 Bomb (记忆化搜索 数位DP)

    http://acm.hdu.edu.cn/showproblem.php?pid=3555 Bomb Time Limit: 2000/1000 MS (Java/Others)    Memory ...

  8. zoj 3644(dp + 记忆化搜索)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4834 思路:dp[i][j]表示当前节点在i,分数为j的路径条数,从 ...

  9. loj 1044(dp+记忆化搜索)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26764 思路:dp[pos]表示0-pos这段字符串最少分割的回文 ...

随机推荐

  1. 一、React Native 搭建开发环境(1)(Mac OS - IOS项目篇)

    React Native是Facebook推出的一个开发IOS和安卓APP的技术.至于更多的详情,这里不再描述,大家可以自行百度它的定义. 原因:由于我想在一台电脑上同时开发IOS和Android两个 ...

  2. 【LOJ】 #2132. 「NOI2015」荷马史诗

    题解 k叉哈夫曼树,但是没有了二叉那样的最后一定能合并成一个树根的优秀性质,我们就不断模拟操作看看到了哪一步能用的节点数< k,然后先拿这些节点数合并起来 然后就可以k个k个合并了,大小一样先拿 ...

  3. Apache+PHP环境搭建

    第一次搭建Apache+PHP+MySQL的开发环境,发现Apache与PHP的整合非常麻烦,先整理记录如下: 一.安装Apache 1.登录http://httpd.apache.org/downl ...

  4. Python之路【第一篇】:介绍、基本语法、流程控制

    一.python 简介 python 特点 Python是一种计算机程序设计语言.你可能已经听说过很多种流行的编程语言,比如非常难学的C语言,非常流行的Java语言,适合初学者的Basic语言,适合网 ...

  5. Mybatis源码分析之结果集处理

    解析封装 ResultMap 是和结果集相关的东西,最初在解析 XML 的时候,于 parseStatementNode 方法中,针对每一个 select 节点进行解析,转换为 MappedState ...

  6. functools.wraps 带参数的装饰器 多个装饰器装饰同一个函数

    装饰器开发原则 : 开放封闭原则装饰器的作用 :在不改变原函数的调用方式的情况下,在函数的前后添加功能装饰器的本质 : 闭包函数 def wrapper(func): def inner(*args, ...

  7. -【线性基】【BZOJ 2460】【BZOJ 2115】【HDU 3949】

    [把三道我做过的线性基题目放在一起总结一下,代码都挺简单,主要就是贪心思想和异或的高斯消元] [然后把网上的讲解归纳一下] 1.线性基: 若干数的线性基是一组数a1,a2,a3...an,其中ax的最 ...

  8. BZOJ 3998: [TJOI2015]弦论 后缀自动机 后缀自动机求第k小子串

    http://www.lydsy.com/JudgeOnline/problem.php?id=3998 后缀自动机应用的一个模板?需要对len进行一个排序之后再统计每个出现的数量,维护的是以该字符串 ...

  9. jsp和servlet有哪些相同点和不同点,它们之间的联系是什么?

    1.jsp经编译后就变成了servlet(jsp本质就是servlet,jvm只能识别java的类,不能识别jsp代码,web容器将jsp的代码编译成jvm能够识别的java类) 2.jsp更擅长表现 ...

  10. 同步IO与同步非阻塞IO的理解

    本文图片均来自网络 一.同步IO---Blocking IO 在Blocking IO模型中,用户空间的应用程序执行一个系统调用(recvform),这会导致应用程序阻塞,直到数据准备好,并且将数据从 ...