A Easy Dp Problem.
Code:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<set>
#include<queue>
#include<map>
#include<sstream>
#include<iostream>
using namespace std;
#define FOR(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
#define DOR(i,a,b) for(int (i)=(a);(i)>=(b);(i)--)
#define bug puts("Fuck");
#define LL long long
#define pb push_back
#define mp make_pair
#define nMax 2010
#define eps 1e-8
#define inf 0x7fffffff
char a[nMax],b[nMax];
int dp[nMax][nMax],l[nMax][nMax];
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
#endif
#define mod 1000000007
while(~scanf("%s%s",a+1,b+1)){
memset(dp,0,sizeof(dp));
int la=strlen(a+1),lb=strlen(b+1);
dp[0][0]=1;
for(int i=max(la,lb);i>=0;i--){
dp[i][0]=dp[0][i]=1;
l[i][0]=l[0][i]=i;
}
for(int i=1;a[i];i++) {
for(int j=1;b[j];j++){
if(a[i]==b[j]) {
dp[i][j] = dp[i-1][j-1];
l[i][j] = l[i-1][j-1] + 1;
}
else {
if(l[i-1][j]<l[i][j-1]){
dp[i][j] = dp[i-1][j];
l[i][j] = l[i-1][j] + 1;
}else if(l[i-1][j]>l[i][j-1]){
dp[i][j] = dp[i][j-1];
l[i][j] = l[i][j-1] + 1;
}else {
dp[i][j] = dp[i-1][j] + dp[i][j-1];
l[i][j] = l[i-1][j] + 1;
dp[i][j] %= mod;
}
}
}
}
printf("%d\n",dp[la][lb]);
}
return 0;
}