PAT1097 Deduplication on a Linked List
来源:互联网 发布:linux chmod命令 编辑:程序博客网 时间:2024/05/05 18:47
题目链接:
http://www.nowcoder.com/pat/5/problem/4321
题目大意:
要求找出一个线性表中的结点数据绝对值重复的结点,仅保留第一个,其余的按地址连接的顺序去除,并且连接成另一个线性表。
分析:
水题,用数组下标模拟地址。遍历去重时,利用set不能放重复元素的特性进行判断,如果不能插入结点数据,证明是重复的,模拟链表删除一个结点。
代码:
#define _CRT_SECURE_NO_WARNINGS#include<iostream>#include<stdio.h>#include<stdlib.h>#include<string.h>#include<set>using namespace std;struct Node{ int _val; int _nextAdr;};Node list[100005];struct Mark{ int _curAdr; int _val; int _nextAdr;};Mark delNode[100005];int main(){ int staAdr; int n; set<int> s; scanf("%d%d",&staAdr,&n); int adr; while (n--) { scanf("%d",&adr); scanf("%d%d",&list[adr]._val,&list[adr]._nextAdr); } int curAdr = staAdr; int val; int size; int preAdr = 0; int cnt = 0; while (curAdr != -1) { val = list[curAdr]._val; val = val > 0 ? val : -val; size = s.size(); s.insert(val); if (size == s.size()) { list[preAdr]._nextAdr = list[curAdr]._nextAdr; delNode[cnt]._curAdr = curAdr; delNode[cnt]._val = list[curAdr]._val; delNode[cnt]._nextAdr = -1; if (cnt > 0) { delNode[cnt - 1]._nextAdr = curAdr; } cnt++; } else { preAdr = curAdr; } curAdr = list[curAdr]._nextAdr; } curAdr = staAdr; while (list[curAdr]._nextAdr != -1) { printf("%05d %d %05d\n", curAdr, list[curAdr]._val, list[curAdr]._nextAdr); curAdr = list[curAdr]._nextAdr; } //curAdr = list[curAdr]._nextAdr; printf("%05d %d -1\n", curAdr, list[curAdr]._val); for (int i = 0; i < cnt; i++) { printf("%05d %d",delNode[i]._curAdr,delNode[i]._val); if (delNode[i]._nextAdr == -1) { printf(" -1\n"); } else { printf(" %05d\n",delNode[i]._nextAdr); } } return 0;}
0 0
- PAT1097 Deduplication on a Linked List
- PAT 1097 Deduplication on a Linked List
- 1097.Deduplication on a Linked List
- 1097. Deduplication on a Linked List (25)
- 1097. Deduplication on a Linked List (25)
- 1097. Deduplication on a Linked List (25)
- 1097. Deduplication on a Linked List (25)
- 1097. Deduplication on a Linked List (25)
- A1097. Deduplication on a Linked List (25)
- 1097. Deduplication on a Linked List
- 1097. Deduplication on a Linked List (25)
- pat 1097 Deduplication on a Linked List
- 1097. Deduplication on a Linked List
- 1097. Deduplication on a Linked List (25)
- 1097. Deduplication on a Linked List (25)
- 1097. Deduplication on a Linked List (25)
- 1097. Deduplication on a Linked List (25)
- 1097 Deduplication on a Linked List
- linux文件按最新时间排序
- 黑马程序员——集合知识概括(下)
- 泛型
- 字符串处理
- Runtime
- PAT1097 Deduplication on a Linked List
- Android任务和返回栈完全解析,细数那些你所不知道的细节
- Leetcode168: House Robber II
- 001 Two Sum
- java中堆栈和队列的实现方式
- NSnotification通知
- eclipse连接oracle11g教程(win7系统)
- python基础_Scrapy爬虫基础学习一
- 抛弃PHP,WordPress.com以JavaScript重写