乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      7.3 鏈表處理

       印度阿三17 2019-09-14

      2019年9月PAT - 練習(xí)筆記——7.3

      以下頁(yè)碼標(biāo)注的是閱讀器中實(shí)際頁(yè)碼,而不是書(shū)本身自印的頁(yè)碼。

      第7章 提高篇(1)——數(shù)據(jù)結(jié)構(gòu)專(zhuān)題(1)

      7.3 鏈表處理

      注意

      1. 注意結(jié)點(diǎn)地址輸出格式要求
      2. 注意對(duì)無(wú)效結(jié)點(diǎn)的處理

      目錄

      1. B1025 / A1074 反轉(zhuǎn)鏈表
      2. A1032 Sharing
      3. A1052 Linked List Sorting
      4. A1097 Deduplication on a Linked List

      1. B1025 / A1074 反轉(zhuǎn)鏈表

        給定一個(gè)常數(shù) K 以及一個(gè)單鏈表 L,請(qǐng)編寫(xiě)程序?qū)?L 中每 K 個(gè)結(jié)點(diǎn)反轉(zhuǎn)。例如:給定 L 為 1→2→3→4→5→6,K 為 3,則輸出應(yīng)該為 3→2→1→6→5→4;如果 K 為 4,則輸出應(yīng)該為 4→3→2→1→5→6,即最后不到 K 個(gè)元素不反轉(zhuǎn)。

        輸入格式:

        每個(gè)輸入包含 1 個(gè)測(cè)試用例。每個(gè)測(cè)試用例第 1 行給出第 1 個(gè)結(jié)點(diǎn)的地址、結(jié)點(diǎn)總個(gè)數(shù)正整數(shù) N (≤105)、以及正整數(shù) K (≤N),即要求反轉(zhuǎn)的子鏈結(jié)點(diǎn)的個(gè)數(shù)。結(jié)點(diǎn)的地址是 5 位非負(fù)整數(shù),NULL 地址用 ?1 表示。

        接下來(lái)有 N 行,每行格式為:

        Address Data Next
        

        其中 Address 是結(jié)點(diǎn)地址,Data 是該結(jié)點(diǎn)保存的整數(shù)數(shù)據(jù),Next 是下一結(jié)點(diǎn)的地址。

        輸出格式:

        對(duì)每個(gè)測(cè)試用例,順序輸出反轉(zhuǎn)后的鏈表,其上每個(gè)結(jié)點(diǎn)占一行,格式與輸入相同。

        輸入樣例:

        00100 6 4
        00000 4 99999
        00100 1 12309
        68237 6 -1
        33218 3 00000
        99999 5 68237
        12309 2 33218
        

        輸出樣例:

        00000 4 33218
        33218 3 12309
        12309 2 00100
        00100 1 99999
        99999 5 68237
        68237 6 -1
        
        1. 我的

          #include <iostream>
          #include <vector>
          #include <map>
          
          using namespace std;
          
          typedef struct {
          	int address;
          	int data;
          	int next;
          }Node;
          
          int main(void)
          {
          	int start = 0, n = 0, k = 0;
          	scanf("%d %d %d", &start, &n, &k);
          	
          	vector<Node> nodes(n);
          	map<int, int> normal, reverse;
          	for (int i = 0;i < n;  i) {
          		int address = 0, data = 0, next = 0;
          		scanf("%d %d %d", &address, &data, &next);
          		nodes[i] = {address, data, next};
          		normal[address] = i;
          		reverse[next] = i;
          	}
          	
          	int i = 0, j = 0, now = start, first = start;
          	for (;now != -1;  i,   j) {
          		now = nodes[normal[now]].next;
          		if (k - 1 == j % k) {
          			if (first != start) printf("d\n", nodes[reverse[now]].address);
          			for (int count = 0, now1 = now;count < k;  count) {
          				printf("d %d ", nodes[reverse[now1]].address, nodes[reverse[now1]].data);
          				now1 = nodes[reverse[now1]].address;
          				if (count   1 < k) printf("d\n", nodes[reverse[now1]].address);
          			}
          			first = now;
          		}
          	}
          	if (i % k) {
          		printf("d\n", first);
          		for (;first != -1;) {
          			printf("d %d ", first, nodes[normal[first]].data);
          			first = nodes[normal[first]].next;
          			if (first != -1) printf("d\n", first);
          		}
          	}
          	printf("-1");
          	
          	return 0;
          }
          
        2. 《算法筆記》P272

          要考慮可能存在無(wú)效結(jié)點(diǎn)的情況,即不是由題目給出的頭結(jié)點(diǎn)引出的單鏈表上的結(jié)點(diǎn),這些結(jié)點(diǎn)是要去掉的,最終不予輸出。”(測(cè)試點(diǎn)6)

          輸入:
          00000 6 3
          00000 1 11111
          11111 2 22222
          22222 3 -1
          33333 4 44444
          44444 5 55555
          55555 6 -1
          輸出:
          22222 3 11111
          11111 2 00000
          00000 1 -1
          

      2. A1032 Sharing

        To store English words, one method is to use linked lists and store a word letter by letter. To save some space, we may let the words share the same sublist if they share the same suffix. For example, loading and being are stored as showed in Figure 1.

        fig.jpg

        Figure 1

        You are supposed to find the starting position of the common suffix (e.g. the position of i in Figure 1).

        Input Specification:

        Each input file contains one test case. For each case, the first line contains two addresses of nodes and a positive N (≤105), where the two addresses are the addresses of the first nodes of the two words, and N is the total number of nodes. The address of a node is a 5-digit positive integer, and NULL is represented by ?1.

        Then N lines follow, each describes a node in the format:

        Address Data Next
        

        whereAddress is the position of the node, Data is the letter contained by this node which is an English letter chosen from { a-z, A-Z }, and Next is the position of the next node.

        Output Specification:

        For each case, simply output the 5-digit starting position of the common suffix. If the two words have no common suffix, output -1 instead.

        Sample Input 1:

        11111 22222 9
        67890 i 00002
        00010 a 12345
        00003 g -1
        12345 D 67890
        00002 n 00003
        22222 B 23456
        11111 L 00001
        23456 e 67890
        00001 o 00010
        

        Sample Output 1:

        67890
        

        Sample Input 2:

        00001 00002 4
        00001 a 10001
        10001 s -1
        00002 a 10002
        10002 t -1
        

        Sample Output 2:

        -1
        
        1. 我的

          #include <iostream>
          #include <map>
          
          using namespace std;
          
          int main(void)
          {
          	int start1 = 0, start2 = 0, n = 0;
          	scanf("%d %d %d", &start1, &start2, &n);
          	
          	map<int, int> normal;
          	for (int i = 0;i < n;  i) {
          		int address = 0, c = 0, next = 0;
          		scanf("%d %c %d", &address, &c, &next);
          		normal[address] = next;
          	}
          	
          	map<int, bool> link1;
          	int now = start1;
          	for (;now != -1;now = normal[now]) link1[now] = true;
          	now = start2;
          	for (;now != -1;now = normal[now]) if (link1[now]) break;
          	if (now != -1) printf("d", now);
          	else printf("-1");
          	
          	return 0;
          }
          

          注意無(wú)效結(jié)點(diǎn)的處理

          注意兩個(gè)鏈表頭結(jié)點(diǎn)為同一個(gè)的情況:測(cè)試點(diǎn)4

        2. 《算法筆記》P277


      3. A1052 Linked List Sorting

        A linked list consists of a series of structures, which are not necessarily adjacent in memory. We assume that each structure contains an integer key and a Next pointer to the next structure. Now given a linked list, you are supposed to sort the structures according to their key values in increasing order.

        Input Specification:

        Each input file contains one test case. For each case, the first line contains a positive N (<105) and an address of the head node, where N is the total number of nodes in memory and the address of a node is a 5-digit positive integer. NULL is represented by ?1.

        Then N lines follow, each describes a node in the format:

        Address Key Next
        

        where Address is the address of the node in memory, Key is an integer in [?105,105], and Next is the address of the next node. It is guaranteed that all the keys are distinct and there is no cycle in the linked list starting from the head node.

        Output Specification:

        For each test case, the output format is the same as that of the input, where N is the total number of nodes in the list and all the nodes must be sorted order.

        Sample Input:

        5 00001
        11111 100 -1
        00001 0 22222
        33333 100000 11111
        12345 -1 33333
        22222 1000 12345
        

        Sample Output:

        5 12345
        12345 -1 00001
        00001 0 11111
        11111 100 22222
        22222 1000 33333
        33333 100000 -1
        
        1. 我的

          #include <iostream>
          #include <map>
          
          using namespace std;
          
          int main(void)
          {
          	int n = 0, start = 0;
          	cin >> n >> start;
          	
          	map<int, int> normal, addresses, keys;
          	for (int i = 0;i < n;  i) {
          		int address = 0, key = 0, next = 0;
          		cin >> address >> key >> next;
          		normal[address] = next;
          		addresses[address] = key;
          	}
          	
          	int now = start;
          	for (;now != -1;now = normal[now]) keys[addresses[now]] = now;
          	
          	printf("%d ", keys.size());
          	map<int, int>::iterator iter = keys.begin();
          	for (;iter != keys.end();  iter) printf("d\nd %d ", iter->second, iter->second, iter->first);
          	printf("-1");
          	
          	return 0;
          }
          

          注意無(wú)效結(jié)點(diǎn)的處理:測(cè)試點(diǎn)1、4

        2. 《算法筆記》P279


      4. A1097 Deduplication on a Linked List

        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:

        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
        
        1. 我的

          #include <iostream>
          #include <map>
          #include <queue>
          
          using namespace std;
          
          map<int, int> normal, addresses;
          
          void Output(queue<int>&q)
          {
          	if (!q.empty()) {
          		printf("d %d ", q.front(), addresses[q.front()]);
          		q.pop();
          		for (;!q.empty();q.pop()) printf("d\nd %d ", q.front(), q.front(), addresses[q.front()]);
          		printf("-1\n");
          	}
          }
          
          int main(void)
          {
          	int start = 0, n = 0;
          	scanf("%d %d", &start, &n);
          	
          	queue<int> result, remove;
          	for (int i = 0;i < n;  i) {
          		int address = 0, key = 0, next = 0;
          		scanf("%d %d %d", &address, &key, &next);
          		normal[address] = next;
          		addresses[address] = key;
          	}
          	
          	map<int, bool> nums;
          	for (int now = start;now != -1;now = normal[now]) {
          		int key = addresses[now];
          		if (nums[abs(key)]) remove.push(now);
          		else {
          			result.push(now);
          			nums[abs(key)] = true;
          		}
          	}
          	Output(result);
          	Output(remove);
          	
          	return 0;
          }
          

          注意鏈表為空的情況:測(cè)試點(diǎn)2

        2. 《算法筆記》P282


      來(lái)源:https://www./content-4-451151.html

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶(hù) 評(píng)論公約

        類(lèi)似文章 更多