码迷,mamicode.com
首页 > 其他好文 > 详细

H - String painter

时间:2018-07-14 21:32:08      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:pac   ios   open   sync   区间   ems   bsp   cstring   max   

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstring>
 4 #include <cstdio>
 5 #include <string>
 6 #include <map>
 7 #include <cmath>
 8 #include <vector>
 9 
10 #define Faster ios::sync_with_stdio(false),cin.tie(0)
11 #define Read freopen("in.txt","r",stdin),freopen("out.txt","w",stdout)
12 #define Close fclose(stdin),fclose(stdout)
13 const int maxn = 105;
14 using namespace std;
15 const int MOD = 1e9+7;
16 typedef long long ll;
17 
18 char s1[maxn];
19 char s2[maxn];
20 int dp[maxn][maxn];
21 int len;
22 int ans[maxn];
23 
24 int main(){
25     Faster;
26     while(~scanf("%s%s", s1, s2)){
27         len = strlen(s1);
28         memset(dp, 0, sizeof(dp));
29         for(int j = 0;j < len; j++){    //j为尾
30             for(int i = j;i >= 0;i--){        //i为头
31                 dp[i][j] = dp[i+1][j] + 1;    //每个都单独刷一次
32                 for(int k = i+1;k <= j;k++){
33                     if(s2[i] == s2[k]){
34                         dp[i][j] = min(dp[i][j], (dp[i+1][k]+dp[k+1][j]));
35                     }
36                 }
37             }
38         }
39         for(int i = 0;i < len;i++){
40             ans[i] = dp[0][i];
41         }
42         for(int i = 0;i < len;i++){
43             if(s1[i] == s2[i]){
44                 ans[i] = ans[i-1];    //对应位置相等的话,就不用刷新
45             }
46             else{
47                 for(int j = 0;j < i;j++){
48                     ans[i] = min(ans[i], ans[j] + dp[j+1][i]);    //寻找j来分割区间的最优解
49                 }
50             }
51         }
52         printf("%d\n", ans[len-1]);
53     }
54     return 0;
55 }

 

H - String painter

标签:pac   ios   open   sync   区间   ems   bsp   cstring   max   

原文地址:https://www.cnblogs.com/jaydenouyang/p/9310932.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!