Ural1577(DP)

2372 단어
F - E-mail
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit 
Status 
Practice 
URAL 1577
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
",dp[length1][length2]); }

좋은 웹페이지 즐겨찾기