image.png

    1. #include <iostream>
    2. #include <list>
    3. #include <string>
    4. #include <stdio.h>
    5. using namespace std;
    6. int main()
    7. {
    8. int n, x1, x2;
    9. list<int> a[10001]; //用list定义数组,好神奇
    10. cin >> n;
    11. while (n--)
    12. {
    13. string st;
    14. cin >> st;
    15. if (st == "new") //这里不就相当于什么都没干,提前开好了已经
    16. {
    17. cin >> x1;
    18. }
    19. else if (st == "add")
    20. {
    21. cin >> x1 >> x2;
    22. a[x1].push_back(x2);
    23. }
    24. else if (st == "merge")
    25. {
    26. cin >> x1 >> x2;
    27. a[x1].merge(a[x2]); //在这里wa了好几次,我给他加上了清空函数,结果对,但是wa了
    28. }
    29. else if (st == "unique") //这里记得要先排序
    30. {
    31. cin >> x1;
    32. a[x1].sort();
    33. a[x1].unique();
    34. }
    35. else if (st == "out")
    36. {
    37. cin >> x1;
    38. a[x1].sort();
    39. list<int>::iterator i;
    40. for (i = a[x1].begin(); i != a[x1].end(); i++)
    41. {
    42. cout << *i << " ";
    43. }
    44. cout << endl;
    45. }
    46. }
    47. return 0;
    48. }
    • List 非常适合合并 merge 操作
    • List 竟然害提供了 unique 方法???
      • unique 之前一定要先排序