zoukankan      html  css  js  c++  java
  • H

     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
    ", ans[len-1]);
    53     }
    54     return 0;
    55 }
  • 相关阅读:
    Binary Search Tree Iterator 解答
    Invert Binary Tree 解答
    Min Stack 解答
    Trapping Raining Water 解答
    Candy 解答
    Jump Game II 解答
    Implement Hash Map Using Primitive Types
    Gas Station 解答
    Bucket Sort
    HashMap 专题
  • 原文地址:https://www.cnblogs.com/ouyang_wsgwz/p/9310932.html
Copyright © 2011-2022 走看看