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;
}


截图:

——Apie陈小旭
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值