zoukankan      html  css  js  c++  java
  • HDU

    Problem Describe

    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

    题意 : 每次能将一个区间里面的字母改成一个同一个字母,问最少需要多少次将str1串变成str2串

    思路 : 这个题感觉很怪 ,明显的区间dp ,可是偏偏无从下手 , 它每次dp的时候str1 串 和 str2串匹配的时候总是感觉很难搞 , 用平常的区间dp写了一下 WA了 , 于是菜的我去搜了题解 发现区间 dp 还可以这样搞 ,神奇 呐~~

    AC code :

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    const int maxn = 110 ;
    
    int dp[maxn][maxn] ,len ,ans[maxn] ;
    char str1[maxn] ,str2[maxn] ;
    
    int main() {
    	while(~scanf("%s %s",str1 + 1 ,str2 + 1 )) {
    		len = strlen(str1 + 1) ;
    		memset(dp ,0 , sizeof(dp) ) ;
    		memset(ans ,0 ,sizeof(ans) ) ;
    		for (int i = 1 ; i <= len ; i ++) {
    			for (int j = i ; j <= len ; j ++ ) {
    				dp[i][j] = j - i + 1 ;
    			}
    		}
    		for (int l = 2 ; l <= len ; l ++ ) {
    			for (int i = 1 ; i <= len - l + 1 ; i ++) {
    				int j = i + l - 1;
    				if ( str2[i] == str2[j] ) dp[i][j] = dp[i][j-1];
    				else dp[i][j] = dp[i][j-1] + 1;
    				for (int k = i ; k <= j - 1 ; k ++ ) {
    					dp[i][j] = min(dp[i][j] ,dp[i][k] + dp[k+1][j] ) ;
    				}
    			}
    		} 
    		for (int i = 1 ; i <= len ; i ++ ) ans[i] = dp[1][i] ;
    		for (int i = 1 ; i <= len ; i ++ ) {
    			if ( str1[i] == str2[i] ) {
    				ans[i] = ans[i-1];
    			}
    			for (int j = 1 ; j <= i - 1 ; j ++ ) {
    				ans[i] = min(ans[i] ,ans[j] + dp[j+1][i] );
    			}
    		}
    		printf("%d
    ",ans[len]);
    	}
    	return 0;
    } 
    
  • 相关阅读:
    编程小技巧之 Linux 文本处理命令(二)
    基金投资经验总结
    基金投资需要掌握的几个基本概念
    动目标指示MTI和动目标检测MTD
    雷达信号处理基础-快时间维和慢时间维
    信号处理第二篇——接着谈正弦信号
    毫米波雷达第一篇——测距篇
    信号处理第一篇——浅谈正弦信号
    python学习总结篇(3)——类
    python中的if __name__ == '__main__' 如何正确理解?
  • 原文地址:https://www.cnblogs.com/Nlifea/p/11745936.html
Copyright © 2011-2022 走看看