zoukankan      html  css  js  c++  java
  • A1097 Deduplication on a Linked List [链表去重]

    在这里插入图片描述

    #include<vector>
    #include<iostream>
    #include<algorithm>
    #include<unordered_map>
    #include<set>
    #include<map>
    #include<cstring>
    #include<string>
    #include<queue>
    #include<array>
    #include<stack>
    using namespace std;
    const int maxn = 100010;
    const int TABLE = 10001;
    struct Node
    {
    	int address,data,next;
    	bool flag;
    	int order;
    }node[maxn];
    bool cmp(Node a, Node b)
    {
    	return a.order < b.order;
    }
    bool isExist[TABLE];
    int main()
    {
    	memset(isExist, false, sizeof(isExist));
    	for (int i = 0; i < maxn; i++)
    	{
    		node[i].order = 2 * maxn;
    	}
    	int n, begin, address;
    	cin >> begin >> n;
    	for (int i = 0; i < n; i++)
    	{
    		cin >> address;
    		cin >> node[address].data >> node[address].next;
    		node[address].address = address;
    	}
    	int countvalid = 0, countRemoved = 0, p = begin;
    	while (p != -1)
    	{
    		if (!isExist[abs(node[p].data)]) {
    			isExist[abs(node[p].data)] = true;
    			node[p].order = countvalid++;
    		}
    		else
    		{
    			node[p].order = maxn + countRemoved++;
    		}
    		p = node[p].next;
    	}
    	sort(node, node + maxn, cmp);
    	int count = countvalid + countRemoved;
    	for (int i = 0; i < count; i++)
    	{
    		if (i != countvalid - 1 && i != count - 1)
    		{
    			printf("%05d %d %05d
    ", node[i].address, node[i].data,node[i+1].address);
    		}
    		else
    		{
    			printf("%05d %d -1
    ", node[i].address, node[i].data);
    		}
    	}
    	return 0;
    }
    
  • 相关阅读:
    算法提高 道路和航路
    奇偶剪枝
    二分求值
    并查集--路径压缩
    Oracle数据库导入导出DMP文件
    Spring IoC的实现与思考(一)
    sql基础拾遗
    jquery事件函数的使用之focus
    Java动态代理之cglib
    Java se之动态代理
  • 原文地址:https://www.cnblogs.com/Hsiung123/p/13812025.html
Copyright © 2011-2022 走看看