Dynamic Planning Learning Series-dividing DP (three)

Source: Internet
Author: User

Divide DP Third question, Wikioi 1040, send me n a wa~~~

Main topic:
This problem has UVA features, enough nonsense, is actually read into a length of 200 of the string (do not know why the branch input, completely meaningless AH), into the M part, so that the number of parts of the word amount of maximum
Problem Solving Ideas:
This part of the division with the largest product of the problem is actually very similar, the state transfer equation is also easy to think:
Dp[i][k]=max (Dp[i][k],dp[j][k-1]+scnt[j+1][i]) (J >= K-1)
SCNT array: Scnt[j][i] represents the total number of times that the interval j~i contains
The next question is, how to get the SCNT array, this should be considered an interval DP bar, but this is not as complex as the stone merge, the algorithm starts from the i=0 of the string (to Len-1), and then detects j=0 to j=i this I substring (j ~ i) Word number, state transfer equation :
Scnt[j][i] = scnt[j][i-1] + new_cnt
Where new_cnt represents the newly detected word, the specific code:

for(int k=0;k<cnt;k++){    int tmp_len=st[k].length();    if(i-tmp_len+1>=j&&str.substr(i-tmp_len+1,tmp_len)==st[k])        scnt[j][i]+=1;}

This problem has a big hole , that is, the words in the dictionary may be repeated, send me a lot of WA, but also have to spit the Wikioi test data-too little, sometimes some wrong code may be too.
AC Code:

#include <bits/stdc++.h>#define INF 0x3f3f3f3fUsing namespace Std;int n,a[ the],sum[ the],dp[ the][ the];int Main () {memset (Dp,inf,sizeof (DP));scanf ("%d", &n);for (int i=1;i<=n;i++)dp[i][i]=0;for (int i=1;i<=n;i++)scanf ("%d", &a[i]);sum[0]=0;for (int i=1;i<=n;i++)Sum[i]=sum[i-1]+a[i];for (int len=2;len<=n;len++)    {for (int i=1;i<=n-len+1;i++)        {int j=i+len-1;for (int k=i;k<j;k++)            {dp[i][j]=min (dp[i][j],dp[i][k]+dp[k+1][j]+sum[j]-sum[i-1]);            }        }    }printf ("%d\n", Dp[1][n]);return 0;}

Division dp+ interval DP, is the specific implementation of the algorithm, ladder division DP also brush out, the sense of division will be more than other types of bad think a little (remove the tree and state compression), although divided, but always from the supplement, add the angle to consider, But sometimes it fits the rules of our minds.

Dynamic Planning Learning Series-dividing DP (three)

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.