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.这里解决的问题就是需要一种算法可以快速的计算出这 ...
随机推荐
- codevs1403 新三国争霸
题目描述 Description PP 特别喜欢玩即时战略类游戏,但他觉得那些游戏都有美中不足的地方.灾害总不降临道路,而只降临城市,而且道路不能被占领,没有保护粮草的真实性.于是他就研发了<新 ...
- TCP学习之三:客户端、服务端同步传输字符串
参考学习张子阳大神的博客:http://www.cnblogs.com/JimmyZhang/category/101698.html 一个客户端.发送一条消息 客户端: 服务端: 注意:Networ ...
- Java Netty 4.x 用户指南
问题 今天,我们使用通用的应用程序或者类库来实现互相通讯,比如,我们经常使用一个 HTTP 客户端库来从 web 服务器上获取信息,或者通过 web 服务来执行一个远程的调用. 然而,有时候一个通用的 ...
- JS树形菜单
超全的JS树形菜单源代码共享(有实例图) 树形菜单是很常用的效果,常用在管理软件当中,但是一套树形菜单已经不能满足需求,所以如果能有一套比较全面的树形菜单JS特效代码,将会非常方便,下面懒人萱将超全的 ...
- URI编码解码
.NET string s= System.Web.HttpUtility.UrlEncode("123", System.Text.Encoding.Unicode); //编码 ...
- jemalloc在linux上从安装到使用
jemalloc在linux上从安装到使用 上次在引导大家安装Redis时提到可能会报错: 发现了redis有用到jemalloc. 首先,jemalloc是干什么的? 我们看看作者自己的介绍: j ...
- java 写文本换行
import org.apache.commons.io.FileUtils; public static void main(String[] args) throws IOException { ...
- 关于更新发布CSS和JS文件的缓存问题
现如今,浏览器大战下,各个浏览器也是拼了命的提高性能,升级JS解析引擎,更好的处理浏览器的页面缓存,让用户的浏览体验更快,占用更小的PC资源.那么,问题就出现在JS和CSS缓存,甚至页面缓存上.至于浏 ...
- 关于so文件cp覆盖导致调用者core的研究
先说cp好mv/rm的区别: cp from to,则被覆盖文件 to的inode依旧不变(属性也不变),内容变为from的: mv from to,则to的inode变为from的,相应的,to的属 ...
- 正则表达式 判断 ip:端口 形式
<html> <head> </head> <body> ip:port<input type="" name="z ...