日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

1097 Deduplication on a Linked List (25 分)_35行代码AC

發布時間:2024/2/28 54 豆豆
生活随笔 收集整理的這篇文章主要介紹了 1097 Deduplication on a Linked List (25 分)_35行代码AC 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

立志用最少的代碼做最高效的表達


PAT甲級最優題解——>傳送門


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 (≤10^?5) 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 10^4, 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<bits/stdc++.h> using namespace std; int main() {int fir, n;//1、構建鏈表 vector<int>add(100010), val(100010);cin >> fir >> n;for(int i = 0; i < n; i++) {int address; cin >> address;cin >> val[address] >> add[address];}//2、篩出無效節點,將鏈表存入v vector<int>v;unordered_map<int, int>um;while(fir != -1) {v.push_back(fir);fir = add[fir];}//3、遍歷,不重復的入v1,重復的入v2 vector<int>v1, v2;for(auto i : v) {if(um[abs(val[i])] == 0) {v1.push_back(i);} else v2.push_back(i);um[abs(val[i])]++;} //若為非空,則輸出。 if(!v1.empty()) {for(int i = 0; i < v1.size()-1; i++) printf("%05d %d %05d\n", v1[i], val[v1[i]], v1[i+1]);printf("%05d %d -1\n", v1[v1.size()-1], val[v1[v1.size()-1]]);}if(!v2.empty()) {for(int i = 0; i < v2.size()-1; i++) printf("%05d %d %05d\n", v2[i], val[v2[i]], v2[i+1]);printf("%05d %d -1\n", v2[v2.size()-1], val[v2[v2.size()-1]]);}return 0; }

耗時:

總結

以上是生活随笔為你收集整理的1097 Deduplication on a Linked List (25 分)_35行代码AC的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。