1097. Deduplication on a Linked List
2017-02-21 20:05
253 查看
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:
Sample Output:
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <deque>
#include <queue>
#include <cstring>
#include <vector>
#include <string>
#include <iomanip>
#include <map>
#include <set>
#include <cmath>
#include <stack>
#include <cmath>
#include <algorithm>
using namespace std;
#define max1 100001
#define inf -1
struct node
{
int address;
int data;
int nextaddress;
};
int main()
{
int i,n,firstaddress;
cin>>firstaddress>>n;
node s
;
for(i=0;i<n;i++)
{
cin>>s[i].address>>s[i].data>>s[i].nextaddress;
}
map<int,int>g;
vector<node>s1,s2;
int first=firstaddress,d;
while(first!=-1)
{
for(i=0;i<n;i++)
{
if(first==s[i].address)
{
if(s[i].data<0)
{
d=-s[i].data;
}
else
{
d=s[i].data;
}
if(g[d]!=1)
{
g[d]=1;
s1.push_back(s[i]);
}
else
{
s2.push_back(s[i]);
}
first=s[i].nextaddress;
}
}
}
for(i=0;i<s1.size()-1;i++)
{
printf("%05d %d %05d\n",s1[i].address,s1[i].data,s1[i+1].address);
}
printf("%05d %d -1\n",s1[i].address,s1[i].data);
if(s2.size()!=0)
{
for(i=0;i<s2.size()-1;i++)
{
printf("%05d %d %05d\n",s2[i].address,s2[i].data,s2[i+1].address);
}
printf("%05d %d -1\n",s2[i].address,s2[i].data);
}
return 0;
}
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
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <deque>
#include <queue>
#include <cstring>
#include <vector>
#include <string>
#include <iomanip>
#include <map>
#include <set>
#include <cmath>
#include <stack>
#include <cmath>
#include <algorithm>
using namespace std;
#define max1 100001
#define inf -1
struct node
{
int address;
int data;
int nextaddress;
};
int main()
{
int i,n,firstaddress;
cin>>firstaddress>>n;
node s
;
for(i=0;i<n;i++)
{
cin>>s[i].address>>s[i].data>>s[i].nextaddress;
}
map<int,int>g;
vector<node>s1,s2;
int first=firstaddress,d;
while(first!=-1)
{
for(i=0;i<n;i++)
{
if(first==s[i].address)
{
if(s[i].data<0)
{
d=-s[i].data;
}
else
{
d=s[i].data;
}
if(g[d]!=1)
{
g[d]=1;
s1.push_back(s[i]);
}
else
{
s2.push_back(s[i]);
}
first=s[i].nextaddress;
}
}
}
for(i=0;i<s1.size()-1;i++)
{
printf("%05d %d %05d\n",s1[i].address,s1[i].data,s1[i+1].address);
}
printf("%05d %d -1\n",s1[i].address,s1[i].data);
if(s2.size()!=0)
{
for(i=0;i<s2.size()-1;i++)
{
printf("%05d %d %05d\n",s2[i].address,s2[i].data,s2[i+1].address);
}
printf("%05d %d -1\n",s2[i].address,s2[i].data);
}
return 0;
}
相关文章推荐
- pat 1097 Deduplication on a Linked List
- 1097. Deduplication on a Linked List (25)
- 1097.Deduplication on a Linked List (25)
- PAT (Advanced Level) Practise - 1097. Deduplication on a Linked List (25)
- PAT甲级真题及训练集(18)--1097. Deduplication on a Linked List (25)
- PAT 1097 Deduplication on a Linked List
- 1097 Deduplication on a Linked List
- PAT1097:Deduplication on a Linked List
- PAT (Advanced Level) Practise 1097 Deduplication on a Linked List (25)
- 1097. Deduplication on a Linked List
- PAT (Advanced Level) Practise 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
- 1097. Deduplication on a Linked List (25)
- 1097. Deduplication on a Linked List
- PAT - 甲级 - 1097. Deduplication on a Linked List (25)(链表)
- PAT1097 Deduplication on a Linked List
- 1097. Deduplication on a Linked List (25)-- v.erase(it)会很慢
- 1097.Deduplication on a Linked List