Bzoj 1090 SCOI2003 string folding dynamic planning +hash

Source: Internet
Author: User

Topic: Given a string, in accordance with the problem of the compression method to the shortest possible compression to how long

Interval DP Order F[i][j] Indicates how long a string within the [I,j] interval can be compressed to

Normal interval dp:f[i][j]=min{f[i][k]+f[k+1][j]} (i<=k<=j-1)

In addition, if the string is compressed, then we can enumerate the loop section, using hash to determine

If k is a cyclic section, then there is f[i][j]=min (f[i][j],f[i][i+k-1]+digit[len/k]+2)

Where len=j-i+1,digit represents the length of a number under a decimal

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define M 110# Define BASE 151using namespace Std;typedef unsigned long long ll;int N,digit[m],f[m][m];char s[m];ll hash[m],power[m];voi D pretreatment () {int i;for (i=1;i<=n;i++) digit[i]=digit[i/10]+1;for (power[0]=1,i=1;i<=n;i++) power[i]=power[ I-1]*base;for (i=1;i<=n;i++) hash[i]=hash[i-1]*base+s[i];} int main () {int i,j,k,len;scanf ("%s", s+1); N=strlen (s+1); Pretreatment (); memset (f,0x3f,sizeof f); for (i=1;i<=n;i++) f[i][i]=1;for (len=2;len<=n;len++) for (I=1; (j=i+ len-1) <=n;i++) {for (k=i;k<j;k++) f[i][j]=min (F[i][j],f[i][k]+f[k+1][j]), for (k=1;k<n;k++) if (len%k==0) {ll Hash1=hash[j-k]-hash[i-1]*power[len-k];ll hash2=hash[j]-hash[i+k-1]*power[len-k];if (HASH1!=HASH2) continue;f[i][ J]=min (f[i][j],f[i][i+k-1]+digit[len/k]+2);}} Cout<<f[1][n]<<endl;return 0;}


Bzoj 1090 SCOI2003 string folding dynamic planning +hash

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.