{"id":559,"date":"2013-09-07T16:26:41","date_gmt":"2013-09-07T08:26:41","guid":{"rendered":"http:\/\/blog.dayandcarrot.net\/?p=559"},"modified":"2013-09-07T16:26:41","modified_gmt":"2013-09-07T08:26:41","slug":"1063-set-similarity-25","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=559","title":{"rendered":"1063. Set Similarity (25)"},"content":{"rendered":"<p>\u65f6\u95f4\u9650\u5236\uff1a300ms<br \/>\nGiven two sets of integers, the similarity of the sets is defined to be N<sub>c<\/sub>\/N<sub>t<\/sub>*100%, where N<sub>c<\/sub>\u00a0is the number of distinct common numbers shared by the two sets, and N<sub>t<\/sub>\u00a0is the total number of distinct numbers in the two sets. Your job is to calculate the similarity of any given pair of sets.<br \/>\n<b>Input Specification:<\/b><br \/>\nEach input file contains one test case. Each case first gives a positive integer N (&lt;=50) which is the total number of sets. Then N lines follow, each gives a set with a positive M (&lt;=10<sup>4<\/sup>) and followed by M integers in the range [0, 10<sup>9<\/sup>]. After the input of sets, a positive integer K (&lt;=2000) is given, followed by K lines of queries. Each query gives a pair of set numbers (the sets are numbered from 1 to N). All the numbers in a line are separated by a space.<br \/>\n<b>Output Specification:<\/b><br \/>\nFor each query, print in one line the similarity of the sets, in the percentage form accurate up to 1 decimal place.<br \/>\n<b>Sample Input:<\/b><\/p>\n<pre>3\n3 99 87 101\n4 87 101 5 87\n7 99 101 18 5 135 18 99\n2\n1 2\n1 3<\/pre>\n<p><b>Sample Output:<\/b><\/p>\n<pre>50.0%\n33.3%<\/pre>\n<p>=====================================<br \/>\n\u8fd9\u9898\u76ee\u770b\u8d77\u6765\u633a\u7b80\u5355\uff0c\u4f46\u662f\u56e0\u4e3a<br \/>\n1.\u6570\u636e\u91cf\u5927<br \/>\n2.\u6570\u503c\u8303\u56f4\u5927<br \/>\n\u6240\u4ee5<br \/>\n1.\u8981\u8003\u8651\u6c42\u4ea4\u96c6\u5e76\u96c6\u7684\u6700\u4f73\u7b97\u6cd5<br \/>\n2.\u4e0d\u80fd\u7528bitmap\u6765\u7528\u7a7a\u95f4\u6362\u65f6\u95f4<br \/>\n\u8fd9\u4e2a\u6c42\u4ea4\u96c6\u5e76\u96c6\uff08\u6570\u91cf\uff09\u7684\u7b97\u6cd5\u5176\u5b9e\u8bf4\u6765\u4e5f\u4e0d\u96be\uff0c<br \/>\n\u6709\u4e24\u4e2a\u96c6\u5408A,B\uff0c\u5148\u5047\u8bbe<br \/>\n\u4ed6\u4eec\u5e76\u96c6\u7684\u5927\u5c0fuSize = |A|+|B|<br \/>\n\u4ea4\u96c6\u7684\u5927\u5c0fiSize = 0<br \/>\n\u96c6\u5408\u8fdb\u884c\u6392\u5e8f(\u6700\u5c0f\u65f6\u95f4\u590d\u6742\u5ea6O(nlogn))\u540e\uff0c\u53ef\u4ee5\u7528O(1)\u7684\u7b97\u6cd5\u627e\u51fa\u5e76\u96c6\u4ea4\u96c6\uff1a<br \/>\n<code lang=\"python\"><br \/>\nA\u5143\u7d20\u6307\u9488 ptrA = A.begin()<br \/>\nB\u5143\u7d20\u6307\u9488 ptrB = B.begin()<br \/>\nwhile \u5217\u8868A\/B\u90fd\u6ca1\u6709\u904d\u5386\u5c3d:<br \/>\n    valA = *ptrA<br \/>\n    valB = *ptrB<br \/>\n    if valA == valB:<br \/>\n        ptrA++<br \/>\n        ptrB++<br \/>\n        iSize ++<br \/>\n        uSize -- #\u53bb\u6389\u591a\u4f59\u7684\u5143\u7d20<br \/>\n    elif valA > valB:<br \/>\n        #\u6570\u503c\u5c0f\u7684\u5f80\u540e\u632a\u52a8\u4e00\u683c<br \/>\n        ptrB++<br \/>\n    else:<br \/>\n        ptrA++<br \/>\n<\/code><br \/>\n\u5177\u4f53\u4ee3\u7801\u5982\u4e0b\uff0c\u540e\u6765\u60f3\u60f3\u5176\u5b9e\u7528<set>\u5b9e\u73b0\u66f4\u65b9\u4fbf\u4e00\u70b9:)<br \/>\n<code lang=\"c++\"><br \/>\n#include <stdio.h><br \/>\n#include <vector><br \/>\n#include <algorithm><br \/>\nusing namespace std;<br \/>\ndouble getSimi(vector<long> a, vector<long> b)<br \/>\n{<br \/>\n\tvector<long> *ptrA = &a;<br \/>\n\tvector<long> *ptrB = &b;<br \/>\n\tif( a.size() > b.size())<br \/>\n\t{<br \/>\n\t\t\/\/\u4fdd\u8bc1ptrA\u96c6\u5408\u6570\u76ee\u5c0f<br \/>\n\t\tptrB = &a;<br \/>\n\t\tptrA = &b;<br \/>\n\t}<br \/>\n\tsort(ptrA->begin(), ptrA->end());<br \/>\n\tsort(ptrB->begin(), ptrB->end());<br \/>\n\tint iSize = 0;<br \/>\n\tint uSize = ptrB->size() + ptrA->size();<br \/>\n\tvector<long>::iterator itA = ptrA->begin();<br \/>\n\tvector<long>::iterator itB = ptrB->begin();<br \/>\n\twhile(itA != ptrA->end() && itB!=ptrB->end())<br \/>\n\t{<br \/>\n\t\tlong valA = *itA;<br \/>\n\t\tlong valB = *itB;<br \/>\n\t\tif(valA == valB)<br \/>\n\t\t{<br \/>\n\t\t\tiSize ++;<br \/>\n\t\t\tuSize --;<br \/>\n\t\t\titA++;<br \/>\n\t\t\titB++;<br \/>\n\t\t}<br \/>\n\t\telse if(valA > valB)<br \/>\n\t\t{<br \/>\n\t\t\t\/\/B\u961f\u5217\u4e0b\u79fb\u4e00\u4f4d<br \/>\n\t\t\titB++;<br \/>\n\t\t}<br \/>\n\t\telse<br \/>\n\t\t{<br \/>\n\t\t\t\/\/A\u961f\u5217\u4e0b\u79fb\u4e00\u4f4d<br \/>\n\t\t\titA++;<br \/>\n\t\t}<br \/>\n\t}<br \/>\n\treturn static_cast<double>(iSize)\/uSize;<br \/>\n}<br \/>\nint main()<br \/>\n{<br \/>\n\tint N;<br \/>\n\tscanf(\"%d\", &N);<br \/>\n\tvector<long> *arySets = new vector<long>[N];<br \/>\n\tfor(int i=0; i<n; i++)\n\t{\n\t\tint M;\n\t\tscanf(\"%d\", &#038;M);\n\t\tfor(int j=0; j<m; j++)\n\t\t{\n\t\t\tlong value;\n\t\t\tscanf(\"%ld\", &#038;value);\n\t\t\tif( find(arySets[i].begin(), arySets[i].end(), value) == arySets[i].end())\n\t\t\t\tarySets[i].push_back(value);\n\t\t}\n\t}\n\tint K;\n\tscanf(\"%d\", &#038;K);\n\tfor(int i=0; i<k; i++)\n\t{\n\t\tint index[2];\n\t\tscanf(\"%d %d\", index, &#038;index[1]);\n\t\tindex[0]--;\n\t\tindex[1]--; \/\/\u8c03\u6574\u4e3a\u4ece0\u5f00\u59cb\u7684\u5e8f\u53f7\n\t\tdouble ratio =\n\t\t\tgetSimi(arySets[index[0]], arySets[index[1]]) * 100.0;\n\t\tprintf(\"%.1f%%n\", ratio);\n\t}\n\treturn 0;\n}\n<\/code><\/p>\n<pre>\n\u6d4b\u8bd5\u70b9\t\u7ed3\u679c\t\u7528\u65f6(ms)\t\u5185\u5b58(kB)\t\u5f97\u5206\/\u6ee1\u5206\n0\t\u7b54\u6848\u6b63\u786e\t0\t720\t12\/12\n1\t\u7b54\u6848\u6b63\u786e\t0\t720\t3\/3\n2\t\u7b54\u6848\u6b63\u786e\t0\t790\t3\/3\n3\t\u7b54\u6848\u6b63\u786e\t10\t790\t3\/3\n4\t\u7b54\u6848\u6b63\u786e\t260\t990\t4\/4\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u65f6\u95f4\u9650\u5236\uff1a300ms Given two sets of integers, the similarity of the sets is defined to be Nc\/Nt*100%, where Nc\u00a0is the number of distinct common numbers shared by the two sets, and Nt\u00a0is the total number of distinct numbers in the two sets. Your job is to calculate the similarity of any given pair of sets. Input [&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-559","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\/559","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=559"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/559\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=559"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=559"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=559"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}