URAL 1577. E-mail(简单二维dp)

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

类似于最长公共子序列,相等的话长度+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;
}
时间: 2024-08-05 19:35:51

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

HDU 5074 Hatsune Miku(简单二维dp)

题目大意:给你一些音符之间的联系,给你一个串,让你求出这个串的最大值.-1的时候可以任意替代,其他情况必须为序列上的数. 解题思路:简单二维dp,分情况处理就可以了啊. Hatsune Miku Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 637    Accepted Submission(s): 458 Problem De

HDU 5119 Happy Matt Friends(简单二维dp)

题意不再说了,就是一个二维的dp,维持取最大值是多少. Happy Matt Friends Time Limit: 6000/6000 MS (Java/Others)    Memory Limit: 510000/510000 K (Java/Others) Total Submission(s): 608    Accepted Submission(s): 229 Problem Description Matt has N friends. They are playing a ga

hoj_10014_二维DP

The Triangle Time Limit: 1000ms, Special Time Limit:2000ms, Memory Limit:32768KB Total submit users: 952, Accepted users: 860 Problem 10014 : No special judgement Problem description 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 (Figure 1) Figure 1 shows a number tr

『进阶DP专题:二维DP初步』

<更新提示> <第一次更新> <正文> 二维动态规划 初步 二维动态规划并不是指动态规划的状态是二维的,而是指线性动态规划的拓展,由线性变为了平面,即在一个平面上做动态规划. 例题 马拦过河卒 题目描述 棋盘上A点有一个过河卒,需要走到目标B点.卒行走的规则:可以向下.或者向右.同时在棋盘上C点有一个对方的马,该马所在的点和所有跳跃一步可达的点称为对方马的控制点.因此称之为"马拦过河卒". 棋盘用坐标表示,A点(0, 0).B点(n, m)(n, m

HDU 4901 The Romantic Hero(二维dp)

题目大意:给你n个数字,然后分成两份,前边的一份里面的元素进行异或,后面的一份里面的元素进行与.分的时候按照给的先后数序取数,后面的里面的所有的元素的下标一定比前面的大.问你有多上种放元素的方法可以使得前面异或的值和后面与的值相等. dp[x][y] 表示走到第x步,得到y这个数字一共有多少种方法. 但是需要注意这里得分一下,不能直接用dp数组存种数,你需要分一下从上一层过来的次数,和这一层自己可以到达的次数.然后取和的时候前后两个集合的种数进行乘法,注意边乘边取余. 顺便给一组数据: 4 3

二维dp(O(N^3)实现) zoj3230

1 #include<iostream> 2 #include<string.h> 3 #include<stdio.h> 4 #define maxn 125 5 using namespace std; 6 7 int cost[maxn][maxn],w[maxn][maxn]; 8 int dp[maxn][maxn]; 9 int N,M; 10 int main(){ 11 while(cin>>N>>M){ 12 if (N==0

XTU1168:Alice and Bob(二维DP)

摘要:Dota(Defence of the Ancients,远古的守护), 是指基于魔兽争霸3:冰封王座(暴雪娱乐公司出品)的多人即时对战自定义地图,可支持10个人同时连线游戏.Dota以对立的两个小队展开对战,通常是5v5,游戏目的是守护自己的远古遗迹(近卫方的生命之树.天灾方的冰封王座),同时摧毁对方的远古遗迹.DotA是目前唯一被暴雪娱乐公司官方认可的魔兽争霸RPG.Dota在大学生中的风靡程度令人咂舌,而随着玩家对游戏的理解深入,本身存在于游戏中的许多数学模型被挖掘出来进行研究.游戏

(review)zoj4800 二维dp 状态转移很灵活

1 #include<iostream> 2 #include<stdio.h> 3 4 using namespace std; 5 6 double dp[10005][125]; 7 double p[125][125]; 8 int pk[10005]; 9 10 int N,M; 11 12 double fmax(double a,double b){ 13 if(a-b>0) return a;else return b; 14 } 15 int main(){

NOJ1060 接苹果 二维DP

题目描述 很少有人知道奶牛爱吃苹果.农夫约翰的农场上有两棵苹果树(编号为1和2), 每一棵树上都长满了苹果.奶牛贝茜无法摘下树上的苹果,所以她只能等待苹果 从树上落下.但是,由于苹果掉到地上会摔烂,贝茜必须在半空中接住苹果(没有人爱吃摔烂的苹果).贝茜吃东西很快,她接到苹果后仅用几秒钟就能吃完.每一分钟,两棵苹果树其中的一棵会掉落一个苹果.贝茜已经过了足够的训练, 只要站在树下就一定能接住这棵树上掉落的苹果.同时,贝茜能够在两棵树之间 快速移动(移动时间远少于1分钟),因此当苹果掉落时,她必定站