zoukankan      html  css  js  c++  java
  • 雅虎书面问题——最大的问题汇总

    总结:sort功能在日常编程,经常使用;同时。内存初始化函数,常常被用来fill,在本节。我们计算LIS作为一个例子来解释这两个函数的应用。

    样例:有一些老鼠,有x和y两个属性,如今要选择一个最大集合。当中从前到后,老鼠的x属性依次递减,y属性依次递增。


    分析:假设我们先依照x属性递减排列,那么形成一个数组,然后问题就转化为求解这个数组中y属性递增排列的最长字串。于是转化为一个动态规划问题。

    关键点2:在动态规划的解法中。我们只记录了最长的集合长度,那么怎样将集合恢复出来?这里,我们用到了“前缀树”的思想:在非常多搜索结构中,我们往往须要搜索一种路径。然而,从某个点開始的路径可能有非常多条。可是每一个点的前缀节点唯独一个。在这样的情况下,我们只须要记录某个节点的前缀节点,然后就能恢复出来这条路径。


    写这道题目的时候。犯下的错误:

    sort函数的递减排列;

    memset函数,開始使用这个函数对int类型进行了非零的初始化。比方memset(len, 1, 12*4);想想这里为什么出错?这个函数的定义式子是什么样的?


    详细的算法例如以下:

    #include <iostream>
    #include <iomanip>
    #include <vector>
    #include<cstring>
    #include <string>
    #include <algorithm>
    #include <cstdlib>
    #include <cmath>
    #include <stack>
    #include <climits>
    
    using namespace std;
    struct mice{
    	int x;
    	int y;
    	mice(){}
    	mice(int x1, int y1):x(x1), y(y1){}
    };
    
    struct mycom{
    	bool operator()(mice a, mice b)const{
    		return (b.x<a.x);
    	}
    }Com;
    
    int main(int argc, char *argv[])
    {
    	mice my[12];
    	int i, res=-1, resindex=-1;
    	for (i = 0; i < 12; ++i){
    		my[i]=mice(rand()%100, rand()%100);
    	}
    	sort(my, my+12, Com);
    	
    	int len[12];	
    	fill(len, len+12, 1);
    
    	int pre[12];
    	memset(pre, -1, 12*4);
    	fill(pre, pre+12, -1);
    
    	for (int i = 1; i < 12; ++i){
    		for(int k=0; k<i;k++){
    			if(my[i].y > my[k].y && len[k]+1 > len[i]){
    				len[i]=len[k]+1;
    				pre[i]=k;	
    			}	
    		}
    		if(len[i]>res){
    			res= len[i];
    			resindex = i;
    		}
    	}
    	stack<int> s;
    	int curindex=resindex;
    	while(curindex!=-1){
    		s.push(curindex);	
    		curindex = pre[curindex];
    	}
    	cout << res <<endl;
    	while(s.empty()==false){
    		int index= s.top();
    		s.pop();
    		cout << my[index].x << " " <<my[index].y <<endl;
    	}
    	return 0;
    }


  • 相关阅读:
    弹性盒布局(Flexbox布局)
    CSS子元素在父元素中水平垂直居中的几种方法
    Vue中watch用法详解
    深入理解vue中的slot与slot-scope
    Spring 源码学习 03:创建 IoC 容器的几种方式
    Spring 源码学习 02:关于 Spring IoC 和 Bean 的概念
    Spring 源码阅读环境的搭建
    DocView 现在支持自定义 Markdown 模版了!
    Dubbo 接口,导出 Markdown ,这些功能 DocView 现在都有了!
    线程池 ThreadPoolExecutor 原理及源码笔记
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/4600999.html
Copyright © 2011-2022 走看看