PAT_A1097#Deduplication on a Linked List
Source:
Description:
Input Specification:
Output Specification:
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 Keys:
Attention:
Code:1 #include<cstdio> 2 #include<vector> 3 #include<cmath> 4 using namespace std; 5 const int M=1e5+10; 6 int mp[M]={0}; 7 struct node 8 { 9 int data; 10 int adrs,rear; 11 }link[M],t; 12 13 int main() 14 { 15 #ifdef ONLINE_JUDGE 16 #else 17 freopen("Test.txt","r",stdin); 18 #endif // ONLINE_JUDGE 19 20 int first,n; 21 scanf("%d%d",&first,&n); 22 for(int i=0; i<n; i++) 23 { 24 scanf("%d%d%d",&t.adrs,&t.data,&t.rear); 25 link[t.adrs] = t; 26 } 27 vector<int> keep,remo; 28 while(first != -1) 29 { 30 if(mp[(int)abs(link[first].data)]==0) 31 { 32 keep.push_back(first); 33 mp[(int)abs(link[first].data)]=1; 34 } 35 else 36 remo.push_back(first); 37 first = link[first].rear; 38 } 39 for(int i=0; i<keep.size(); i++) 40 { 41 if(i!=0) 42 printf("%05dn",keep[i]); 43 printf("%05d %d ",keep[i],link[keep[i]].data); 44 } 45 if(keep.size()) 46 printf("-1n"); 47 for(int i=0; i<remo.size(); i++) 48 { 49 if(i!=0) 50 printf("%05dn",remo[i]); 51 printf("%05d %d ",remo[i],link[remo[i]].data); 52 } 53 if(remo.size()) 54 printf("-1n"); 55 56 57 return 0; 58 } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |