{"id":317,"date":"2013-06-11T19:50:25","date_gmt":"2013-06-11T11:50:25","guid":{"rendered":"http:\/\/blog.dayandcarrot.net\/?p=317"},"modified":"2013-06-11T19:50:25","modified_gmt":"2013-06-11T11:50:25","slug":"1032-sharing-25","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=317","title":{"rendered":"1032. Sharing (25)"},"content":{"rendered":"<h1><span style=\"font-size: 13px;\">\u65f6\u95f4\u9650\u5236<\/span><\/h1>\n<div id=\"problemInfo\">\n<div>\n<div>100 ms<\/div>\n<\/div>\n<div>\n<div>\u5185\u5b58\u9650\u5236<\/div>\n<div>32000 kB<\/div>\n<\/div>\n<div>\n<div>\u4ee3\u7801\u957f\u5ea6\u9650\u5236<\/div>\n<div>16000 B<\/div>\n<\/div>\n<div>\n<div>\u5224\u9898\u7a0b\u5e8f<\/div>\n<div>Standard<\/div>\n<\/div>\n<div>\u4f5c\u8005<\/div>\n<div>CHEN, Yue<\/div>\n<\/div>\n<div id=\"problemContent\">\nTo 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, &#8220;loading&#8221; and &#8220;being&#8221; are stored as showed in Figure 1.<br \/>\n<center><img decoding=\"async\" alt=\"\" src=\"http:\/\/pat.zju.edu.cn\/upload\/1w_m16pjsommxz.jpg\" \/><br \/>\nFigure 1<\/center>You are supposed to find the starting position of the common suffix (e.g. the position of &#8220;i&#8221; in Figure 1).<br \/>\n<b>Input Specification:<\/b><br \/>\nEach input file contains one test case. For each case, the first line contains two addresses of nodes and a positive N (&lt;= 10<sup>5<\/sup>), 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.<br \/>\nThen N lines follow, each describes a node in the format:<br \/>\n<i>Address Data Next<\/i><br \/>\nwhere\u00a0<i>Address<\/i>\u00a0is the position of the node,\u00a0<i>Data<\/i>\u00a0is the letter contained by this node which is an English letter chosen from {a-z, A-Z}, and<i>Next<\/i>\u00a0is the position of the next node.<br \/>\n<b>Output Specification:<\/b><br \/>\nFor each case, simply output the 5-digit starting position of the common suffix. If the two words have no common suffix, output &#8220;-1&#8221; instead.<br \/>\n<b>Sample Input 1:<\/b><\/p>\n<pre>11111 22222 9\n67890 i 00002\n00010 a 12345\n00003 g -1\n12345 D 67890\n00002 n 00003\n22222 B 23456\n11111 L 00001\n23456 e 67890\n00001 o 00010<\/pre>\n<p><b>Sample Output 1:<\/b><\/p>\n<pre>67890<\/pre>\n<p><b>Sample Input 2:<\/b><\/p>\n<pre>00001 00002 4\n00001 a 10001\n10001 s -1\n00002 a 10002\n10002 t -1<\/pre>\n<p><b>Sample Output 2:<\/b><\/p>\n<pre>-1<\/pre>\n<p>&nbsp;\n<\/p><\/div>\n<p>====================================<br \/>\n\u6ca1\u5565\u6280\u672f\u542b\u91cf\u7684\u9898\uff0c\u8bb0\u5f97\u65e9\u4e9b\u5e74\u6570\u636e\u7ed3\u6784\u671f\u4e2d\u8003\u8003\u8fc7\uff0c\u5f53\u65f6\u89c9\u5f97\u597d\u96be<br \/>\n\u5176\u5b9e\u5982\u679c\u6807\u8bb0\u4e0b\u8d70\u8fc7\u54ea\u91cc\u7684\u8bdd\u5c31\u597d\u7b80\u5355<br \/>\n\u6ca1\u5565\u597d\u8bf4\u7684\uff0c\u6ce8\u610f\u8f93\u51fa\u683c\u5f0f\uff0c\u6bd4\u59821\u8981\u53d8\u621000001<br \/>\n===================================<\/p>\n<pre>\n#include <stdio.h>\n#include <iostream>\n#include <iomanip> \/\/ std::setfill, std::setw\nusing namespace std;\n#define END_OF_NODE -1\n#define MAX_NODE_QTY 100000\nstruct Node\n{\n\tchar letter;\n\tint nextPtr;\n\tbool visited;\n\tNode()\n\t{\n\t\tnextPtr = END_OF_NODE;\n\t\tvisited = false;\n\t}\n};\nNode nodes[MAX_NODE_QTY];\nint main()\n{\n\tint root_A_ptr, root_B_ptr;\n\tint N;\n\tscanf(\"%d %d %d\", &root_A_ptr, &root_B_ptr, &N);\n\t\/\/Read node infomation\n\tfor(int i=0; i<n; i++)\n\t{\n\t\tint index, nextPtr;\n\t\tchar ch;\n\t\tscanf(\"%d %c %d\", &#038;index, &#038;ch, &#038;nextPtr);\n\t\tnodes[index].letter = ch; \/\/Actually it's not needed in the output..\n\t\tnodes[index].nextPtr = nextPtr;\n\t}\n\t\/\/\/\/Start from one root to traverse\n\tint ptrCurrent = root_A_ptr;\n\twhile(ptrCurrent != END_OF_NODE)\n\t{\n\t\tnodes[ptrCurrent].visited = true; \/\/Mark\n\t\tptrCurrent = nodes[ptrCurrent].nextPtr;\n\t}\n\t\/\/\/\/Start from next root until we find a visited node\n\tptrCurrent = root_B_ptr;\n\twhile(ptrCurrent != END_OF_NODE)\n\t{\n\t\tif( nodes[ptrCurrent].visited )\n\t\t{\n\t\t\t\/\/VISITED!\n\t\t\t\/\/printf(\"%d\", ptrCurrent);\n\t\t\tcout << setfill('0') << setw(5) << ptrCurrent;\n\t\t\treturn 0;\n\t\t}\n\t\tnodes[ptrCurrent].visited = true;\n\t\tptrCurrent = nodes[ptrCurrent].nextPtr;\n\t}\n\tprintf(\"-1\");\n\treturn 0;\n}\n<\/pre>\n<p>\u6d4b\u8bd5\u70b9\t\u7ed3\u679c\t\u7528\u65f6(ms)\t\u5185\u5b58(kB)\t\u5f97\u5206\/\u6ee1\u5206<br \/>\n0\t\u7b54\u6848\u6b63\u786e\t0\t1650\t10\/10<br \/>\n1\t\u7b54\u6848\u6b63\u786e\t0\t1770\t1\/1<br \/>\n2\t\u7b54\u6848\u6b63\u786e\t0\t1900\t8\/8<br \/>\n3\t\u7b54\u6848\u6b63\u786e\t0\t1630\t1\/1<br \/>\n4\t\u7b54\u6848\u6b63\u786e\t0\t1780\t2\/2<br \/>\n5\t\u7b54\u6848\u6b63\u786e\t30\t1900\t3\/3<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u65f6\u95f4\u9650\u5236 100 ms \u5185\u5b58\u9650\u5236 32000 kB \u4ee3\u7801\u957f\u5ea6\u9650\u5236 16000 B \u5224\u9898\u7a0b\u5e8f Standard \u4f5c\u8005 CHEN, Yue 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, &#8220;loading&#8221; and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[84],"class_list":["post-317","post","type-post","status-publish","format-standard","hentry","category-study","tag-pat"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/317","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=317"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/317\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=317"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=317"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=317"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}