2019年9月8日秋季PAT甲级题解-2-1161-Merging Linked Lists (25 分) 双解法

青旅半醒 2024-04-18 15:28 152阅读 0赞

Given two singly linked lists L​1​​=a​1​​→a​2​​→⋯→a​n−1​​→a​n​​ and L​2​​=b​1​​→b​2​​→⋯→b​m−1​​→b​m​​. If n≥2m, you are supposed to reverse and merge the shorter one into the longer one to obtain a list like a​1​​→a​2​​→b​m​​→a​3​​→a​4​​→b​m−1​​⋯. For example, given one list being 6→7 and the other one 1→2→3→4→5, you must output 1→2→7→3→4→6→5.

Input Specification:

Each input file contains one test case. For each case, the first line contains the two addresses of the first nodes of L​1​​ and L​2​​, plus a positive N (≤10​^5​​) which is the total number of nodes given. 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:

  1. Address Data Next

where Address is the position of the node, Data is a positive integer no more than 10​5​​, and Next is the position of the next node. It is guaranteed that no list is empty, and the longer list is at least twice as long as the shorter one.

Output Specification:

For each case, output in order the resulting linked list. Each node occupies a line, and is printed in the same format as in the input.

Sample Input:

  1. 00100 01000 7
  2. 02233 2 34891
  3. 00100 6 00001
  4. 34891 3 10086
  5. 01000 1 02233
  6. 00033 5 -1
  7. 10086 4 00033
  8. 00001 7 -1

Sample Output:

  1. 01000 1 02233
  2. 02233 2 00001
  3. 00001 7 34891
  4. 34891 3 10086
  5. 10086 4 00100
  6. 00100 6 00033
  7. 00033 5 -1

分析

属于甲级常考的静态链表题目。如果想了解静态链表题的思路可以先去看晴神宝典。

第一种写法就是晴神宝典的写法,用排序,只用一个vector,并且会修改到新链表的next项。

第二种写法就是参考1133柳神的写法,不同排序,用多个vector,不用修改next项。

第三种写法是第二种写法的优化(大家直接看她的就可以了):https://blog.csdn.net/Exupery_/article/details/100698828


满分代码(方法一)

  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <set>
  5. #include <map>
  6. #include <cmath>
  7. using namespace std;
  8. int k, n,m,a,b,c,d,e,ar=1,br=1;
  9. int ad[100005];
  10. struct node{
  11. int addr,data,next,rank;
  12. bool f=false;
  13. };
  14. vector<node> v;
  15. bool cmp(node &a,node &b){
  16. if(a.f!=b.f)return a.f>b.f;
  17. else return a.rank<b.rank;
  18. }
  19. int main() {
  20. scanf("%d %d %d", &a,&b,&n);
  21. v.resize(n);
  22. for(int i=0;i<n;i++){
  23. scanf("%d %d %d",&c,&d,&e);
  24. ad[c]=i;
  25. v[i].addr=c;
  26. v[i].data=d;
  27. v[i].next=e;
  28. }
  29. int as=a,bs=b;
  30. while(as!=-1){
  31. v[ad[as]].rank=ar;
  32. v[ad[as]].f=true;
  33. as=v[ad[as]].next;
  34. ar++;
  35. }
  36. br=ar;
  37. while(bs!=-1){
  38. v[ad[bs]].rank=br;
  39. v[ad[bs]].f=true;
  40. bs=v[ad[bs]].next;
  41. br++;
  42. }
  43. ar--;br--;
  44. if(ar<br-ar){
  45. as=b;
  46. bs=a;
  47. ar=1;
  48. while(as!=-1){
  49. v[ad[as]].rank=ar;
  50. v[ad[as]].f=true;
  51. as=v[ad[as]].next;
  52. ar++;
  53. }
  54. br=ar;
  55. while(bs!=-1){
  56. v[ad[bs]].rank=br;
  57. v[ad[bs]].f=true;
  58. bs=v[ad[bs]].next;
  59. br++;
  60. }
  61. ar--;br--;
  62. }
  63. sort(v.begin(),v.end(),cmp);
  64. for(int i=0;i<br;i++){
  65. if(i>1 && i<ar){
  66. v[i].rank+=i/2;
  67. }else if(i>=ar){
  68. v[i].rank=(br-i)*3;
  69. }
  70. }
  71. sort(v.begin(),v.end(),cmp);
  72. int bl=br-ar;
  73. for(int i=0;i<br;i++){
  74. if(bl>0 && i>0 && (i+1)%3==0){
  75. int t=v[i-1].next;
  76. v[i-1].next=v[i].addr;
  77. v[i].next=t;
  78. bl--;
  79. }
  80. }
  81. for(int i=0;i<br;i++){
  82. if(i==br-1) printf("%05d %d -1",v[i].addr,v[i].data);
  83. else printf("%05d %d %05d\n",v[i].addr,v[i].data,v[i].next);
  84. }
  85. return 0;
  86. }

