天天看點

PAT甲級1097

1097. Deduplication on a Linked List (25)

時間限制

300 ms

記憶體限制

65536 kB

代碼長度限制

16000 B

判題程式

Standard

作者

CHEN, Yue

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 (<= 105) 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 104, 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

#include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int MAXN = 100000;
struct Node
{
  int address, data,next;
  Node():address(-1),data(100001),next(-1){}
}node[MAXN];
int hashtable[1000010];
int main()
{
  int N, start;
  fill(hashtable, hashtable + 1000010, 0);
  scanf("%d%d", &start, &N);
  
  int address;
  for (int i = 0; i < N; i++)
  {
    scanf("%d", &address);
    node[address].address = address;
    scanf("%d %d", &node[address].data, &node[address].next);
  }
  if (start == -1 || node[start].data == 100001)
  {
    printf("-1\n-1");
    return 0;
  }
  int pre = start, p = start;
  vector<Node> v;
  //cout << v.size() - 1 << endl;
  while (p != -1)
  {
    if (!hashtable[abs(node[p].data)])
    {
      hashtable[abs(node[p].data)]++;
      pre = p;
      p = node[p].next;
    }
    else
    {
      v.push_back(node[p]);
      node[pre].next = node[p].next;
      p = node[pre].next;
    }
    
  }
  if (v.size())
  {
    for (int i = 0; i < v.size() - 1; i++)//由于v.size()傳回的是無符号整數,若V為空那麼減1不是負數!!!!
    {
      v[i].next = v[i + 1].address;
    }
    v[v.size() - 1].next = -1;
  }
  
  while (start != -1)
  {
    if(node[start].next!=-1)
    printf("%05d %d %05d\n", node[start].address, node[start].data, node[start].next);
    else
      printf("%05d %d %d\n", node[start].address, node[start].data, node[start].next);
    start = node[start].next;
  }
  for (int i = 0; i < v.size(); i++)
  {
    if (v[i].next!=-1)
      printf("%05d %d %05d\n", v[i].address, v[i].data, v[i].next);
    else
      printf("%05d %d %d\n", v[i].address, v[i].data, v[i].next);

  }
  return 0;
}      
上一篇: PAT甲級1102
下一篇: PAT甲級1052

繼續閱讀