// 统计 s1 的子序列中是 s1 和 s2 的最长公共子序列的个数
1 #include "bits/stdc++.h" 2 using namespace std; 3 int T; 4 char s1[1010], s2[1010]; 5 int dp[1010][1010]; 6 long long Count[1010][1010]; 7 const long long MOD = 1000000007; 8 9 int main() 10 { 11 int len1, len2; 12 scanf("%d", &T); 13 while(T--) { 14 memset(dp, 0, sizeof(dp)); 15 memset(Count, 0, sizeof(Count)); 16 scanf("%s", s1 + 1); 17 scanf("%s", s2 + 1); 18 len1 = strlen(s1 + 1); 19 len2 = strlen(s2 + 1); 20 int i, j; 21 22 for(i = 0; i <= len1; ++i) { 23 Count[i][0] = 1; 24 } 25 for(j = 0; j <= len2; ++j) { 26 Count[0][j] = 1; 27 } 28 29 for(i = 1; i <= len1; ++i) { 30 int k = 0, p = 0; 31 for(j = 1; j <= len2; ++j) { 32 // printf("%d %d ", i, j); 33 // if(s1[i] == s2[j]) { 34 // dp[i][j] = dp[i - 1][k] + 1; 35 // } 36 // else { 37 // dp[i][j] = dp[i - 1][j]; 38 // } 39 // if(dp[i - 1][k] < dp[i - 1][j]) { 40 // k = j; 41 // } 42 if(s1[i] == s2[j]) { 43 dp[i][j] = dp[i - 1][j - 1] + 1; 44 } 45 else { 46 dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); 47 } 48 49 if(s1[i] == s2[j]) { 50 p = j; 51 } 52 if(dp[i][j] == dp[i - 1][p - 1] + 1) { 53 Count[i][j] += Count[i - 1][p - 1]; 54 } 55 if(dp[i][j] == dp[i - 1][j]) { 56 Count[i][j] += Count[i - 1][j]; 57 } 58 Count[i][j] %= MOD; 59 } 60 } 61 printf("%lld ", Count[len1][len2]); 62 } 63 }