洛谷P2470||bzoj1068 [SCOI2007]压缩

bzoj1068

洛谷P2470

区间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 #define se 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 }
View Code

 

posted @ 2018-11-04 22:13  hehe_54321  阅读(249)  评论(0编辑  收藏  举报
AmazingCounters.com