zoukankan      html  css  js  c++  java
  • PAT (Advanced Level) Practice 1097 Deduplication on a Linked List (25分) (静态链表+测试实例)

    1.题目

    Given a singly linked list L with integer keys, you are supposed to remove the nodes with duplicated absolute values of the keys. That is, for each value K, only the first node of which the value or absolute value of its key equals K will be kept. At the mean time, all the removed nodes must be kept in a separate list. For example, given L being 21→-15→-15→-7→15, you must output 21→-15→-7, and the removed list -15→15.

    Input Specification:

    Each input file contains one test case. For each case, the first line contains the address of the first node, and a positive N (≤10​5​​) which is the total number of nodes. The address of a node is a 5-digit nonnegative integer, and NULL is represented by −1.

    Then N lines follow, each describes a node in the format:

    Address Key Next
    

    where Address is the position of the node, Key is an integer of which absolute value is no more than 10​4​​, and Next is the position of the next node.

    Output Specification:

    For each case, output the resulting linked list first, then the removed list. Each node occupies a line, and is printed in the same format as in the input.

    Sample Input:

    00100 5
    99999 -7 87654
    23854 -15 00000
    87654 15 -1
    00000 -15 99999
    00100 21 23854
    

    Sample Output:

    00100 21 23854
    23854 -15 99999
    99999 -7 -1
    00000 -15 87654
    87654 15 -1

    2.题目分析 

    1.使用数组标记,第一次出现的放在ori中,重复的放在dedup中,并同时更新ori数组中最后一个元素的next

    注意:5位address以及next;全重复、无重复

    3.代码

    #include<iostream>
    #include<vector>
    #include<cmath>
    using namespace std;
    struct node
    {
    	int address;
    	int data;
    	int next;
    };
    int mark[100010];
    int main()
    {
    	int headadd, n,a;
    	scanf("%d %d", &headadd, &n);
    	vector<node>list(100010);
    	vector<node>ori;
    	vector<node>dedup;
    	for (int i = 0; i < n; i++)
    	{
    		scanf("%d", &a);
    		scanf("%d %d", &list[a].data, &list[a].next);
    		list[a].address = a;
    	}
    	int temp = headadd;
    	while (temp != -1)
    	{
    		if (mark[abs(list[temp].data)] == 0) { ori.push_back(list[temp]); mark[abs(list[temp].data)] = 1; }
    		else
    		{
    			ori.back().next = list[temp].next;
    			dedup.push_back(list[temp]);
    		}
    		temp = list[temp].next;
    	}
    	if (ori.size() > 1)
    	{
    		printf("%05d %d %05d
    ", ori[0].address, ori[0].data, ori[0].next);
    		int i;
    		for ( i = 1; i < ori.size()-1; i++)
    		{
    			printf("%05d %d %05d
    ", ori[i].address, ori[i].data, ori[i].next);
    		}
    		printf("%05d %d -1
    ", ori[i].address, ori[i].data);
    	}
    	else
    		printf("%05d %d -1
    ", ori[0].address, ori[0].data);
    	if (dedup.size() > 1)
    	  {
    		int i;
    		printf("%05d %d %05d
    ", dedup[0].address, dedup[0].data, dedup[1].address);
    		for ( i = 1; i < dedup.size()-1; i++)
    		{
    			printf("%05d %d %05d
    ", dedup[i].address, dedup[i].data, dedup[i+1].address);
    		}
    		printf("%05d %d -1
    ", dedup[i].address, dedup[i].data);
    	}
    	else if(dedup.size()==1)
    		printf("%05d %d -1", dedup[0].address, dedup[0].data);
    
    }
    
    // 00100 4
    // 99999 -7 -1
    // 23854 -15 00000
    // 00000 -15 99999
    // 00100 21 23854
    
    // 00100 5
    // 00100 15 23854
    // 23854 -15 00000
    // 00000 -15 99999
    // 99999 -15 87654
    // 87654 15 -1
    
    
    // 00100 5
    // 99999 -7 87654
    // 23854 -16 00000
    // 87654 15 -1
    // 00000 -17 99999
    // 00100 21 23854
    
  • 相关阅读:
    学习笔记26_MVC前台强类型参数
    Elasticsearch 和 solr 的区别
    Mysq索引优化(什么情况创建索引,什么情况不创建索引)
    Spring支持的常用数据库传播属性和事务隔离级别
    方法的参数传递机制
    JAVA类初始化和实例初始化
    JAVA设计模式
    CentOS 7 下安装 Nginx 服务,并开机自启动
    JAVA发送xml格式的接口请求
    JAVA接收postman的中raw的参数
  • 原文地址:https://www.cnblogs.com/Jason66661010/p/12788842.html
Copyright © 2011-2022 走看看