This question first started with stack and map. First, the two linked lists were pushed into the stack, and then two elements were compared from the top of the stack. The last data time-out was found.
Then, a clever method is used to calculate the number of reverse link nodes of each node. As long as it is an intersection node, the number of reverse link nodes is 2, and thus the intersection point is calculated. I found that the last test case still timed out, so I searched for it online and found that someone had used the first method on the internet. Strange, is it because I processed all integers into strings? The input and output are changed to cin >>, why is cout? Pay attention to these details. Method 1: write the code
// 1032.cpp: defines the entry point of the console application. // # Include "stdafx. h "# include <string> # include <iostream> # include <map> # include <stack> using namespace std; struct Node {char data; string next ;}; stack <string> s1; stack <string> s2; int main () {string start, end; string a; int n; map <string, Node> list; freopen ("1032-in.txt", "r", stdin); // freopen ("1032-out.txt", "w", stdout ); while (cin> start> end> n) {for (int I = 0; I <n; I ++) {Node node; cin> a> node. data> nod E. next; list. insert (make_pair (a, node); // map-insert} string next = start; while (true) {s1.push (next); map <string, Node> :: iterator it = list. find (next); // map-find if (it! = List. end () next = it-> second. next; else break; if (next = "-1") break;} next = end; while (true) {s2.push (next );Map<String, Node >:: iterator it = list. find (next); // map-findIf (it! = List. end () next = it-> second. next; else break; if (next = "-1") break;} string t1, t2; string pos = ""; while (true) {t1 = s1.top (); t2 = s2.top (); s1.pop (); s2.pop (); if (t1! = T2 | s1.empty () | s2.empty () break; pos = t1;} if (! Pos. empty () cout <pos <endl; else cout <"-1" <endl; return 0 ;}
Map. find returns the position of the iterator input by map. erase or the value of the input keyword. Map must be used skillfully
The last thing we use is the method that many people use to mark the website. All the elements that have been accessed for the first time are marked.
Access started from start2. As long as it is an intersection node, when you access it again, the flag is 1, so the intersection node is calculated. This is the subject of the 2012 postgraduate entrance exam. Why can't I have any idea ......
// 1032.cpp: defines the entry point of the console application. // # Include "stdafx. h "# include <stdio. h ># include <string >#include <iostream> # include <map> # include <stack> using namespace std; struct Node {char data; int next ;} node [100010]; bool flag [100010]; int main () {int a, B, n; int s1, s2; char data; freopen ("1032-in.txt ", "r", stdin); while (scanf ("% d", & a, & B, & n )! = EOF) {int pos =-1; for (int I = 0; I <n; I ++) {scanf ("% d % c % d", & s1, & data, & s2); node [s1]. next = s2;} int next = a; while (next! =-1) {flag [next] = true; next = node [next]. next;} next = B; while (next! =-1) {if (flag [next]) {pos = next; break;} next = node [next]. next;} if (a = B) {printf ("% 05d \ n", a); return 0;} if (pos! =-1) printf ("% 05d \ n", pos); else printf ("-1 \ n");} return 0 ;}
Note that one test case is that the two start nodes are the same.
PAT-1032