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的更多相关文章

  1. 【二进制枚举+LCS】Card Hand Sorting

    [二进制枚举+LCS]Card Hand Sorting 题目描述 When dealt cards in the card game Plump it is a good idea to start ...

  2. 51Nod 1006:最长公共子序列Lcs(打印LCS)

    1006 最长公共子序列Lcs  基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的). ...

  3. HDU 1159 Common Subsequence:LCS(最长公共子序列)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 题意: 求最长公共子序列. 题解: (LCS模板题) 表示状态: dp[i][j] = max ...

  4. 最大匹配字符串LCS,The Longest Common Substring

    public enum BackTracking { UP, LEFT, NEITHER, UP_AND_LEFT } public abstract class LCSBaseMatch { /// ...

  5. (字符串)最长公共子序列(Longest-Common-Subsequence,LCS)

    问题: 最长公共子序列就是寻找两个给定序列的子序列,该子序列在两个序列中以相同的顺序出现,但是不必要是连续的. 例如序列X=ABCBDAB,Y=BDCABA.序列BCA是X和Y的一个公共子序列,但是不 ...

  6. 我的第一篇博客----LCS学习笔记

    LCS引论 在这篇博文中,博主要给大家讲一个算法----最长公共子序列(LCS)算法.我最初接触这个算法是在高中学信息学竞赛的时候.那时候花了好长时间理解这个算法.老师经常说,这种算法是母算法,即从这 ...

  7. 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( ...

  8. 整理的一些模版LCS(连续和非连续)

    对于连续的最大串,我们称之为子串....非连续的称之为公共序列.. 代码: 非连续连续 int LCS(char a[],char b[],char sav[]){ int lena=strlen(a ...

  9. 最长公共子序列(LCS问题)

    先简单介绍下什么是最长公共子序列问题,其实问题很直白,假设两个序列X,Y,X的值是ACBDDCB,Y的值是BBDC,那么XY的最长公共子序列就是BDC.这里解决的问题就是需要一种算法可以快速的计算出这 ...

随机推荐

  1. Beta阶段项目总结

    1.   每个成员在beta 阶段的实践和alpha 阶段有何改进? 王文奇:对数据库的操作更为熟练,在java web中实现对数据库的修改更加完善 刘元柱:对javascript,css和servl ...

  2. SDC Tcl package of Timequest

    Tcl comand Tcl Commands all_clocks all_inputs all_outputs all_registers create_clock create_generate ...

  3. apache和tomcat有什么不同,为什么要整合apache 和tomcat

    1. Apache是web服务器,Tomcat是应用(java)服务器,它只是一个servlet容器,是Apache的扩展.2. Apache和Tomcat都可以做为独立的web服务器来运行,但是Ap ...

  4. webstorm快捷键大全

    使用webstorm一段时间了,这里分享一下常用到的快捷键,不用死记,孰能生巧! Ctrl+/ 或 Ctrl+Shift+/ 注释(// 或者/*-*/ ) Shift+F6 重构-重命名 Ctrl+ ...

  5. 优先队列实现Huffman编码

    首先把所有的字符加入到优先队列,然后每次弹出两个结点,用这两个结点作为左右孩子,构造一个子树,子树的跟结点的权值为左右孩子的权值的和,然后将子树插入到优先队列,重复这个步骤,直到优先队列中只有一个结点 ...

  6. iOS如何彻底避免数组越界

    我们先来看看有可能会出现的数组越界Crash的地方: ? 1 2 3 4 5 6 7 - (void)tableView:(UITableView *)tableView didSelectRowAt ...

  7. c++中的继承与初始化

    1.在c++中构造函数.析构函数.=运算符.友元无法继承 2.const 成员.引用成员.类的对象成员没有默认构造函数时,需在类的构造函数初始化列表中对其进行初始化 3.基类无默认构造函数,派生类需在 ...

  8. The difference between QA, QC, and Test Engineering

    Tuesday, March 06, 2007 Posted by Allen Hutchison, Engineering Manager and Jay Han, Software Enginee ...

  9. yii框架安装

    YII安装:      下载最版本http://www.framework.com 下载高级的->yii with advanced APPlication template 解压至访问目录下 ...

  10. js 实时监听input中值变化

    注意:用到了jquery需要引入jquery.min.js. 需求: 1.每个地方需要分别打分,总分为100; 2.第一个打分总分为40; 3.第二个打分总分为60. 注意:需要判断null.&quo ...