给你两个子串,让你找出来一个最短的字符串包括这两个子串,输出最多的子串有多少种。

类似于最长公共子序列,相等的话长度+1,不想等的话比較长度,使用长度小的。

1577. E-mail

Time limit: 1.0 second

Memory limit: 64 MB
Vasya started to use the Internet not so long ago, so he has only two e-mail accounts at two different servers. For each of them he has a password, which is a non-empty string consisting of only lowercase
latin letters. Both mail servers accept a string as a password if and only if the real password is its subsequence.
Vasya has a hard time memorizing both passwords, so he would like to come up with a single universal password, which both servers would accept. Vasya can't remember too long passwords, hence he is interested
in a universal password of a minimal length. You are to help Vasya to find the number of such passwords.

Input

The input consists of 2 lines, each of them containing the real password for one of the servers. The length of each password doesn't exceed 2000 characters.

Output

Output the number of universal passwords of minimal length modulo 109 + 7.

Samples

input output
b
ab
1
abcab
cba
4

#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-8
#define M 1000100
#define LL long long
//#define LL long long
#define INF 0x3f3f3f
#define PI 3.1415926535898
#define mod 1000000007 const int maxn = 2010; using namespace std; LL dp[maxn][maxn];
LL len[maxn][maxn]; char str1[maxn];
char str2[maxn]; int main()
{
while(~scanf("%s %s",str1+1, str2+1))
{
int n = strlen(str1+1);
int m = strlen(str2+1);
memset(dp, 0, sizeof(dp));
memset(len, 0, sizeof(len));
for(int i = 0; i <= max(n, m); i++)
{
dp[i][0] = 1;
dp[0][i] = 1;
len[i][0] = i;
len[0][i] = i;
}
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
if(str1[i] == str2[j])
{
dp[i][j] = dp[i-1][j-1];
len[i][j] = len[i-1][j-1]+1;
continue;
}
if(len[i-1][j] > len[i][j-1])
{
dp[i][j] = dp[i][j-1];
len[i][j] = len[i][j-1]+1;
continue;
}
if(len[i][j-1] > len[i-1][j])
{
dp[i][j] = dp[i-1][j];
len[i][j] = len[i-1][j]+1;
continue;
}
len[i][j] = len[i-1][j]+1;
dp[i][j] += dp[i-1][j]+dp[i][j-1];
dp[i][j] %= mod;
}
}
cout<<dp[n][m]<<endl;
}
return 0;
}

URAL 1577. E-mail(简单二维dp)的更多相关文章

  1. 传纸条 NOIP2008 洛谷1006 二维dp

    二维dp 扯淡 一道比较基本的入门难度的二维dp,类似于那道方格取数,不过走过一次的点下次不能再走(看提交记录里面好像走过一次的加一次a[i][j]的也AC了,,),我记得当年那道方格取数死活听不懂, ...

  2. 关于二维DP————站上巨人的肩膀

    意匠惨淡经营中ing, 语不惊人死不休........ 前几天学了DP,做了个简单的整理,记录了关于DP的一些概念之类的,今天记录一下刚学的一个类型 ----关于二维DP 那建立二维数组主要是干嘛用的 ...

  3. 洛谷p1732 活蹦乱跳的香穗子 二维DP

    今天不BB了,直接帖原题吧  地址>>https://www.luogu.org/problem/show?pid=1732<< 题目描述 香穗子在田野上调蘑菇!她跳啊跳,发现 ...

  4. HDU - 2159 FATE(二维dp之01背包问题)

    题目: ​ 思路: 二维dp,完全背包,状态转移方程dp[i][z] = max(dp[i][z], dp[i-1][z-a[j]]+b[j]),dp[i][z]表示在杀i个怪,消耗z个容忍度的情况下 ...

  5. 洛谷P1048 采药 二维dp化一维

    题目描述 辰辰是个天资聪颖的孩子,他的梦想是成为世界上最伟大的医师.为此,他想拜附近最有威望的医师为师.医师为了判断他的资质,给他出了一个难题.医师把他带到一个到处都是草药的山洞里对他说:“孩子,这个 ...

  6. 洛谷1387(基础二维dp)

    题目很简单,数据也很小,但是思路不妨借鉴:dp[i][j]代表以(i,j)为右下角的最长正方形边长. 类比一维里面设“以XX为结尾的最XXX(所求)”. 另外define不要乱用!尤其这种min套mi ...

  7. 简单二维元胞自动机 MATLAB实现

    20世纪50年代,乌尔姆和冯·诺依曼(对此人真是崇拜的五体投地)为了研究机器人自我复制的可能性,提出了一种叫做元胞自动机(Cellular Automaton,CA)的算法.该算法采用局相互作用规则, ...

  8. BZOJ 2748: [HAOI2012]音量调节【二维dp,枚举】

    2748: [HAOI2012]音量调节 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 2010  Solved: 1260[Submit][Statu ...

  9. To the Max 二维dp(一维的变形)

    Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any ...

随机推荐

  1. FZU Problem 2156 Climb Stairs DP

    http://acm.fzu.edu.cn/problem.php?pid=2156 题目大意: 爬楼梯,要爬到n这个位置,每次可以走x也可以走y,然后一定要经过A和B两点,求最终到达n的方案数. 思 ...

  2. 最短路算法详解(Dijkstra/SPFA/Floyd)

    新的整理版本版的地址见我新博客 http://www.hrwhisper.me/?p=1952 一.Dijkstra Dijkstra单源最短路算法,即计算从起点出发到每个点的最短路.所以Dijkst ...

  3. 【LCS】POJ1458Common Subsequence

    题目链接:http://poj.org/problem?id=1458 这是一道最长公共子序列的模板题: #include<iostream> #include<string> ...

  4. Python 爬虫从入门到进阶之路(五)

    在之前的文章中我们带入了 opener 方法,接下来我们看一下 opener 应用中的 ProxyHandler 处理器(代理设置). 使用代理IP,这是爬虫/反爬虫的第二大招,通常也是最好用的. 很 ...

  5. 解决linux下cocos2dx不能播放声音

    cocos2dx2.2.1在linux下引用#include "SimpleAudioEngine.h".报错找不到该文件. 改动makefile文件,加入 SHAREDLIBS ...

  6. trident原理及编程指南

    目录 trident原理及编程指南 一.理论介绍 1.trident是什么? 2.trident处理单位 3.事务类型 二.编程指南 1.定义输入流 2.统计单词数量 3.输出统计结果 4.split ...

  7. 小型机与pc服务器区别

    按CPU的类型来区分,小型机是基于RISC(精简指令集)架构的专用服务器,而服务器是基于CISC(复杂指令集)架构的PC服务器.小型机相对于普通服务器来说,一直有不可比拟的优势.(1)如必能稳定,它具 ...

  8. 搭建hbase-0.94.26集群环境 分类: B7_HBASE 2015-01-24 17:14 861人阅读 评论(0) 收藏

    先安装hadoop1.2.1,见http://blog.csdn.net/jediael_lu/article/details/38926477 1.配置hbase-site.xml <prop ...

  9. sublime课程3 emmet插件中的常用符号有哪些

    sublime课程3 emmet插件中的常用符号有哪些 一.总结 一句话总结:emmet插件中的符号和css选择器里面哪些符号的意思很像. 1.+是干嘛的? 组合 2.{}是干嘛的? 标签里面的inn ...

  10. java痛苦学习之路[十二]JSON+ajax+Servlet JSON数据转换和传递

    1.首先client须要引入 jquery-1.11.1.js 2.其次javawebproject里面须要引入jar包  [commons-beanutils-1.8.0.jar.commons-c ...