Ural1577(DP)
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]);
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.