大楼轮廓,值等于aim的最大子数组,异或数组(值未解决)

程序1:大楼轮廓

#include<iostream>  //大楼轮廓 ,比较器,vector排序,map找最后一个元素时出错,未解决
using namespace std;
#include<string>
#include<typeinfo>
#include<deque>
#include<algorithm>
#include<stack>
#include<map>

struct Node
{
    bool isUp;
    int position;
    int height;
    Node(){};
    Node(bool bORe,int posi,int h)
    {
        isUp = bORe;
        position = posi;
        height = h;          
    }
};

class compater
{
public:
    bool operator()(Node n1,Node n2)
    {
        if(n1.position!=n2.position)
        {
            return n1.position<n2.position;
        }
        if(n1.isUp!=n2.isUp)
        {
            return n1.isUp?-1:1;
        }
    }
};

vector<vector<int> > buildingOutline(vector<vector<int> > buildings)
{
    vector<Node> nodes;
    for(int i = 0;i<buildings.size();i++)
    {
        Node a;
        a = Node(true, buildings[i][0], buildings[i][2]);
        nodes.push_back(a);
        a = Node(false, buildings[i][1], buildings[i][2]);
        nodes.push_back(a);
    }
    sort(nodes.begin(),nodes.end(),compater());
    cout<<"position"<<" :";
    for(int i =0;i<nodes.size();i++)
    {
        cout<<nodes[i].position<<" "<<nodes[i].height<<" "<<nodes[i].isUp<<endl;
    }
    cout<<"end"<<endl;
    map<int,int> htMap;  //key是某一个高度,value是这个高度出现的次数
    map<int,int> pmMap;
    for(int i = 0;i<nodes.size();i++)
    {
        if(nodes[i].isUp)
        {
            map<int,int>::iterator pos = htMap.find(nodes[i].height);
            if(pos==htMap.end())
            {
                htMap.insert(pair<int,int>(nodes[i].height,1));
            }
            else
            {
                htMap.insert(pair<int,int>(nodes[i].height,pos->second+1));
            }
        }
        else
        {
            map<int,int>::iterator pos = htMap.find(nodes[i].height);
            if(pos!=htMap.end())
            {
                if(pos->second==1)
                {
                    htMap.erase(pos);
                }
                else
                {
                    htMap.insert(pair<int,int>(nodes[i].height,pos->second-1));
                }
            }
        }
        if(pmMap.empty())
        {
            pmMap.insert(pair<int,int>(nodes[i].position,0));
        }
        else
        {
            map<int,int>::reverse_iterator it = htMap.rbegin();
            pmMap.insert(pair<int,int>(nodes[i].position,it->first));     //出错,未完成
        }
    }
    vector<vector<int> > res;  //生成轮廓
    int start=0;
    int height=0;
    for(map<int,int>::iterator i=pmMap.begin();i != pmMap.end();i++) //pmMap的key是位置,如果遍历会按照key升序排列
    {
        int curPosition = i->first;
        int curMaxHeight = i->second;
        if(height!=curMaxHeight)
        {
            if(height!=0)
            {
                vector<int> newRecord;
                newRecord.push_back(start);
                newRecord.push_back(curPosition);
                newRecord.push_back(height);
                res.push_back(newRecord);
            }
            start = curPosition;
            height = curMaxHeight;
        }
    }
    return res;
}

int main()
{
    vector<vector<int> >tmp;
    vector<int> a;
    a.push_back(1);
    a.push_back(6);
    a.push_back(4);
    tmp.push_back(a);
    vector<int> b;
    b.push_back(2);
    b.push_back(4);
    b.push_back(3);
    tmp.push_back(b);
    vector<int> c;
    c.push_back(5);
    c.push_back(8);
    c.push_back(5);
    tmp.push_back(c);
    vector<int> d;
    d.push_back(7);
    d.push_back(10);
    d.push_back(3);
    tmp.push_back(d);
    vector<vector<int> >res = buildingOutline(tmp);
    // for(int i=0;i<res.size();i++)
    // {
    //     cout<<res[i][0]<<" "<<res[i][1]<<" "<<res[i][2]<<endl;
    // }
    return 0;
}

程序2:值等于aim的最大子数组,异或数组(未输出正确值)

#include<iostream>  //值等于aim的最大子数组,异或数组
using namespace std;
#include<string>
#include<deque>
#include<algorithm>
#include<stack>
#include<map>
#include<vector>
#include<unordered_map>

int maxLength(vector<int> arr,int aim)
{
    if(arr.size()==0)
    {
        return 0;
    }
    unordered_map<int,int> m;
    m.insert(pair<int,int>(-1,0));
    int len = 0;
    int sum = 0;
    for(int i=0;i<arr.size();i++)
    {
        sum+=arr[i];
        if(m.find(sum-aim)!=m.end())
        {
            len = max(len,i-m.find(sum-aim)->second);
        }
        if(m.find(sum)==m.end())
        {
            m.insert(pair<int,int>(sum,i));
        }
    }
    return len;
}

int mostEOR(vector<int>arr)
{
    int ans = 0;
    int x_or = 0;
    int len = arr.size();
    int dp[len] = {0};
    unordered_map<int,int>m2;
    m2.insert(pair<int,int>(0,-1));
    for(int i =0;i<arr.size();i++)
    {
        x_or^=arr[i];
        if(m2.find(x_or)!=m2.end())
        {
            int pre = m2.find(x_or)->second;
            dp[i] = pre ==-1?1:(dp[pre]+1);
        }
        if(i>0)
        {
            dp[i]=max(dp[i-1],dp[i]); 
        }
        m2.insert(pair<int,int>(x_or,i));
        ans = max(ans,dp[i]);
    }
    return ans;
}

int main()
{
    vector<int>a;
    a.push_back(7);
    a.push_back(3);
    a.push_back(2);
    a.push_back(1);
    a.push_back(1);
    a.push_back(7);
    a.push_back(-6);
    a.push_back(-1);
    a.push_back(7);
    // cout<<maxLength(a,7);

    vector<int>b;

    b.push_back(3);
    b.push_back(2);
    b.push_back(1);
    b.push_back(0);
    b.push_back(1);
    b.push_back(2);
    b.push_back(3);
    b.push_back(0);
    cout<<mostEOR(b);
    return 0;
}
全部评论

相关推荐

11-15 17:19
湖南大学 Java
成果成果成果果:这是哪个公司的hr,这么离谱吗,我没见过用性别卡技术岗的,身边女性同学拿大厂offer的比比皆是
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务