PAT 1097 Deduplication on a Linked List
来源:互联网 发布:身份证借人开淘宝店 编辑:程序博客网 时间:2024/06/06 10:39
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 599999 -7 8765423854 -15 0000087654 15 -100000 -15 9999900100 21 23854Sample Output:
00100 21 2385423854 -15 9999999999 -7 -100000 -15 8765487654 15 -1
#include <vector>
#include <unordered_map>
#include <string>
#include <algorithm>
#include <stdio.h>
#define MAXN 100000
struct LinkNode {
std::string address;
std::string next_address;
int vaule;
LinkNode* next;
bool is_redundant;
};
void PrintLinkedList(LinkNode* h){
printf("Print LinkedList:");
while (h) {
printf("%d ", h->vaule);
}
printf("\n");
}
int main() {
std::unordered_map<std::string, LinkNode*> node_map;
std::unordered_map<int, std::string> vaule_map;
std::string head_address;
int n;
std::cin >> head_address;
std::cin >> n;
LinkNode* nodes[MAXN];
LinkNode* head_node;
for (int i = 0; i < n; i++) {
std::string address;
int vaule;
std::string next_address;
std::cin >> address;
std::cin >> vaule;
std::cin >> next_address;
nodes[i] = new LinkNode;
nodes[i]->vaule = vaule;
nodes[i]->address = address;
nodes[i]->next_address = next_address;
nodes[i]->is_redundant = false;
nodes[i]->next = NULL;
if (address == head_address) {
head_node = nodes[i];
}
node_map[address] = nodes[i];
}
LinkNode* p = head_node;
while (p) {
p->next = node_map[p->next_address];
p = p->next;
}
LinkNode* temp = head_node;
while (temp) {
if (vaule_map[abs(temp->vaule)] == "") { // new
vaule_map[abs(temp->vaule)] = temp->address;
} else { // existed
temp->is_redundant = true;
}
temp = temp->next;
}
temp = head_node;
LinkNode* prior;
LinkNode* removed_head = (LinkNode*)malloc(sizeof(LinkNode));
removed_head->next = NULL;
LinkNode* temp_removed = removed_head;
LinkNode* mid;
while (temp) {
mid = temp->next;
if (temp->is_redundant) {
prior->next = mid;
temp_removed->next = temp;
temp_removed = temp;
temp_removed->next = NULL;
} else {
prior = temp;
}
temp = mid;
}
while (head_node) {
printf("%s %d %s\n", head_node->address.c_str(), head_node->vaule, head_node->next==NULL?"-1":head_node->next->address.c_str());
head_node = head_node->next;
}
temp = removed_head->next;
while (temp) {
printf("%s %d %s\n", temp->address.c_str(), temp->vaule, temp->next == NULL ? "-1" : temp->next->address.c_str());
temp = temp->next;
}
for (int i = 0; i < n; i++) {
delete nodes[i];
}
system("pause");
}
- PAT 1097 Deduplication on a Linked List
- pat 1097 Deduplication on a Linked List
- PAT 1097 Deduplication on a Linked List
- PAT-Deduplication on a Linked List (25)
- PAT--1097. Deduplication on a Linked List
- 【PAT】1097. Deduplication on a Linked List
- PAT-Deduplication on a Linked List
- PAT 1097 Deduplication on a Linked List (25)
- PAT 1097-Deduplication on a Linked List (25)
- 1097 Deduplication on a Linked List
- PAT A 1097. Deduplication on a Linked List (25)
- PAT-A-1097. Deduplication on a Linked List (25)
- PAT A 1097. Deduplication on a Linked List (25)
- PAT 1097. Deduplication on a Linked List (25)
- [PAT]1097. Deduplication on a Linked List (25)
- 【PAT】1097. Deduplication on a Linked List (25)
- PAT 1097. Deduplication on a Linked List (25)
- PAT甲级练习题A1097. Deduplication on a Linked List (25)
- uv纹理坐标设定与贴图规则
- “工业大数据”之“华山论剑”——也谈工业大数据分析
- RabbitMQ的应用场景以及基本原理介绍
- 关于Facebook的开源项目Keyframes
- directx初第九卷
- PAT 1097 Deduplication on a Linked List
- auto,register,static,const,volatile的区别
- Oracle 11g RAC常用检查操作指令实验
- gtk网址
- Python学习VII --- 异常处理
- linux下maven配置
- 【matlab】进度条嵌入主GUI界面
- 小鑫去爬山
- Spark商业案例与性能调优实战100课》第20课:大数据性能调优的本质和Spark性能调优要点分析