1097. Deduplication on a Linked List (25)

题目链接:http://www.patest.cn/contests/pat-a-practise/1097
题目:

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

分析:
维护五个变量:原串首地址,原串当前地址,重复串首地址,重复串当前地址,还有接下来要判断的地址,则会清晰简单很多。
AC代码:
#include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<stack>
#include<string>
#include<string.h>
#include<map>
#include<cmath>
#include<set>
using namespace std;
struct Node{
 int value;
 int next;
}buf[100001];
int main(){
 freopen("F://Temp/input.txt", "r", stdin);
 int firstAdd, num;
 cin >> firstAdd >> num;
 for (int i = 0; i < num; ++i){
  int iAdd;
  cin >> iAdd;
  cin >> buf[iAdd].value >> buf[iAdd].next;
 }
 set<int>S;
 int curOriginalAdd = firstAdd;//记录原数字串的当前地址
 int firstDuplicaAdd = -2;//记录重复数字串的首地址
 int curDuplicaAdd = -2;//记录重复数字串的当前地址
 S.insert(buf[firstAdd].value);
 int add = firstAdd;
 for (add = buf[firstAdd].next; add != -1; add = buf[add].next){
  int curValue = buf[add].value;
  if (S.find(curValue) == S.end() && S.find(-curValue) == S.end()){//当不是重复数字时
   S.insert(curValue);//当前数字加入S
   buf[curOriginalAdd].next = add;//加入原数字串
   curOriginalAdd = add;//更新原数字串地址
  }
  else{
   if (-2 == firstDuplicaAdd){
    firstDuplicaAdd = add;//当作重复数字串的首地址
    curDuplicaAdd = add;//更新重复数字串地址
   }
   else{
    buf[curDuplicaAdd].next = add;//加入重复数字串
    curDuplicaAdd = add;//更新重复数字串地址
   }
  }
 }
 buf[curOriginalAdd].next = -1;//加入末尾指针
 buf[curDuplicaAdd].next = -1;//加入末尾指针
 for (int add1 = firstAdd; add1 != -1; add1 = buf[add1].next){
  if (-1 == buf[add1].next)
   printf("%05d %d %d\n", add1, buf[add1].value, buf[add1].next);
  else
   printf("%05d %d %05d\n", add1, buf[add1].value, buf[add1].next);
 }
 if (-2 == firstDuplicaAdd)return 0; //注意没有重复数字的情况,则不输出重复数字串
 for (int add2 = firstDuplicaAdd; add2 != -1; add2 = buf[add2].next){
  if (-1 == buf[add2].next)
   printf("%05d %d %d\n", add2, buf[add2].value, buf[add2].next);
  else
   printf("%05d %d %05d\n", add2, buf[add2].value, buf[add2].next);
 }
 return 0;
}


截图:
1097. Deduplication on a Linked List (25)_第1张图片
——Apie陈小旭

你可能感兴趣的:(1097. Deduplication on a Linked List (25))