Hackerrank11 LCS Returns 枚举+LCS
Given two strings, a and , b find and print the total number of ways to insert a character at any position in string a such that the length of the Longest Common Subsequence of characters in the two strings increases by one.
Input Format
The first line contains a single string denoting a.
The second line contains a single string denoting b.
Constraints
Scoring
- Strings a and b are alphanumeric (i.e., consisting of arabic digits and/or upper and lower case English letters).
- The new character being inserted must also be alphanumeric (i.e., a digit or upper/lower case English letter).
1<=|a|<=5000 1<=|b|<=5000
Output Format
Print a single integer denoting the total number of ways to insert a character into string in such a way that the length of the longest common subsequence of and increases by one.
Sample Input
aa
baaa
Sample Output
4
题意:给出两个串a b,由字母或数字组成, 然后在a串任意一个位置插入一个字母或数字, 使得ab串的LCS增加1 求方案数
思路:枚举a串的n+1个插入位置i,枚举每种插入的字符c,对于每个c,在b中找到对应的位置j, 如果LCS[i-1][j-1] + LCS2[i+1][j+1] == K(ab串原来的lcs) 那么就可以通过在i位置插入字符c来满足条件, 其中Lcs【i】【j】表示a串的1-i与b串的1-j之间的lcs Lcs2【i】【j】表示a串的i-la与b串的j-lb之间的lcs lcs[i][j]求法就是普通的做法,lcs2[i][j]逆过来再求一遍就好了
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <string>
#include <stack>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <set>
using namespace std;
const int INF = 0x3f3f3f3f;
typedef long long ll;
const int N = ; char a[N], b[N], ra[N], rb[N];
int dp1[N][N], dp2[N][N];
int la, lb, K;
void init1() {
memset(dp1, , sizeof dp1);
for(int i = ; i <= la; ++i)
for(int j = ; j <= lb; ++j) {
if(a[i - ] == b[j - ]) dp1[i][j] = dp1[i - ][j - ] + ;
else dp1[i][j] = max(dp1[i - ][j], dp1[i][j - ]);
}
K = dp1[la][lb];
}
void init2() {
memset(dp2, , sizeof dp2);
int l = ;
for(int i = la - ; i >= ; --i) ra[l++] = a[i];
l = ;
for(int i = lb - ; i >= ; --i) rb[l++] = b[i];
for(int i = ; i <= la; ++i)
for(int j = ; j <= lb; ++j) {
if(ra[i - ] == rb[j - ]) dp2[i][j] = dp2[i - ][j - ] + ;
else dp2[i][j] = max(dp2[i - ][j], dp2[i][j - ]);
}
}
vector<int> p[];
vector<char> alp;
void solve() {
for(int i = ; i < ; ++i) p[i].clear();
for(int i = ; i < lb; ++i) p[ b[i] ].push_back(i + );
alp.clear();
for(char i = 'a'; i <= 'z'; ++i) alp.push_back(i);
for(char i = 'A'; i <= 'Z'; ++i) alp.push_back(i);
for(char i = ''; i <= ''; ++i) alp.push_back(i);
int ans = ;
for(int i = ; i <= la + ; ++i) {
for(int j = ; j < alp.size(); ++j) {
char c = alp[j];
for(int k = ; k < p[c].size(); ++k) {
int r1 = i - ;
int r2 = la - i + ;
int r3 = p[c][k] - ;
int r4 = lb - p[c][k];
if(dp1[r1][r3] + dp2[r2][r4] == K) { ans++; break; }
}
}
}
printf("%d\n", ans);
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
#endif
while(~scanf("%s%s", a, b)) {
la = strlen(a);
lb = strlen(b);
init1();
init2();
solve();
}
return ;
}
Hackerrank11 LCS Returns 枚举+LCS的更多相关文章
- 【二进制枚举+LCS】Card Hand Sorting
[二进制枚举+LCS]Card Hand Sorting 题目描述 When dealt cards in the card game Plump it is a good idea to start ...
- 51Nod 1006:最长公共子序列Lcs(打印LCS)
1006 最长公共子序列Lcs 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的). ...
- HDU 1159 Common Subsequence:LCS(最长公共子序列)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 题意: 求最长公共子序列. 题解: (LCS模板题) 表示状态: dp[i][j] = max ...
- 最大匹配字符串LCS,The Longest Common Substring
public enum BackTracking { UP, LEFT, NEITHER, UP_AND_LEFT } public abstract class LCSBaseMatch { /// ...
- (字符串)最长公共子序列(Longest-Common-Subsequence,LCS)
问题: 最长公共子序列就是寻找两个给定序列的子序列,该子序列在两个序列中以相同的顺序出现,但是不必要是连续的. 例如序列X=ABCBDAB,Y=BDCABA.序列BCA是X和Y的一个公共子序列,但是不 ...
- 我的第一篇博客----LCS学习笔记
LCS引论 在这篇博文中,博主要给大家讲一个算法----最长公共子序列(LCS)算法.我最初接触这个算法是在高中学信息学竞赛的时候.那时候花了好长时间理解这个算法.老师经常说,这种算法是母算法,即从这 ...
- LightOJ1157 LCS Revisited(DP)
题目求两个字符串s1,s2不同的LCS个数. 经典的求LCS的DP是这样的: LCS[i][j]表示s1[0...i]和s2[0...j]的LCS LCS[i][j]从LCS[i-1][j-1]+1( ...
- 整理的一些模版LCS(连续和非连续)
对于连续的最大串,我们称之为子串....非连续的称之为公共序列.. 代码: 非连续连续 int LCS(char a[],char b[],char sav[]){ int lena=strlen(a ...
- 最长公共子序列(LCS问题)
先简单介绍下什么是最长公共子序列问题,其实问题很直白,假设两个序列X,Y,X的值是ACBDDCB,Y的值是BBDC,那么XY的最长公共子序列就是BDC.这里解决的问题就是需要一种算法可以快速的计算出这 ...
随机推荐
- WCF 服务编程 - 常用绑定
WCF 定义了5中常用的绑定. 一. 绑定 1.基本绑定: 对应于BasicHttpBinding类.基本绑定能够将WCF服务公开为传统的ASMX Web服务,使得原客户端能够与新的服务协作.如果客 ...
- Bestcoder#5 1002
Bestcoder#5 1002 Poor MitsuiTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- java从基础知识(十)java多线程(下)
首先介绍可见性.原子性.有序性.重排序这几个概念 原子性:即一个操作或多个操作要么全部执行并且执行的过程不会被任何因素打断,要么都不执行. 可见性:一个线程对共享变量值的修改,能够及时地被其它线程看到 ...
- 51Nod 算法马拉松21(迎新年)
这次打算法马拉松是在星期五的晚上,发挥还算正常(废话,剩下的题都不会= =). 讲讲比赛经过吧. 8:00准时发题,拿到之后第一时间开始读. A配对,看上去像是二分图最大权匹配,一看范围吓傻了,先跳过 ...
- PHP window下安装Spl_Types模块
1. Window下,Spl_Types的模块的下载地址:http://pecl.php.net/package/SPL_Types/0.4.0/windows 2. php的可执行文件已经加到系统的 ...
- Nodejs 饭店
一个Node.js饭店的发展历程 前面的一堆理论似乎不太好明白,最后讲一个关于饭店发展历程的故事作为结尾吧. 第一年 饭店开张,只有一个厨师(同时还兼任老板.服务员.打荷.收银员),当一个客 ...
- Docker - 创建Swarm
1. 准备 我们需要: Docker Engine 1.12 or later installed the IP address of the manager machine open ports b ...
- 面向过程(POP)、面向对象(OOP)、面向接口(IOP)、面向切面(AOP)
面向过程:典型的是C/C++的结构体,结构体里只有变量,没有处理变量的方法,需要专门编写处理变量的方法. 面向对象:ArrayList<Integer> list=new ArrayLis ...
- JDBC连接数据库
JDBC连接数据库 1.加载JDBC驱动程序. Class.forName("com.mysql.jdbc.Driver"); 建立连接,. Connection conn = D ...
- vs使用
1.控制dll是否生成到本地,如图,右击dll,选择属性,设置复制到本地为true即可