The first question is bare dp ....
Second question or bare DP, note a special case (I forgot what the special case was QAQQQ)
The scrolling array can be pressed off one dimension, and ^ (XOR) ratio! (not) Slow ...
1 /**************************************************************2 problem:24233 User:rausen4 language:c++5 result:accepted6 time:964 Ms7 memory:892 KB8 ****************************************************************/9 Ten#include <cstdio> One#include <cstring> A#include <algorithm> - - using namespacestd; the Const intN =5005; - Const intMoD =100000000; - intf[2][n], g[2][n]; - intL1, L2, Cur, I, J; + CharS1[n], s2[n]; - + voidRead () { A CharCH =GetChar (); at while(Ch <'A'|| CH >'Z') && ch! ='.') -CH =GetChar (); - while(ch! ='.') -S1[++L1] = ch, ch =GetChar (); -CH =GetChar (); - while(Ch <'A'|| CH >'Z') && ch! ='.') inCH =GetChar (); - while(ch! ='.') toS2[++L2] = ch, ch =GetChar (); + } - the intMain () { * read (); $ for(j =0; J <= L2; ++j)Panax Notoginsengg[0][J] =1; -g[1][0] =1; theCur =0; + for(i =1; I <= L1; ++i) A for(j =1, cur ^=1; J <= L2; ++j) the if(S1[i] = =S2[j]) { +F[CUR][J] = f[!cur][j-1] +1; -G[CUR][J] = g[!cur][j-1]; $ if(F[cur][j] = = F[cur][j-1]) $G[CUR][J] + = g[cur][j-1], G[cur][j]%=MoD; - if(F[cur][j] = = f[!cur] [j]) -G[CUR][J] + = G[!cur][j], g[cur][j]%=MoD; the}Else{ -F[CUR][J] = F[!cur][j], g[cur][j] = g[!cur] [j];Wuyi if(F[cur][j] < f[cur][j-1]){ theF[CUR][J] = f[cur][j-1]; -G[CUR][J] = g[cur][j-1]; Wu}Else if(F[cur][j] = = F[cur][j-1]){ -G[CUR][J] + = g[cur][j-1]; About if(F[!cur][j-1] ==F[cur][j]) $G[CUR][J]-= g[!cur][j-1]; -G[CUR][J]%=MoD; - } - } Aprintf"%d\n%d\n", F[cur][l2]% mod, G[CUR][L2]%MoD); + return 0; the}View Code
BZOJ2423 [HAOI2010] Longest common sub-sequence