PAT 1097. Deduplication on a Linked List (25)

地址: http://www.patest.cn/contests/pat-a-practise/1097

没什么好说的, 我逗比就是了

  1 #include<iostream>

  2 #include<iomanip>

  3 #include<cstdio>

  4 #include<cstring>

  5 #include<map>

  6 #include<algorithm>

  7 

  8 using namespace std;

  9 

 10 const int MAX_NODES = 100000 + 10;

 11 struct List

 12 {

 13     int next[MAX_NODES];

 14     int last;

 15     int cur;

 16 

 17     List():last(0),cur(0)

 18     {

 19         memset(next, 0xff, sizeof(next));

 20     }

 21 

 22     void add_node(int position)

 23     {

 24         next[position] = next[last];

 25         next[last] = position;

 26         last = position;

 27     }

 28 }source, splited;

 29 

 30 const int MAX_NUM = 10000 + 10;

 31 const int NUL = -1;

 32 bool num_hash[MAX_NUM];

 33 

 34 struct Node

 35 {

 36     int addr;

 37     int key;

 38     int next_addr;

 39 

 40     inline bool operator < (const Node &other) const

 41     {

 42         return (*this).addr < other.addr;

 43     }

 44 } nodes[MAX_NODES];

 45 int addr2int[1000000 + 10];

 46 int head_addr;

 47 int n;

 48 

 49 void split()

 50 {

 51     memset(num_hash, 0, sizeof(num_hash));

 52     int pre = 0;

 53     for (int i = source.next[0]; i != -1; i = source.next[i])

 54     {

 55         int num = abs(nodes[i-1].key);

 56 

 57         if (!num_hash[num])

 58         {

 59             num_hash[num] = true;

 60             pre = i;

 61         }

 62         else

 63         {

 64             source.next[pre] = source.next[i];

 65             splited.add_node(i);

 66             i = pre;

 67         }

 68     }

 69 }

 70 void print()

 71 {

 72     for (int i = source.next[0]; i != -1; i = source.next[i])

 73     {

 74         cout << setw(5) << setfill('0') << nodes[i-1].addr << " ";

 75         cout << nodes[i-1].key << " ";

 76         if (source.next[i] == -1) cout << -1 << endl;

 77         else cout << setw(5) << setfill('0') <<  nodes[source.next[i] - 1].addr << endl;

 78     }

 79     for (int i = splited.next[0]; i != -1; i = splited.next[i])

 80     {

 81         cout << setw(5) << setfill('0') << nodes[i-1].addr << " ";

 82         cout << nodes[i-1].key << " ";

 83         if (splited.next[i] == -1)  cout << -1 << endl; 

 84         else cout << setw(5) << setfill('0') <<  nodes[splited.next[i] - 1].addr << endl;

 85     }

 86 }

 87 

 88 int main()

 89 {

 90     while(cin >> head_addr >> n)

 91     {

 92         for (int i = 0; i < n; ++i)

 93         {

 94             cin >> nodes[i].addr >> nodes[i].key >> nodes[i].next_addr;

 95         }

 96         sort(nodes, nodes + n);

 97         for (int i = 0; i < n; ++i)

 98         {

 99             addr2int[nodes[i].addr] = i + 1;

100         }

101         int position = addr2int[head_addr];

102         for (int i = 0; i < n; ++i)

103         {

104             source.add_node(position);

105             if (nodes[position - 1].next_addr == -1) break;

106             position = addr2int[nodes[position - 1].next_addr];

107         }

108         split();

109         print();

110     }

111     return 0;

112 }

 

你可能感兴趣的:(list)