zoukankan      html  css  js  c++  java
  • BZOJ 1260&UVa 4394 区间DP

    题意:

      给一段字符串成段染色,问染成目标串最少次数.

    SOL:

      区间DP...

      DP[i][j]表示从i染到j最小代价

      转移:dp[i][j]=min(dp[i][j],dp[i+1][k]+dp[k+1][j]);

    CODE:

      BZ:

    /*=================================================================
    # Created time: 2016-03-28 21:10
    # Filename: uva4394.cpp
    # Description: 
    =================================================================*/
    #define me AcrossTheSky 
    #include <cstdio> 
    #include <cmath> 
    #include <ctime> 
    #include <string> 
    #include <cstring> 
    #include <cstdlib> 
    #include <iostream> 
    #include <algorithm> 
      
    #include <set> 
    #include <map> 
    #include <stack> 
    #include <queue> 
    #include <vector> 
     
    #define lowbit(x) (x)&(-x) 
    #define FOR(i,a,b) for((i)=(a);(i)<=(b);(i)++) 
    #define FORP(i,a,b) for(int i=(a);i<=(b);i++) 
    #define FORM(i,a,b) for(int i=(a);i>=(b);i--) 
    #define ls(a,b) (((a)+(b)) << 1) 
    #define rs(a,b) (((a)+(b)) >> 1) 
    #define getlc(a) ch[(a)][0] 
    #define getrc(a) ch[(a)][1] 
     
    #define maxn 500
    #define maxm 100000 
    #define pi 3.1415926535898 
    #define _e 2.718281828459 
    #define INF 1070000000 
    using namespace std; 
    typedef long long ll; 
    typedef unsigned long long ull; 
     
    template<class T> inline 
    void read(T& num) { 
        bool start=false,neg=false; 
        char c; 
        num=0; 
        while((c=getchar())!=EOF) { 
            if(c=='-') start=neg=true; 
            else if(c>='0' && c<='9') { 
                start=true; 
                num=num*10+c-'0'; 
            } else if(start) break; 
        } 
        if(neg) num=-num; 
    } 
    /*==================split line==================*/ 
    char A[maxn],C[maxn];
    int dp[maxn][maxn],ans[maxn];
    int main(){ 
    	freopen("a.in","r",stdin);
    	//while (scanf("%s",C)!=EOF){ 
    		 scanf("%s",A);
    		//FORP(i,0,maxn) B[i]='';
    		int n=strlen(A);
    		memset(dp,0,sizeof(dp));
    		FORP(i,0,n-1) 
    			FORP(j,i,n-1) dp[i][j]=INF;
    		FORP(i,0,n-1) dp[i][i]=1;
    		 FORP(l,1,n-1)
    		 	FORP(i,0,n-1-l) 
    			{
    				int j=i+l;
    				dp[i][j]=dp[i+1][j]+1;
    				FORP(k,i+1,j) 
    				if (A[k]==A[i]) dp[i][j]=min(dp[i][j],dp[i+1][k]+dp[k+1][j]);
    			}
    	printf("%d",dp[0][n-1]);
    	//FORP(i,0,n-1)
    		//	FORP(j,i,n-1) printf("%d%c",dp[i][j],j==n-1?'
    ':' ');
    		/*memset(ans,0,sizeof(ans));
    		ans[0]=(A[0]!=C[0]);
    		FORP(i,1,n-1)
    			if (A[i]==C[i])	ans[i]=ans[i-1];
    			else {
    				ans[i]=dp[0][i];
    				FORP(j,0,i-1) ans[i]=min(ans[i],ans[j]+dp[j+1][i]);
    			}
    		printf("%d
    ",ans[n-1]);
    	}*/
    }
    

     UVa:

      

    /*=================================================================
    # Created time: 2016-03-28 21:10
    # Filename: uva4394.cpp
    # Description: 
    =================================================================*/
    #define me AcrossTheSky 
    #include <cstdio> 
    #include <cmath> 
    #include <ctime> 
    #include <string> 
    #include <cstring> 
    #include <cstdlib> 
    #include <iostream> 
    #include <algorithm> 
      
    #include <set> 
    #include <map> 
    #include <stack> 
    #include <queue> 
    #include <vector> 
     
    #define lowbit(x) (x)&(-x) 
    #define FOR(i,a,b) for((i)=(a);(i)<=(b);(i)++) 
    #define FORP(i,a,b) for(int i=(a);i<=(b);i++) 
    #define FORM(i,a,b) for(int i=(a);i>=(b);i--) 
    #define ls(a,b) (((a)+(b)) << 1) 
    #define rs(a,b) (((a)+(b)) >> 1) 
    #define getlc(a) ch[(a)][0] 
    #define getrc(a) ch[(a)][1] 
     
    #define maxn 500
    #define maxm 100000 
    #define pi 3.1415926535898 
    #define _e 2.718281828459 
    #define INF 1070000000 
    using namespace std; 
    typedef long long ll; 
    typedef unsigned long long ull; 
     
    template<class T> inline 
    void read(T& num) { 
        bool start=false,neg=false; 
        char c; 
        num=0; 
        while((c=getchar())!=EOF) { 
            if(c=='-') start=neg=true; 
            else if(c>='0' && c<='9') { 
                start=true; 
                num=num*10+c-'0'; 
            } else if(start) break; 
        } 
        if(neg) num=-num; 
    } 
    /*==================split line==================*/ 
    char A[maxn],C[maxn];
    int dp[maxn][maxn],ans[maxn];
    int main(){ 
    	freopen("a.in","r",stdin);
    	while (scanf("%s",C)!=EOF){ 
    		 scanf("%s",A);
    		//FORP(i,0,maxn) B[i]='';
    		int n=strlen(A);
    		memset(dp,0,sizeof(dp));
    		FORP(i,0,n-1) 
    			FORP(j,i,n-1) dp[i][j]=INF;
    		FORP(i,0,n-1) dp[i][i]=1;
    		 FORP(l,1,n-1)
    		 	FORP(i,0,n-1-l) 
    			{
    				int j=i+l;
    				dp[i][j]=dp[i+1][j]+1;
    				FORP(k,i+1,j) 
    				if (A[k]==A[i]) dp[i][j]=min(dp[i][j],dp[i+1][k]+dp[k+1][j]);
    			}
    		//FORP(i,0,n-1)
    		//	FORP(j,i,n-1) printf("%d%c",dp[i][j],j==n-1?'
    ':' ');
    		memset(ans,0,sizeof(ans));
    		ans[0]=(A[0]!=C[0]);
    		FORP(i,1,n-1)
    			if (A[i]==C[i])	ans[i]=ans[i-1];
    			else {
    				ans[i]=dp[0][i];
    				FORP(j,0,i-1) ans[i]=min(ans[i],ans[j]+dp[j+1][i]);
    			}
    		printf("%d
    ",ans[n-1]);
    	}
    }
    
  • 相关阅读:
    Ajax
    对于本地上关联了远程的git上的项目,删除现有的关联,然后关联新创建的git项目
    UNI-APP_uni-simple-router的快速上手(路由,nui路由拦截)
    js之观察者模式和发布订阅模式区别
    Vue CLI 3.0脚手架如何在本地配置mock数据
    es6解构赋值和扩展运算符
    js Array数组详细操作方法及解析合集
    list排序
    java常用的object数据处理
    java 读写 txt 文件
  • 原文地址:https://www.cnblogs.com/YCuangWhen/p/5330760.html
Copyright © 2011-2022 走看看