zoukankan      html  css  js  c++  java
  • UVa 497

    题目:最大上升子序列。输出一组解。

    分析:dp,LIS。数据较小 O(n^2)算法就可以。

               设以第i个数字作为最大上升子序列中的最后一个数的长度为 f(i),则有转移方程:

                f(i)= max(f(j)) { 0=< j < i  && data[j] < data[i] };

               用一个数组记录前驱,递归输出就可以。

    说明:注意输出格式有点纠结。

    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    #include <cstdio>
    
    using namespace std;
    
    char buf[256];
    int  data[10000];
    int  dp[10000];
    int  front[10000];
    
    void output( int d, int s )
    {
    	if ( front[s] >= s )
    		printf("Max hits: %d
    ",d+1);
    	else
    		output( d+1, front[s] );
    	printf("%d
    ",data[s]);
    }
    
    int main()
    {
    	int n;
    	while (~scanf("%d",&n)) {
    		getchar();
    		getchar();
    		while ( n -- ) {
    			char ch;
    			int count = 0;
    			while (gets(buf) && buf[0])
    				data[count ++] = atoi(buf);
    			for ( int i = 0 ; i < count ; ++ i ) {
    				dp[i] = 1;
    				front[i] = i;
    				for ( int j = 0 ; j < i ; ++ j )
    					if ( data[i] > data[j] && dp[i] < dp[j]+1 ) {
    						dp[i] = dp[j]+1;
    						front[i] = j;
    					}
    			}
    			
    			int max = 0;
    			for ( int i = 1 ; i < count ; ++ i )
    				if ( dp[i] > dp[max] )
    					max = i;
    			
    			if ( count )
    				output( 0, max );
    			else printf("Max hits: 0
    ");
    			if ( n ) printf("
    ");
    		}
    	}
    	return 0;
    }
    

  • 相关阅读:
    PHP基础介绍
    day96
    day95
    day94
    day93
    day93之微信推送
    22个必须知道的css技巧
    利用Js或Css滤镜实现IE6中PNG图片半透明效果 IE6PNG妥妥的
    dedecms调用日期格式化形式大全
    innerHTML动态添加html代码和脚本兼容性问题处理方法
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/5314691.html
Copyright © 2011-2022 走看看