Location>code7788 >text

dp set dp

Popularity:237 ℃/2024-08-09 20:23:57

Let's start with\(dp\) bend (of a river or mountain range, in place names)\(dp\) Probably a something.

To understand it more perceptually, you now have a dynamically programmed array\(g\)And then your\(f\) expense or outlay\(g\) (used form a nominal expression)somehowTransfer as a subscript.

In fact, this\(g\) needs to satisfy monotonicity, which is then equivalent to the fact that you are in a\(DAG\) do\(dp\).. Why is it important to satisfy monotonicity, otherwise there is the possibility of a ring, and having a ring means that your state has posteriority, and with posteriority you cannot transfer.

folk festival

First the dynamic programming of the longest common subsequence is well done and will not be mentioned here.

Here let the dynamic programming array of the longest common subsequence be\(g\)

Let's set\(f_{i,{w_1,w_2,\cdots,w_k}}\) Yes Length\(i\) both (... and...)\(g_{i,j}=w_j\) The number of strings in the

But then you realize\(w\) You can't save it. So we considered if there was anything of that nature that could moderator us to optimize.

Apparently.\(g_{i,j}-g_{i,j-1}\le 1\). So you consider\(f\) The back dimension of does not store the true sense of the\(w\)while storing\(w\) of the difference array, so that each bit of the\(0\) or\(1\). The number of states total\(n\times 2^k\), acceptable.

But then you realize that we know\(s_i\)(given in the title), which means that the current\(g_i\) just follow\(t_i\) cap (a poem)\(g_{i-1}\) Related. And so we were doing\(g\) The transfer is recorded only when the\(g_{i-1}\) Just do it (that's rolling array optimization).

And then there's our\(f\) It can also be optimized with rolling arrays or the space will explode.

Code:

#include<bits/stdc++.h>
#define int long long
#define mod 1000000007
#define N 40005
using namespace std.
int n,k,f[2][N][3],g[2][20],cnt[N],res[20];
char s[20];
void solve1(int state){
memset(g[0],0,sizeof g[0]);
for(int i=0;i<k;i++){
g[0][i+1]=state>>i&1;
}
for(int i=1;i<=k;i++){
g[0][i]+=g[0][i-1];
}
}
int solve2(){
int state=0;
for(int i=1;i<=k;i++){
int x=g[1][i]-g[1][i-1]; int state|=x*(1<i-1;=k;i++){
state|=x*(1<<i-1);
}
return state.
}
void solve(int cur,int state,char c,int now,int las){
if(las==0)return;//no contribution no need to transfer
if(c=='I'&&now==2)return;//includes substring NOI
int ne=c=='N'?1:0;//if N, next match from I, otherwise from N
if(now==0&&c=='N')ne=1;//if the current should match the first 0 and is N, after jumping a bit
else if(now==1&&c=='O')ne=2;//same as above
solve1(state);//restore the state of the differential record
memset(g[1],0,sizeof g[1]);//clear first
for(int i=1;i<=k;i++){
if(s[i]==c){//If s_i=t_j, you can use special transfer
g[1][i]=max(g[1][i],g[0][i-1]+1);//here g_{0,i-1} is lcs_{i-1,j-1}
}
g[1][i]=max({g[1][i],g[0][i],g[1][i-1]});//otherwise use a general transfer (g_{0,i} is lcs_{i,j-1} and g_{1,i-1} is lcs_{i-1,j})
}
int res=solve2();// get the current array into a differential state again and save it
(f[cur][res][ne]+=las)%=mod;//inherit the previous one
}
signed main(){
cin>>n>>k>>s+1;
int cur=0;
f[0][0][0][0]=1;
for(int i=1;i<=n;i++){
cur^=1;
memset(f[cur],0,sizeof f[cur]);
for(int j=0;j<1<<k;j++){
for(int p=0;p<3;p++){
char c=p==0?'N':p==1?'O':'I';
solve(cur,j,c,0,f[cur^1][j][0]);
solve(cur,j,c,1,f[cur^1][j][1]); solve(cur,j,c,1,f[cur^1][j][1]).
solve(cur,j,c,2,f[cur^1][j][2]);
}
}
}
for(int i=1;i<1<<k;i++){
cnt[i]=cnt[i>>1]+(i&1);//cnt is the number binary representation of this number has a few 1s, here there are a few 1s on behalf of the lcs length is a few
}
for(int i=0;i<1<<k;i++){
for(int j=0;j<3;j++){
(res[cnt[i]]+=f[cur][i][j])%=mod;
}
}
for(int i=0;i<=k;i++){
cout<<res[i]<<'\n';
}
return 0;
}

Independent sets of small N

An independent set is a set of points with no edges between them. A maximal weighted independent set is the one that has the largest sum of weights among all the independent sets.

Let's start this question with a partial score.

  • \(n\le 8\)

Enumerate all possible weight assignments. Then refer to thethis questionJust write a dynamic programming.

  • \(n\le 100\)

in the first place\(f\) Comparison of arrays andthis questionThe establishment of the same.

We consider the\(f\) The value of the array is set to the state.

found\(g_{u,v_0,v_1}\) because of\(f_{u,0}=v_0,f_{u,1}=v_1\) of the number of programs.

We enumerate\(i,j,p,q\) consist of\(f_{u,0},f_{u,1},f_{v,0},f_{v,1}\)

thus there is\(g'_{u,i+\max(p,q),j+p}\rightarrow g'_{u,i+\max(p,q),j+p}+g_{u,i,j}+g_{v,p,q}\)

  • \(n\le 1000\)

One important reason why the above transfer could not pass is that our state complexity is too high, so we have to optimize the state.

We redefine\(f_{u,1/0}\) because of\(u\) Whether or not it is mandatory within the subtreeunselectnodal\(u\) the maximum weights at the time. So there is\(0\le f_{u,0}-f_{u,1}\le k\). Since the difference between the two is no more than\(u\) The weights of the points, and the weights do not exceed\(k\)This optimizes away a\(n\)