ural 1577 E-mail

ural 1577 E-mail



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

[]  [Go Back]  [Status]  

Description

Vasya started to use the Internet not so long ago, so he hasonly two e-mail accounts at two different servers. For each of them he has a password, which is anon-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 upwith a single universal password, which both servers would accept. Vasya can'tremember too long passwords, hence he is interested in a universal passwordof 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

inputoutput
b
ab
1
abcab
cba
4

Hint

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


开始拿到这个题目没有思路,忽然发现这个题目很想最长公共子序列,而且长度 为 2000,如果想 dp 求最长公共子序列那样肯定O(n^2) 肯定可以解决

于是我试着在纸上模拟求解LIS,顺着求LIS的路径竟然推出了样例



#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int maxn=2010;
const int mod=1000000007;
int dp[maxn][maxn],num[maxn][maxn];
char s1[maxn],s2[maxn];

int main()
{
    while(scanf("%s %s",s1+1,s2+1)==2)
    {
        //cout<<"bug"<<endl;
        int len1=strlen(s1+1),len2=strlen(s2+1);
        for(int i=1;i<=len2;i++) num[0][i]=1,dp[0][i]=i;
        for(int i=1;i<=len1;i++) num[i][0]=1,dp[i][0]=i;
        dp[0][0]=0;
        num[0][0]=1;

        //cout<<len1<<" "<<len2<<endl;

        for(int i=1;i<=len1;i++)
        for(int j=1;j<=len2;j++)
          if(s1[i]==s2[j]) dp[i][j]=dp[i-1][j-1]+1;
          else dp[i][j]=min(dp[i-1][j]+1,dp[i][j-1]+1);

        for(int i=1;i<=len1;i++)
        for(int j=1;j<=len2;j++)
        {
            num[i][j]=0;
            if(s1[i]==s2[j]) num[i][j]=(num[i][j]+num[i-1][j-1])%mod;
            else{
              if(dp[i][j]==dp[i-1][j]+1) num[i][j]=(num[i][j]+num[i-1][j])%mod;
              if(dp[i][j]==dp[i][j-1]+1) num[i][j]=(num[i][j]+num[i][j-1])%mod;
            }
        }
        printf("%d\n",num[len1][len2]);
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值