Light OJ 1013 Love Calculator(DP)
题目大意:
给你两个字符串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即可。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<map>
using namespace std;
typedef long long LL;
#define Max(a,b) (a>b?a:b)
const int INF = 1e9+;
const int maxn = ;
const int MOD = ;
LL dp[maxn*][maxn][maxn];///dp[总长度][包含i个A串字符][包含j个B串字符]
char A[maxn], B[maxn]; int main()
{
int T, cas = ;
scanf("%d", &T);
while(T --)
{
scanf("%s %s", A, B);
int lenA = strlen(A);
int lenB = strlen(B);
memset(dp, , sizeof(dp));
for(int i=; i<=lenA; i++) dp[i][i][] = ;
for(int i=; i<=lenB; i++) dp[i][][i] = ; for(int i=; i<=lenA+lenB; i++)
for(int j=; j<=lenA; j++)
for(int k=; k<=lenB; k++)
{
if(A[j-] == B[k-])
dp[i][j][k] += dp[i-][j-][k-];
else
dp[i][j][k] += dp[i-][j-][k] + dp[i-][j][k-];
}
int k;
for(k = ; k<=lenA+lenB; k ++)
{
if(dp[k][lenA][lenB])
break;
}
printf("Case %d: %d %lld\n", cas ++, k, dp[k][lenA][lenB]);
}
return ;
}
代码在此
Light OJ 1013 Love Calculator(DP)的更多相关文章
- [light oj 1013] Love Calculator
1013 - Love Calculator Yes, you are developing a 'Love calculator'. The software would be quite comp ...
- Light oj 1013 - Love Calculator (LCS变形)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1013 题意: 给你两个字符串,让你构造出一个长度最小的字符串,且它的子序列包含 ...
- Light OJ 1031---Easy Game(区间DP)
题目链接 http://lightoj.com/volume_showproblem.php?problem=1031 Description You are playing a two player ...
- (light OJ 1005) Rooks dp
http://www.lightoj.com/volume_showproblem.php?problem=1005 PDF (English) Statistics Forum Tim ...
- light oj 1068 - Investigation 数位DP
思路:典型的数位DP!!! dp[i][j][k]:第i位,对mod取余为j,数字和对mod取余为k. 注意:由于32位数字和小于95,所以当k>=95时,结果肯定为0. 这样数组就可以开小点, ...
- Light OJ 1005 - Rooks(DP)
题目大意: 给你一个N和K要求确定有多少种放法,使得没有两个车在一条线上. N*N的矩阵, 有K个棋子. 题目分析: 我是用DP来写的,关于子结构的考虑是这样的. 假设第n*n的矩阵放k个棋子那么,这 ...
- light oj 1057 状压dp TSP
#include <iostream> #include <cstdlib> #include <cstring> #include <queue> # ...
- light oj 1037 状压dp
#include <iostream> #include <cstdlib> #include <cstring> #include <queue> # ...
- Light OJ 1406 Assassin`s Creed 减少国家DP+支撑点甚至通缩+最小路径覆盖
标题来源:problem=1406">Light OJ 1406 Assassin`s Creed 意甲冠军:向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方 思路: ...
随机推荐
- [转] EF Configuring a DbContext
本文转自:https://docs.microsoft.com/en-us/ef/core/miscellaneous/configuring-dbcontext Note This document ...
- Rouh set 入门知识3(上下近似集,正负域,边界域)
在RS中,引入两个概念:一个是下近似集,另一个是上近似集.下近似集是指当一个集合不能利用有效的等价关系被恰当的分类是时,则可通过另外的集合来达到这个集合的近似.形式上,设X⊆U是任一子集,R是U上的等 ...
- Git CMD - pull: Fetch from and integrate with another repository or a local branch
命令格式 git pull [options] [<repository> [<refspec>…]] 命令参数 -q, --quiet 安静模式. -v, --verbos ...
- java移位运算的用途
参考下面这篇文章 http://blog.csdn.net/gaowen_han/article/details/7163104 http://jinguo.iteye.com/blog/540150 ...
- WPF 媒体播放器(MediaElement)使用实例(转)
在WPF 中可以使用MediaElement 为应用程序添加媒体播放控件,以完成播放音频.视频功能.由于MediaElement 属于UIElement,所以它同时也支持鼠标及键盘的操作.本篇将使用M ...
- 【转】iOS-Core-Animation-Advanced-Techniques(四)
原文:http://www.cocoachina.com/ios/20150105/10812.html 隐式动画和显式动画 隐式动画 按照我的意思去做,而不是我说的. -- 埃德娜,辛普森 我们在第 ...
- clipboard.js IE下 不允许复制时, 问题修改
问题描述:https://github.com/zenorocha/clipboard.js/wiki/Known-IssuesOn IE9-11 there's a prompt that asks ...
- 《tr命令》-linux命令五分钟系列之六
本原创文章属于<Linux大棚>博客. 博客地址为http://roclinux.cn. 文章作者为roc 希望您能通过捐款的方式支持Linux大棚博客的运行和发展.请见“关于捐款” == ...
- html良好结构-之豆瓣风格
良好HTML结构 1 结构层次 unit> hd+ unit-wrap>section>item2 语议化的结构 html5 html 语议 ck-box-unit ck-hd-wr ...
- yii2源码学习笔记(四)
继续了解组件Component.php /** * Returns a value indicating whether a property is defined for this componen ...