Solution: I hit it once, and I haven't written this DP in a long time.
1#include <cstdio>
2#include <cstring>
3#include <string>
4#include <vector>
5#include <algorithm>
6#include <iostream>
7 usingnamespaceStd
8typedefLongLongLL;
9ConstintMAX = 1e3+Ten;
TenConstintMOD = 1e9+7;
OneintDp[max][max];
ALL F[max][max];
-intnext[ -];
-CharA[max],b[max];
theintMain () {
-intCAs
-scanf"%d", &cas);
- while(cas--) {
+scanf"%s%s", A +1, B +1);
-Memset (DP,0,sizeof(DP));
+intN=strlen (A +1);
AintM=strlen (b +1);
at for(intI=1; i<=n;i++) {
- for(intj=1; j<=m;j++) {
-if(A[i]==b[j]) {
-Dp[i][j]=max (dp[i][j],dp[i-1][j-1]+1);
-}
-Else{
inDp[i][j]=max (dp[i][j-1],dp[i-1][J]);
-}
to}
+}
-Memset (F,0,sizeof(f));
the for(intI=0; i<=n;i++) {
*memset (Next,0,sizeof(Next));
$ for(intj=0; j<=m;j++) {
Panax Notoginsengif(dp[i][j]==0) {
-f[i][j]=1;
the}
+Else{
Anext[b[j]-'a']=j;
theif(dp[i-1][j]==dp[i][j]) {
+F[i][j]= (f[i][j]+f[i-1][J])%mod;
-}
$if(next[a[i]-'a']) {
$intp=next[a[i]-'a'];
-if(p&&dp[i-1][p-1]+1==dp[i][j]) {
-F[i][j]= (f[i][j]+f[i-1][p-1])%mod;
the}
-}
Wuyi}
the}
-}
Wucout<<f[n][m]<<endl;
-}
About }
Bestcoder Round #47 1003