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;
    } 
    
  • 相关阅读:
    SharePoint 2010 WebPart Web部件 总贴
    使用SPD打开sharepoint2013网站另存为模板的功能
    Sahrepoint2013 创建HTML布局页面
    文本文件与二进制文件
    理论篇 HTTP笔记
    移动篇 视口viewport
    JS篇 命名规范: AMD、CMD、CommonJS
    CSS篇 CSSOM
    JS篇 学习笔记
    CSS篇 Process Document第7章 Technical Report Development Process翻译
  • 原文地址:https://www.cnblogs.com/Nlifea/p/11745936.html
Copyright © 2011-2022 走看看