zoukankan      html  css  js  c++  java
  • String painter

    String painter

    Problem Description

    There are two strings A and B with equal length. Both strings are made up of lower case letters. Now you have a powerful string painter. With the help of the painter, you can change a segment of characters of a string to any other character you want. That is, after using the painter, the segment is made up of only one kind of character. Now your task is to change A to B using string painter. What’s the minimum number of operations?

    Input

    Input contains multiple cases. Each case consists of two lines:
    The first line contains string A.
    The second line contains string B.
    The length of both strings will not be greater than 100.

    Output

    A single line contains one integer representing the answer.

    Sample Input
    zzzzzfzzzzz
    abcdefedcba
    abababababab
    cdcdcdcdcdcd
    
    Sample Output
    6
    7
    

    给定两个字符串a,b,可以在a的某个区间里全涂成某个字符,求变成b的最少涂法。
    这题想不到的话很难,看了题解也不容易懂。
    要做两次dp,第一次:
    dp[i][j]:把一个空串涂成b[i][j]之间的字符的最小花费。
    因为是区间涂色,所以如果涂色区间有交集的话,先涂上的区间可以缩小,因为后涂的区间会把之前的覆盖,相当于没有交集。
    所以,长度为1,dp[i][j]=1。
    如果b区间两端点相同,即 b[i]==b[j],则dp[i][j]=dp[i+1][j],不是dp[i+1][j-1]+1,因为如果b[i+1] 和b[j-1]中都不和b[i]一样,dp[i+1][j-1]+1也对,如果有一个或两个一样,显然这样就不是最优答案。
    第二次:
    ans[i]: a字符串前i个与b的前i个涂成一样的最小花费,
    当a[i]=b[i]时,ans[i]=ans[i-1],因为涂i-1的时候可以往右延伸一个,把前i个也涂了
    否则,ans[i]=min(ans[k]+dp[k+1][i]),因为不相等,所以i位置必定要涂

    #include<bits/stdc++.h>
    #include<stdio.h>
    #include<algorithm>
    #include<ctype.h>
    using namespace std;
    template<class T> inline bool read(T &x){
        x=0;register char c=getchar();register bool f=0;
        while(!isdigit(c)){if(c==EOF)return false;f^=c=='-',c=getchar();}
        while(isdigit(c))x=(x<<3)+(x<<1)+(c^48),c=getchar();
        if(f)x=-x;
        return true;
    }
    template<class T>inline void print (T x){
        if(x<0)putchar('-'),x=-x;
        if(x>9)print (x/10);
        putchar('0'+x%10);
    }
    #define Init(a,v) memset(a,v,sizeof(a))
    typedef long long ll;
    const ll MAXN=1e2+8,inf=0x3f3f3f3f,mod=1e9+7;
    char a[MAXN],b[MAXN];
    int n,dp[MAXN][MAXN],ans[MAXN];
    int main() {
        a[0]=b[0]='a';
        while(~scanf("%s%s",a+1,b+1)){
            n=strlen(a)-1;
            for(int i=1;i<=n;++i)dp[i][i]=1;
            for(int len=2;len<=n;++len){
                for(int i=1,j=len;j<=n;++i,++j){
                    dp[i][j]=len;
                    if(b[i]==b[j])dp[i][j]=dp[i+1][j];
                    else{
                        for(int k=i;k<j;++k)
                            dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]);
                    }
                }
            }
            ans[1]=(a[1]!=b[1]);
            for(int i=2;i<=n;++i){
                ans[i]=i;
                if(a[i]==b[i])ans[i]=ans[i-1];
                else{
                    for(int k=0;k<i;++k)
                        ans[i]=min(ans[i],ans[k]+dp[k+1][i]);
                }
            }
            print(ans[n]);
            putchar('
    ');
        }
        return 0;
    }
    
  • 相关阅读:
    Redux 学习总结
    ECMAScript 6 学习总结
    Bootstrap 前端UI框架
    React.js 学习总结
    html 之 <meta> 标签之http-equiv
    Leetcode Excel Sheet Column Number (C++) && Excel Sheet Column Title ( Python)
    490
    414
    494
    458
  • 原文地址:https://www.cnblogs.com/foursmonth/p/14155960.html
Copyright © 2011-2022 走看看