4565: [Haoi2016] word matches and

Source: Internet
Author: User

Topic links

The main topic: there is a length of n 01 strings, you can each time the adjacent K-word match and, get a new character and get a certain score. Get the new word
Characters and fractions are determined by this K-character. You need to ask for the maximum score you can get.

Puzzle: Interval + pressure DP
See this for details.

My Harvest: 23333

#include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <

Algorithm> using namespace std;
#define N 305 #define LL long long int n,k;
int c[259],w[259];
ll F[n][n][259],inf;

Char Str[n];

inline void up (ll &x,ll y) {x=x>y?x:y;} 
            void work () {for (Int. l=2;l<=n;l++) for (int i=1;i<=n-l+1;i++) {int j=i+l-1,len=j-i;
            while (len>=k) len-=k-1; for (int m=j;m>i;m-=k-1) for (int s=0; s< (1<<len); s++) if (f[i][m-1][s]!=inf) {if (F[m][j][0]!=inf) up (f[i][j][s<<1],f[i][m-
                    if (F[m][j][1]!=inf) up (f[i][j][s<<1|1],f[i][m-1][s]+f[m][j][1]);
                } if (len==k-1) {ll g[2];g[0]=g[1]=inf; for (int s=0; s< (1<<k); s++) if (f[i][j][s]!=inf) g[c[S]]=max (G[c[s]],f[i][j][s]+w[s]);
}} cout<<*max_element (f[1][n],f[1][n]+ (1<<k)) <<endl;
    } void Init () {scanf ("%d%d%s", &n,&k,str+1);
    for (int i=0;i< (1<<k); i++) scanf ("%d%d", &c[i],&w[i]);
    memset (F,128,sizeof (f)); inf=f[0][0][0];
for (int i=1;i<=n;i++) f[i][i][str[i]-' 0 ']=0;
    } int main () {init ();
    Work ();
return 0; }

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.