洛谷P2470||bzoj1068 [SCOI2007]壓縮
阿新 • • 發佈:2018-11-04
區間dp入門題?只要注意到每個M“管轄”的區間互不相交即可
錯誤記錄:有點小坑,比如aaaacaaaac最優解為aRRcR(意會坑在哪裡),踩了一次
1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 #include<vector> 5 #include<cassert> 6 using namespace std; 7 #define fi first 8 #defineView Codese second 9 #define mp make_pair 10 #define pb push_back 11 typedef long long ll; 12 typedef unsigned long long ull; 13 typedef pair<int,int> pii; 14 int an[101][101]; 15 bool v1[101][101]; 16 int tmp[101]; 17 char s[1010]; 18 int n; 19 int solve(int l,int r) 20 { 21 assert(l<=r);22 if(l==r) return 1; 23 if(v1[l][r]) return an[l][r]; 24 int ans=0x3f3f3f3f; 25 for(int i=l;i<r;++i) 26 ans=min(ans,solve(l,i)+solve(i+1,r)); 27 tmp[l-1]=0; 28 for(int p=l;p<=r;++p) 29 { 30 tmp[p]=tmp[p-1]+1; 31 if((p-l+1)%2==0&&!strncmp(s+l,s+l+(p-l+1)/2,(p-l+1)/2)) 32 tmp[p]=min(tmp[p],tmp[l+(p-l+1)/2-1]+1); 33 } 34 ans=min(ans,tmp[r]+(l!=1)); 35 //printf("1t%d %d %d\n",l,r,ans); 36 v1[l][r]=1; 37 return an[l][r]=ans; 38 } 39 int main() 40 { 41 scanf("%s",s+1);n=strlen(s+1); 42 printf("%d\n",solve(1,n)); 43 return 0; 44 }