ural 1577 E-mail

ural 1577 E-mail



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

[Submit]  [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

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.


开始拿到这个题目没有思路,忽然发现这个题目很想最长公共子序列,而且长度 为 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;
}


你可能感兴趣的:(ural 1577 E-mail)