"Bzoj 3864" Hero meets DEVIL-DP set dp__ dynamic programming

Source: Internet
Author: User

The CLS is a good early question ... Very classic Ah ...
For a string s s with a length of n N, how many string T t of length M M makes the LCS (S,T) LCS (s,t) 0,1...N 0,1...N respectively.

We want to be able to use a f[j][{lcs[1][j],lcs[2][j],lcs[3][j]...lcs[n][j]}] f[j][\{lcs[1][j],lcs[2][j],lcs[3][j]...lcs[n][j]\}] To represent the current state to calculate the answer.
At first it seems to be trying to directly use the subsequence to allow the ... Will find it difficult and not too right. So go back to the LCS calculation to see what the properties are available.
Lcs[i][j]=max{lcs[i−1][j],lcs[i][j−1], [S[i]==t[j]]∗ (Lcs[i−1][j−1]+1)} Lcs[i][j]=\max\{lcs[i-1][j],lcs[i][j-1], [ S[i] = = T[j]] * (Lcs[i-1][j-1] + 1) \}
Notice that in this DP matrix, the difference between any two adjacent squares is at most 1, so that we can use a pressure to make the difference between adjacent lattices to represent the LCS State of T-T string to J-bit.
For transitions between states, you can enumerate the LCS LCS status of the previous line, and then enumerate the current rows and what characters to match, to calculate the current line LCS LCS status, and to get the state of the shape pressure. Simply preprocessing the matching character of the current row can be enumerated to quickly complete the transfer.
The preprocessing complexity is O (n2n) O (n2^n), and the DP complexity is O (m2n) O (m2^n).
It's a bit of a hassle to be clear about this stuff ...

  
The code of the Ugly one B:

* I'll chase the meteor for you, a thousand times over.
    Please wait for me, until I fade forever.
Just ' coz geotcbrl.
* * #include <bits/stdc++.h> using namespace std; #define FORE (I,u) for (int i = head[u]; i; i = nxt[i]) #define REP (i,a,b) for (int i = A, _ = B; I <= _; i + +) #d
Efine per (int i = A, _ = B; I >= _; I-) #define for (I,A,B) for (int i = A, _ = B; i < _; i + +) #define DWN (I,A,B) for (int i = (int) a)-1, _ = (b); I >= _; I-) #define Fors (s0,s) for (int s0 = (s), _s = s ; S0; S0 = (s0-1) & _s) #define foreach (It,s) for (__typeof (S.begin ()) it = S.begin (); it!= s.end (); it + +) #define MP M Ake_pair #define PB push_back #define PII pair<int, int> #define FIR #define SEC Second #define MS (x,a) Memse  T (x, (a), sizeof (x)) #define GPRINTF (...) fprintf (stderr, __va_args__) #define GOUT (x) std::cerr << #x << "=" << x << std::endl #define GOUT1 (a,i) Std::cerr <<#a << ' [' << i << ']= ' << a[i] << std::endl #define GOUT2 (a,i,j) std::cerr << #a <& Lt  ' [' << i << '] [<< J << ']= ' << a[i][j] << std::endl #define GARR (A,L,R,TP) Rep (__it , L, R) gprintf (tp "%c", a[__it], "\ n" [__it = _]) template <class t> inline void Upmax (T&a, T b) {if (a) < b) A = B;

} template <class t> inline void Upmin (T&a, T b) {if (a > B) a = b;}

typedef long Long LL;
const int MAXN = 100007;  Const

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.