[CQOI]涂色

a799091501的题解

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 using namespace std;
 5 const int maxn=1007;
 6 int a[maxn],f[maxn][maxn];
 7 int n,ans;
 8 char k;
 9 int main(){
10     memset(f,0x7f,sizeof(f)); 
11     while(cin>>k) a[++n]=k-64;
12     for(int i=1;i<=n;i++) f[i][i]=1;
13     for(int l=1;l<=n;l++){
14         for(int i=1;i<=n;i++){
15             int j=i+l;if(j>n) break;
16             if(a[i]==a[j]){
17                 f[i][j]=min(f[i+1][j],f[i][j-1]);
18                 f[i][j]=min(f[i][j],f[i+1][j-1]+1);
19             }
20             for(int k=i;k<=j;k++) f[i][j]=min(f[i][j],f[i][k]+f[k+1][j]);
21         } 
22     }
23     cout<<f[1][n]<<endl;
24     return 0;
25 } 

这样是60分,但我不懂......

 1 #include<iostream>
 2 #include<cstdio>
 3 using namespace std;
 4 const int maxn=1007;
 5 int a[maxn],b[maxn],f[maxn];
 6 int n,ans;
 7 char k;
 8 int main(){
 9     while(cin>>k) a[++n]=k-64;
10     for(int i=1;i<=n;i++){
11         int j=n;
12         if(a[i]!=b[i]){
13             ans++;
14             while(a[j]==b[j]) j--;
15             for(int k=i;k<=j;k++) b[k]=a[i];
16         }
17     } 
18     cout<<ans<<endl;
19     return 0;
20 } 

 

posted @ 2018-08-28 16:30  lcan  阅读(139)  评论(0编辑  收藏  举报