标签:scanf print reg ash res turn 暴力 长度 bre
区间DP,枚举断点,对于一个区间,枚举折叠长度,用hash暴力判断是否能折叠即可
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define Re register
#define F(i,a,b) for(Re int i=(a);i<=(b);i++)
using namespace std;
typedef unsigned long long ull;
inline int read() {
int x=0,f=1;char c=getchar();
while(!isdigit(c)) {if(c=='-')f=-f;c=getchar();}
while(isdigit(c)) x=(x<<1)+(x<<3)+c-48,c=getchar();
return x*f;
}
const int N=110,bas=137;
int n;
ull hs[N],pw[N];
char s[N];
int dp[N][N];
ull get_h(int l,int r) {return hs[r]-hs[l-1]*pw[r-l+1];}
int get_w(int x) {int res=0;while(x) x/=10,res++;return res;}
int main() {
scanf("%s",s+1);n=strlen(s+1);
F(i,1,n) hs[i]=hs[i-1]*bas+s[i];
pw[0]=1; F(i,1,n) pw[i]=pw[i-1]*bas;
memset(dp,0x3f,sizeof(dp));
F(i,1,n) dp[i][i]=1;
F(len,2,n) for(Re int i=1,j=len;j<=n;i++,j++) {
F(k,i,j-1) dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]);
F(k,1,len) if(len%k==0) {
ull val=get_h(i,i+k-1);bool fla=0;
for(int l=i+k;l<=j;l+=k) if(get_h(l,l+k-1)!=val) {fla=1;break;}
if(!fla) dp[i][j]=min(dp[i][j],2+dp[i][i+k-1]+get_w(len/k));
}
}
printf("%d",dp[1][n]);
return 0;
}
[luogu1090 SCOI2003] 字符串折叠(区间DP+hash)
标签:scanf print reg ash res turn 暴力 长度 bre
原文地址:https://www.cnblogs.com/Menteur-Hxy/p/9740675.html