日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

PAT1097:Deduplication on a Linked List

發布時間:2025/3/15 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PAT1097:Deduplication on a Linked List 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1097. Deduplication on a Linked List (25)

時間限制 300 ms 內存限制 65536 kB 代碼長度限制 16000 B 判題程序 Standard 作者 CHEN, Yue

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

思路
1.map模擬一個鏈表dic存放輸入的所有的節點,set模擬一個dupdic記錄一個節點的值來判斷接下來的節點是否由重復的key值,兩個vector分別為去重鏈表和重復節點鏈表。
2.遍歷map,根據set容器內的key值決定是將當前節點插入去重鏈表還是重復節點鏈表,并不斷更新set存放的key值。
3.對兩個vector新鏈表的節點的next地址賦予它下個節點的地址。
4.遍歷兩個鏈表并依次輸出即可,
代碼 #include<iostream> #include<vector> #include<map> #include<set> #include<math.h> using namespace std; class node { public:int address;int key;int next; };map<int,node> dic; set<int> dupdic; vector<node> newlist; vector<node> removed_list;int main() {int head,n;while(cin >> head >> n){//inputfor(int i = 0;i < n;i++){int adr;cin >> adr;dic[adr].address = adr;cin >> dic[adr].key >> dic[adr].next;}//handlewhile(head != -1){if(dupdic.find(abs(dic[head].key)) != dupdic.end()){removed_list.push_back(dic[head]);}else{newlist.push_back(dic[head]);dupdic.insert(abs(dic[head].key));}head = dic[head].next;}int lenn = newlist.size(),lenr = removed_list.size();//outputfor(int i = 1;i <= lenn;i++){if(i == lenn){newlist[i - 1].next = -1;printf("%05d %d %d\n",newlist[i - 1].address,newlist[i - 1].key,newlist[i - 1].next);}else{newlist[i - 1].next = newlist[i].address;printf("%05d %d %05d\n",newlist[i - 1].address,newlist[i - 1].key,newlist[i - 1].next);}}for(int i = 1;i <= lenr;i++){if(i == lenr){removed_list[i - 1].next = -1;printf("%05d %d %d\n",removed_list[i - 1].address,removed_list[i - 1].key,removed_list[i - 1].next);}else{removed_list[i - 1].next = removed_list[i].address;printf("%05d %d %05d\n",removed_list[i - 1].address,removed_list[i - 1].key,removed_list[i - 1].next);}}} }

  

轉載于:https://www.cnblogs.com/0kk470/p/8143150.html

總結

以上是生活随笔為你收集整理的PAT1097:Deduplication on a Linked List的全部內容,希望文章能夠幫你解決所遇到的問題。

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