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.这里解决的问题就是需要一种算法可以快速的计算出这 ...
随机推荐
- 成功开发iPhone软件的10个步骤
总结 几条要注意的原则: 1.了解你的用户,并与他们接触.交谈. 2.不要做虚幻的想象的设计,多从成功软件中汲取经验. 3.软件要设计得“小”. 4.找到足够多的设计方案,通过数量的累计来得到好的质量 ...
- 在Azure虚拟机上安装SQL server
Azure虽然向用户提供SQL paas服务,但是大多数用户还是习惯在用虚拟机自己搭建SQL server,这样的好处是便于后期最大化的扩展,所以鉴于这些情况,所以觉得有必要写这篇博客. 首先,我们要 ...
- ASP.NET上实现
ASP.NET上实现 fengzhuang.cs: using System;using System.Collections.Generic;using System.Linq;using Syst ...
- [转]SpringMVC Controller介绍及常用注解
一.简介 在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层处理之后封装成一个Model ,然后再把该Mo ...
- 怎么把Maven项目转为动态Web项目?
- $q -- AngularJS中的服务(理解)
描述 译者注: 看到了一篇非常好的文章,如果你有兴趣,可以查看: Promises与Javascript异步编程 , 里面对Promises规范和使用情景,好处讲的非常好透彻,个人觉得简单易懂. ...
- js 控制文本只能输入数字
代码如下: <input onkeypress="setNumber()"><script> function setNumber(){ var keyCo ...
- java 连接接数据库 中的代码 放到配置文件中
1.DButil.java package com.jobproject.util; import java.sql.Connection;import java.sql.DriverManager; ...
- rsa密钥文件转化为tortoise认可的pak密钥文件
原贴地址: http://www.vectorns.com/blog/technical-articles/1-tortoisesvn-over-ssh-on-windows-via-putty Ne ...
- Yii 1开发日记 -- 后台搜索功能下拉及关联表搜索
Yii 1 实现后台搜索,效果如下: 一. 下拉搜索: 1.模型中和常规的一样 if (isset($_GET['agency']['status']) && $_GET['agenc ...