PAT 1032. Sharing

其实就是链表求交:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <unordered_map>
 5
 6 using namespace std;
 7
 8 class Node {
 9 public:
10     Node() : data(0), next(0) {}
11     char data;
12     int next;
13 };
14
15 void print_list(unordered_map<int, Node*>& mem, int head) {
16     int cur = head;
17     while (cur != -1) {
18         cout<<mem[cur]->data<<" ";
19         cur = mem[cur]->next;
20     }
21     cout<<endl;
22 };
23
24 int step_list(unordered_map<int, Node*>& mem, int from, int n) {
25     if (n < 0) return -1;
26     int cur = from;
27     while (cur != -1) {
28         if (n == 0) {
29             break;
30         }
31         cur = mem[cur]->next;
32         n--;
33     }
34     return cur;
35 }
36
37 int count_list(unordered_map<int, Node*>& mem, int from) {
38     if (from < 0) return 0;
39     int cur =  from;
40     int cnt = 0;
41     while (cur != -1) {
42         cur = mem[cur]->next;
43         cnt++;
44     }
45     return cnt;
46 }
47
48 int main() {
49     int head_a, head_b;
50     int n;
51
52     unordered_map<int, Node*> mem;
53
54     cin>>head_a>>head_b>>n;
55
56     for (int i=0; i<n; i++) {
57         int addr;
58         Node* np = new Node();
59         scanf("%d %c %d", &addr, &(np->data), &(np->next));
60         mem.insert(make_pair(addr, np));
61     }
62
63     int cnt_a = count_list(mem, head_a);
64     int cnt_b = count_list(mem, head_b);
65
66     int cnt_diff = cnt_a - cnt_b;
67     int head_long  = head_a;
68     int head_short = head_b;
69     if (cnt_diff < 0) {
70         cnt_diff = -cnt_diff;
71         head_long = head_b;
72         head_short= head_a;
73     }
74
75     head_long = step_list(mem, head_long, cnt_diff);
76
77     while (head_long != head_short) {
78         head_long = mem[head_long]->next;
79         head_short= mem[head_short]->next;
80     }
81     if (head_long < 0) {
82         printf("-1\n");
83     } else {
84         printf("%05d\n", head_long);
85     }
86     system("pause");
87     return 0;
88 }
时间: 2024-10-26 04:53:48

PAT 1032. Sharing的相关文章

PAT 1032. Sharing (25)

1032. Sharing (25) 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

1032. Sharing (25)【链表】——PAT (Advanced Level) Practise

题目信息 1032. Sharing (25) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B 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.

【PAT甲级】1032 Sharing (25分)

1032 Sharing (25分) 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 a

PAT:1032. Sharing (25) AC

#include<stdio.h> #include<stdlib.h> #include<string.h> struct node { char data; int next; bool tag; }Node[100066]; int main() { int add1,add2,n; //连表1首地址, scanf("%d%d%d",&add1,&add2,&n); memset(Node,0,sizeof(Node))

PAT (Advanced Level) 1032. Sharing (25)

简单题,不过数据中好像存在有环的链表...... #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<map> #include<queue> #include<vector> using namespace std; const int maxn=100000+10

PAT Advanced 1032 Sharing(25) [链表]

题目 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 sufix. For example, "loading" and "being" are stored

PAT 1032 (未完成)

斗智斗勇系列,这个时间限制太变态了,10^5的数据读进去100ms就完蛋,buffer又不能太多开,但是8M buffer开出来其实也没啥用,还是超时 除去超时数据点,还有一个数据点没有过,不知道是什么边界条件,不管了 判断两条链表是否相交,首先两条链表都走到最后一个节点,如果节点相同,则说明这两条链表是相交的.算出两条链表的长度,len1,len2,max(len1, len2) - min(len1, len2),长的链表先走diff step,再和短的链表一起走,相遇的节点即为共同节点的起

PAT 1032. 挖掘机技术哪家强

PAT1032. 挖掘机技术哪家强 为了用事实说明挖掘机技术到底哪家强,PAT组织了一场挖掘机技能大赛.现请你根据比赛结果统计出技术最强的那个学校. 输入格式: 输入在第1行给出不超过105的正整数N,即参赛人数.随后N行,每行给出一位参赛者的信息和成绩,包括其所代表的学校的编号(从1开始连续编号).及其比赛成绩(百分制),中间以空格分隔. 输出格式: 在一行中给出总得分最高的学校的编号.及其总分,中间以空格分隔.题目保证答案唯一,没有并列. 输入样例: 6 3 65 2 80 1 100 2

PAT 1003 Sharing (25)

题目描述 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 sto