L2-002. 链表去重(用两个队列来储存两个链表)
2018-03-28 18:50
274 查看
题目链接:https://www.patest.cn/contests/gplt/L2-002
思路: 用两个队列来储存两个链表,注意格式输出就好,还要注意不存在 被删除链表的情况
代码:
思路: 用两个队列来储存两个链表,注意格式输出就好,还要注意不存在 被删除链表的情况
代码:
#include <iostream> #include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> #include <algorithm> #include <queue> using namespace std; const int maxn =100000+5; struct Edge { int key,next; } e[maxn]; int flag[maxn]; int main() { typedef pair<int,int> p;//第一个值为地址,第二个为键值 queue<p>l,l1; int N,fir_add,address,x,flag1=0; memset(flag,0,sizeof(flag)); scanf("%d %d",&fir_add,&N); for(int i=0; i <N; i++) { scanf("%d",&address); scanf("%d %d",&e[address].key,&e[address].next); } while(fir_add!=-1) { x=abs(e[fir_add].key); if(flag[x]==0) { l.push((p) { fir_add,e[fir_add].key }); flag[x]=1; } else { l1.push((p) { fir_add,e[fir_add].key }); } fir_add=e[fir_add].next; } p p1; p1=l.front(); l.pop(); printf("%05d %d ",p1.first,p1.second); while(!l.empty()) { p1=l.front(); l.pop(); printf("%05d\n",p1.first); printf("%05d %d ",p1.first,p1.second); } printf("-1\n"); if(!l1.empty()) { flag1=1; p1=l1.front(); l1.pop(); printf("%05d %d ",p1.first,p1.second); } while(!l1.empty()) { p1=l1.front(); l1.pop(); printf("%05d\n",p1.first); printf("%05d %d ",p1.first,p1.second); } if(flag1) printf("-1\n"); return 0; }
相关文章推荐
- L2-002. 链表去重-PAT团体程序设计天梯赛GPLT
- 团体程序设计天梯赛-练习集 L2-002. 链表去重 解题报告
- L2-002. 链表去重
- 团体程序设计天梯赛-练习集L2-002. 链表去重
- L2-002. 链表去重
- L2-002. 链表去重
- L2-002. 链表去重(模拟)
- L2-002.链表去重
- L2-002. 链表去重
- PAT L2-002 链表去重
- L2-002. 链表去重
- L2-002. 链表去重
- L2-002 链表去重
- java数据结构之LinkedDeque(用链表实现的双端(即头尾两个哨兵节点)双向(node里两个指向)队列)
- 天梯- L2-002. 链表去重
- 天梯赛 - L2-002 链表去重
- PAT L2-002. 链表去重 巧妙利用数组的索引
- L2-002. 链表去重
- L2-002. 链表去重 (模拟+空间换时间)
- PAT L2-002. 链表去重 巧妙利用数组的索引