思路:dp(i, j)表示第一个串前i个字符和第二个串前j个字符需要的最短字符串长度,cnt(i, j)表示第一个串前i个字符和第二个串前j个字符需要的最短字符串的个数。

转移方程:

if(s1[i] == s2[j]) dp[i][j] = dp[i-1][j-1] + 1;
else dp[i][j] = 1 + min(dp[i-1][j], dp[i][j-1]);
if(s1[i] == s2[j]) cnt[i][j] = cnt[i-1][j-1]; //字符成功匹配
else {
	if(dp[i-1][j] == dp[i][j-1])
	        cnt[i][j] = cnt[i-1][j] + cnt[i][j-1];
	else if(dp[i-1][j] < dp[i][j-1])
		cnt[i][j] = cnt[i-1][j];
	else
		cnt[i][j] = cnt[i][j-1];
}

AC代码

#include <cstdio>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <cstring>
#include <utility>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define eps 1e-10
#define inf 0x3f3f3f3f
#define PI pair<int, int>
typedef long long LL;
const int maxn = 30 + 5;
char s1[maxn], s2[maxn];
int dp[maxn][maxn], cnt[maxn][maxn];
int main() {
	int T, kase = 1;
	scanf("%d", &T);
	getchar();
	while(T--) {
		fgets(s1+1, sizeof(s1), stdin);
		fgets(s2+1, sizeof(s2), stdin);
		//scanf("%s%s", s1+1, s2+1);
		int n = strlen(s1+1) - 1, m = strlen(s2+1) - 1;
		for(int i = 0; i < maxn; ++i) {
			dp[0][i] = dp[i][0] = i;
			cnt[0][i] = cnt[i][0] = 1;
		}
		for(int i = 1; i <= n; ++i)
			for(int j = 1; j <= m; ++j) {
				if(s1[i] == s2[j]) dp[i][j] = dp[i-1][j-1] + 1;
				else dp[i][j] = 1 + min(dp[i-1][j], dp[i][j-1]);
			}
		for(int i = 1; i <= n; ++i)
			for(int j = 1; j <= m; ++j) {
				if(s1[i] == s2[j]) cnt[i][j] = cnt[i-1][j-1];
				else {
					if(dp[i-1][j] == dp[i][j-1])
						cnt[i][j] = cnt[i-1][j] + cnt[i][j-1];
					else if(dp[i-1][j] < dp[i][j-1])
						cnt[i][j] = cnt[i-1][j];
					else
						cnt[i][j] = cnt[i][j-1];
				}
		}
		printf("Case #%d: %d %d\n", kase++, dp[n][m], cnt[n][m]);
	}
	return 0;
}

如有不当之处欢迎指出!

UVA - 10723 类似LCS的更多相关文章

  1. UVa 10723 LCS变形 Cyborg Genes

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

  2. uva 10723 Cyborg Genes(LCS变形)

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

  3. UVa 10723 电子人的基因(LCS)

    https://vjudge.net/problem/UVA-10723 题意: 输入两个A~Z组成的字符串,找一个最短的串,使得输入的两个串均是它的子序列,另外还需要统计长度最短的串的个数. 思路: ...

  4. UVa 10723 Cyborg Genes (LCS, DP)

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

  5. UVA - 10723 Cyborg Genes (LCS)

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

  6. uva 10723

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

  7. UVA 111(LCS问题)

     History Grading  Background Many problems in Computer Science involve maximizing some measure accor ...

  8. UVA - 10635 LIS LCS转换

    白书例题,元素互不相同通过哈希转换为LIS求LCS #include<iostream> #include<algorithm> #include<cstdio> ...

  9. uva 10453 dp/LCS变形

    https://vjudge.net/problem/UVA-10453 给出一个字符串,问最少添加几个字符使其变为回文串,并输出任意一种答案.就是一个类似于LCS的题目,而且简化了一下,只会出现三种 ...

随机推荐

  1. python_百文买百鸡问题

    百文买百鸡问题 -- 不定方程 -- 公鸡5文钱一只,母鸡3文钱一只,小鸡3只一文钱,用100文钱买100只鸡,如何买? -- 列出方程式 x + y + z = 100 5x + 3y + z/3 ...

  2. SqlSession 同步为注册,因为同步未激活

    Creating a new SqlSessionSqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7d620db9] ...

  3. kindeditor使用

    下载地址http://kindeditor.net/down.php @官方文档 使用步骤: 引入js <script charset="utf-8" src="r ...

  4. shell第二篇

    第二篇知道shell是什么,再来了解一下shell的分类及相关历史 参考百度百科:shell 1.shell概念(计算机壳层) 在计算机科学中,Shell俗称壳(用来区别于核),是指"提供使 ...

  5. 数据库备份shell脚本

    法一: #!/bin/bash [ ! -d /server/backup ] && mkdir /server/backup mysqldump -u root -A -B > ...

  6. Notice!

    之后的小车内容在这里更新,开源社区,新浪博客不再更新.

  7. 用swing做一个简单的正则验证工具

    直接上代码吧,因为我对swing也不熟悉,照着API一点点拼出来的. import java.awt.event.ActionEvent; import java.awt.event.ActionLi ...

  8. JAVA并发编程学习笔记------锁顺序死锁

    一.需求描述: 将资金从一个账户转移到另一个账户. 二.程序实现: (1)账户类: public class Account { private long account; public Accoun ...

  9. chromedriver禁用图片,禁用js,切换UA

    selenium 模拟chrome浏览器,此时就是一个真实的浏览器,一个浏览器该加载的该渲染的它都加载都渲染,所以爬取网页的速度很慢.如果可以不加载图片等操作,网页加载速度就会快不少,代码中列出了了禁 ...

  10. 浅谈python模块的导入操作

    1.什么是模块 在Python中有一个概念叫做模块(module). 所谓模块,就是将代码量较大的程序分割成多个有组织的,彼此独立但双能互相交互的代码片段, 这些自我包含的有组织的代码段就是模块. 2 ...