http://acm.hdu.edu.cn/showproblem.php?pid=5791
Both
problem DescriptionAlice Gets the sequences A and B. A easy problem comes. How many pair of sequence A ' and sequence B ' is same. For example, {the} and {same} are. {1,2,4} and {1,4,2} is not same. A subsequence of a. B ' is a subsequence of B. The subsequnce can is not continuous. For example, {1,1,2} has 7 subsequences {1},{1},{2},{1,1},{1,2},{1,2},{1,1,2}. The answer can be very large. Output the answer mod 1000000007.
Input The input contains multiple test cases.
for each test case, the first line cantains the integers n,m(1≤n,m≤ ). The next line contains N integers. The next line followed M integers. All integers is between 1 and.
Outputfor each test case, output the answer mod 1000000007.
Sample Input3 21 2 32 13 21 2 31 2
Sample Output23
Test instructions: There are two strings, for 22 substrings the same number of numbers (can be discontinuous).
Idea: A bit similar to the LCS DP.
1#include <cstdio>2#include <algorithm>3#include <cstring>4 using namespacestd;5 #defineN 10056 #defineMOD 10000000077typedefLong LongLL;8 9 LL Dp[n][n];Ten intA[n], b[n]; One /* A 1 2 3 - 2 1 - */ the intMain () - { - intN, M; - while(~SCANF ("%d%d", &n, &m)) { +dp[0][0] =0; - for(inti =1; I <= N; i++) { +scanf"%d", A +i); Adp[i][0] =0; at } - for(inti =1; I <= m; i++) { -scanf"%d", B +i); -dp[0][i] =0; - } - /* in with these three parts - Dp[i-1][j-1] to Dp[i-1][j]-dp[i-1][j-1] + Dp[i][j-1]-dp[i-1][j-1] - if not match dp[i][j] = dp[i-1][j]-dp[i-1][j-1] + dp[i][j-1]-dp[i-1][j-1] + dp[i-1][j-1] the match words dp[i][j] = mismatched status + dp[i-1][j-1] + 1 * Dp[i-1][j] + dp[i][j-1]-dp[i-1][j-1] Indicates how many states currently do not match, because Dp[i-1][j] and dp[i][j] are duplicated, so subtract a $ If the current match is not subtracted, it is necessary to leave one to match the current a[i] and b[j]. Panax Notoginseng */ - for(inti =1; I <= N; i++) { the for(intj =1; J <= M; J + +) { + if(A[i] = =B[j]) { ADP[I][J] = (dp[i-1][J] + dp[i][j-1] +1+ MOD)%MOD; the}Else { +DP[I][J] = (dp[i-1][J] + dp[i][j-1]-dp[i-1][j-1] + MOD)%MOD; - } $ } $ } - -printf"%i64d\n", Dp[n][m]%MOD); the } - return 0;Wuyi}
HDU 5791:two (DP)