满分代码(方法二)

  1. #include<iostream>
  2. #include<vector>
  3. using namespace std;
  4. struct node{
  5. int id,data,next;
  6. };
  7. node a[100005];
  8. int main(){
  9. int ba,bb,n,s,d,e;
  10. scanf("%d %d %d\n",&ba,&bb,&n);
  11. vector<node> va,vb,ans;
  12. for(int i=0;i<n;i++){
  13. scanf("%d %d %d\n",&s,&d,&e);
  14. a[s]={s,d,e};
  15. }
  16. for(;ba!=-1;ba=a[ba].next){
  17. va.push_back(a[ba]);
  18. }
  19. for(;bb!=-1;bb=a[bb].next){
  20. vb.push_back(a[bb]);
  21. }
  22. if(va.size()>vb.size()){
  23. int j=(int)vb.size()-1;
  24. for(int i=0;i<va.size();i++){
  25. if(i>0 && i%2==0 && j>=0){
  26. ans.push_back(vb[j]);
  27. j--;
  28. }
  29. ans.push_back(va[i]);
  30. if(i==va.size()-1 && j==0){
  31. ans.push_back(vb[j]);
  32. }
  33. }
  34. }else {
  35. int j=(int)va.size()-1;
  36. for(int i=0;i<vb.size();i++){
  37. if(i>0 && i%2==0 && j>=0){
  38. ans.push_back(va[j]);
  39. j--;
  40. }
  41. ans.push_back(vb[i]);
  42. if(i==vb.size()-1 && j==0){
  43. ans.push_back(va[j]);
  44. }
  45. }
  46. }
  47. for(int i=0;i<ans.size();i++){
  48. if(i!=ans.size()-1)printf("%05d %d %05d\n",ans[i].id,ans[i].data,ans[i+1].id);
  49. else printf("%05d %d -1",ans[i].id,ans[i].data);
  50. }
  51. return 0;
  52. }

更多测试数据集

  1. //输入1
  2. 00100 01000 6
  3. 02233 2 34891
  4. 00100 6 00001
  5. 34891 3 10086
  6. 01000 1 02233
  7. 10086 4 -1
  8. 00001 7 -1
  9. //输出1
  10. 01000 1 02233
  11. 02233 2 00001
  12. 00001 7 34891
  13. 34891 3 10086
  14. 10086 4 00100
  15. 00100 6 -1
  16. //输入2
  17. 00100 01000 8
  18. 02233 2 34891
  19. 00100 7 00001
  20. 34891 3 10086
  21. 01000 1 02233
  22. 00033 5 02342
  23. 10086 4 00033
  24. 00001 8 -1
  25. 02342 6 -1
  26. //输出2
  27. 01000 1 02233
  28. 02233 2 00001
  29. 00001 8 34891
  30. 34891 3 10086
  31. 10086 4 00100
  32. 00100 7 00033
  33. 00033 5 02342
  34. 02342 6 -1
  35. //输入3
  36. 01000 00033 3
  37. 02233 2 -1
  38. 01000 1 02233
  39. 00033 3 -1
  40. //输出3
  41. 01000 1 02233
  42. 02233 2 00033
  43. 00033 3 -1
  44. //输入4
  45. 00033 01000 3
  46. 02233 2 -1
  47. 01000 1 02233
  48. 00033 3 -1
  49. //输出4
  50. 01000 1 02233
  51. 02233 2 00033
  52. 00033 3 -1
  53. //输入6:游离节点不能输出
  54. 02233 01000 6
  55. 02233 2 00033
  56. 01000 1 -1
  57. 00033 3 23421
  58. 23421 4 -1
  59. 27834 9 78932
  60. 78932 8 -1
  61. //输出6
  62. 02233 2 00033
  63. 00033 3 01000
  64. 01000 1 23421
  65. 23421 4 -1

发表评论

表情:
评论列表 (有 0 条评论,152人围观)

还没有评论,来说两句吧...

相关阅读