Ural1577(DP)

F - E-mail
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Description

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 10  9 + 7.

Sample Input

input output
b
ab
1
abcab
cba
4

Hint

In the second sample, the passwords of minimal length are the following:  abcaba, abcbab, acbcab, cabcab.


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;

int lcs[2005][2005];
int dp[2005][2005];

int main()
{
	char str1[2005],str2[2005];
	scanf("%s%s",str1+1,str2+1);
	int length1=strlen(str1+1);
	int length2=strlen(str2+1);
	int i,j;

	for (i=0;i<=length1;i++)
	{
		lcs[i][0]=0;
		dp[i][0]=1;
	}
	for (i=0;i<=length2;i++)
	{
		lcs[0][i]=0;
		dp[0][i]=1;
	}

	for (i=1;i<=length1;i++)
		for (j=1;j<=length2;j++)
		{
			if (str1[i]==str2[j])
			{
				lcs[i][j]=lcs[i-1][j-1]+1;
				dp[i][j]=dp[i-1][j-1];
			}
			else if (lcs[i-1][j]>lcs[i][j-1])
			{
				lcs[i][j]=lcs[i-1][j];
				dp[i][j]=dp[i-1][j];
			}
			else if (lcs[i-1][j]<lcs[i][j-1])
			{
				lcs[i][j]=lcs[i][j-1];
				dp[i][j]=dp[i][j-1];
			}
			else
			{
				lcs[i][j]=lcs[i][j-1];
				dp[i][j]=(dp[i-1][j]+dp[i][j-1])%1000000007;
			}
		}
	printf("%d\n",dp[length1][length2]);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值