{"id":3070,"date":"2025-12-27T16:22:02","date_gmt":"2025-12-27T08:22:02","guid":{"rendered":"https:\/\/www.xfan.top\/?p=3070"},"modified":"2026-02-04T00:53:51","modified_gmt":"2026-02-03T16:53:51","slug":"%e5%89%8d%e7%ab%af%e5%b7%a5%e7%a8%8b%e5%ae%9e%e8%b7%b5%e4%b8%ad%e7%9a%84%e7%ac%9b%e5%8d%a1%e5%b0%94%e7%a7%af%e7%9b%b4%e7%a7%af%e7%94%9f%e6%88%90","status":"publish","type":"post","link":"https:\/\/www.xfan.top\/index.php\/2025\/12\/27\/3070\/","title":{"rendered":"\u524d\u7aef\u5de5\u7a0b\u5b9e\u8df5\u4e2d\u7684\u7b1b\u5361\u5c14\u79ef(\u76f4\u79ef)\u751f\u6210"},"content":{"rendered":"<h2>\u7b1b\u5361\u5c14\u79ef(\u76f4\u79ef)(Cartesian Product)\u7684\u5b9a\u4e49<\/h2>\n<p>\u7ed9\u5b9a\u591a\u4e2a\u96c6\u5408\uff1a<\/p>\n<pre><code>A = [a1, a2]\nB = [b1, b2, b3]\nC = [c1, c2]<\/code><\/pre>\n<p>\u5b83\u4eec\u7684\u7b1b\u5361\u5c14\u79ef\u5b9a\u4e49\u4e3a\uff1a<\/p>\n<pre><code>A \u00d7 B \u00d7 C = \n    [\n        [a1, b1, c1], \n        [a1, b1, c2],\n        [a1, b2, c1], \n        [a1, b2, c2],\n        ...\n    ]<\/code><\/pre>\n<p><strong>\u672c\u8d28<\/strong>\uff1a<\/p>\n<blockquote>\n<p>\u4ece\u6bcf\u4e00\u4e2a\u96c6\u5408\u4e2d\u5404\u53d6\u4e00\u4e2a\u5143\u7d20\uff0c\u7ec4\u6210\u4e00\u4e2a\u5143\u7ec4\uff0c\u679a\u4e3e\u6240\u6709\u53ef\u80fd\u3002<\/p>\n<\/blockquote>\n<h2>\u4f20\u7edf\u5b9e\u73b0\u65b9\u5f0f\u7684\u95ee\u9898<\/h2>\n<p>\u6700\u5e38\u89c1\u7684 JavaScript \u5b9e\u73b0\u662f\uff1a<\/p>\n<pre><code>function cartesianProduct(...arrays) {\n    return arrays.reduce(\n        (acc, cur) =&gt; acc.flatMap(a =&gt; cur.map(b =&gt; [...a, b])),\n        [[]]\n    );\n}<\/code><\/pre>\n<h3>\u968f\u4e4b\u4ea7\u751f\u7684\u95ee\u9898<\/h3>\n<p>\u5047\u8bbe\uff1a<\/p>\n<ul>\n<li>\u7ef4\u5ea6\u6570 = <code>d<\/code><\/li>\n<li>\u6bcf\u4e2a\u7ef4\u5ea6\u5e73\u5747\u5927\u5c0f = <code>k<\/code><\/li>\n<\/ul>\n<p>\u90a3\u4e48\uff1a<\/p>\n<ul>\n<li>\u7ed3\u679c\u6570\u91cf = <code>k^d<\/code><\/li>\n<li>\u65f6\u95f4\u590d\u6742\u5ea6 = <code>O(k^d)<\/code><\/li>\n<li>\u7a7a\u95f4\u590d\u6742\u5ea6 = <code>O(k^d)<\/code><\/li>\n<\/ul>\n<blockquote>\n<p><strong>\u53ea\u8981\u6570\u636e\u91cf\u7a0d\u5fae\u5927\u4e00\u70b9\uff0c\u8fd9\u79cd\u5199\u6cd5\u4e00\u5b9a\u4f1aOOM\u3002<\/strong><\/p>\n<\/blockquote>\n<h2>\u5de5\u7a0b\u4e2d\u7684\u771f\u5b9e\u9700\u6c42\u662f\u4ec0\u4e48\uff1f<\/h2>\n<p>\u5728\u5b9e\u9645\u5de5\u7a0b\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\uff1a<\/p>\n<ul>\n<li><strong>\u4e0d\u9700\u8981\u5168\u90e8\u7ed3\u679c<\/strong>\u3001\u4e0d\u5141\u8bb8\u4e00\u6b21\u6027\u751f\u6210\u5168\u96c6<\/li>\n<li>\u53ea\u9700\u8981\uff1a\n<ul>\n<li>\u524d <code>N<\/code> \u4e2a<\/li>\n<li>\u6216\u8005\u968f\u673a\u62bd\u53d6 <code>N<\/code> \u4e2a<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u6240\u4ee5\u6838\u5fc3\u95ee\u9898\u53d8\u6210\u4e86\uff1a<\/p>\n<blockquote>\n<p><strong>\u5982\u4f55\u5728\u4e0d\u751f\u6210\u5b8c\u6574\u7b1b\u5361\u5c14\u79ef\u7684\u60c5\u51b5\u4e0b\uff0c\u6709\u5e8f \/ \u968f\u673a\u5730\u4ea7\u751f\u90e8\u5206\u7ed3\u679c\uff1f<\/strong><\/p>\n<\/blockquote>\n<hr \/>\n<h2>\u53ef\u4e2d\u65ad\u7684\u7b1b\u5361\u5c14\u79ef\u751f\u6210 - \u751f\u6210\u5668(Generator) \u65b9\u6848<\/h2>\n<h3>\u57fa\u672c\u601d\u60f3<\/h3>\n<ul>\n<li><strong>\u6df1\u5ea6\u4f18\u5148\u904d\u5386\uff08DFS\uff09<\/strong><\/li>\n<li>\u6bcf\u8d70\u5230\u4e00\u6761\u5b8c\u6574\u8def\u5f84\uff0c\u5c31 <code>yield<\/code> \u4e00\u4e2a\u7ed3\u679c<\/li>\n<li>\u5916\u90e8\u53ea\u53d6\u9700\u8981\u7684\u524d <code>N<\/code> \u4e2a<\/li>\n<\/ul>\n<h3>\u57fa\u7840\u5b9e\u73b0<\/h3>\n<pre><code class=\"language-js\">static getFirstNCartesianProduct&lt;T&gt;(arrays: T[][], count: number): T[][] {\n    const results: T[][] = [];\n\n    function* generate(index = 0, current: T[] = []): Generator&lt;T[]&gt; {\n        if (index === arrays.length) {\n            yield current;\n            return;\n        }\n\n        for (const item of arrays[index]) {\n            yield* generate(index + 1, [...current, item]);\n        }\n    }\n\n    const generator = generate();\n\n    while (results.length &lt; count) {\n        const next = generator.next();\n        if (next.done) break;\n        results.push(next.value);\n    }\n\n    return results;\n}\n<\/code><\/pre>\n<p>\u5e76\u4e0d\u662f\u4e00\u6b21\u6027\u8fd4\u56de\u6240\u6709\u7ed3\u679c\uff0c\u800c\u662f\uff1a<\/p>\n<ul>\n<li><strong>\u6682\u505c\u51fd\u6570\u6267\u884c<\/strong><\/li>\n<li>\u628a <code>value<\/code> \u4ea4\u7ed9\u8c03\u7528\u65b9<\/li>\n<li>\u4e0b\u6b21 <code>.next()<\/code> \u65f6\u4ece\u6682\u505c\u70b9\u7ee7\u7eed<\/li>\n<\/ul>\n<h3><code>yield*<\/code> \u7684\u4f5c\u7528<\/h3>\n<pre><code>yield* generate(nextIndex, nextState);<\/code><\/pre>\n<p>\u7b49\u4ef7\u4e8e\uff1a<\/p>\n<blockquote>\n<p>\u201c\u628a\u5b50\u751f\u6210\u5668\u4ea7\u751f\u7684\u6240\u6709 <code>yield<\/code>\uff0c\u539f\u6837\u8f6c\u4ea4\u7ed9\u5f53\u524d\u751f\u6210\u5668\u201d<\/p>\n<\/blockquote>\n<p><strong>\u975e\u5e38\u5173\u952e\u7684\u4e00\u70b9\uff1a<\/strong><\/p>\n<blockquote>\n<p><code>yield*<\/code> \u5e76\u4e0d\u662f\u4e00\u6b21\u6027\u8fd4\u56de\u6570\u7ec4<br \/>\n\u5b83\u662f <strong>\u9010\u4e2a\u8f6c\u53d1\u5b50\u751f\u6210\u5668\u7684 yield<\/strong><\/p>\n<\/blockquote>\n<hr \/>\n<h3>\u7528\u6700\u7b80\u5355\u7684\u4f8b\u5b50\u7406\u89e3<\/h3>\n<pre><code>function* inner() {\n  yield 1;\n  yield 2;\n}\n\nfunction* outer() {\n  yield 0;\n  yield* inner();\n  yield 3;\n}\n\nconst g = outer()<\/code><\/pre>\n<p>\u8fde\u7eed\u8c03\u7528<code>g.next()<\/code>\uff0c\u4f9d\u6b21\u8f93\u51fa\u7684\u662f\uff1a<\/p>\n<pre><code>0\n1\n2\n3<\/code><\/pre>\n<p>\u800c\u4e0d\u662f\uff1a<\/p>\n<pre><code>0\n[1,2]\n3<\/code><\/pre>\n<hr \/>\n<h2>\u4e3a\u4ec0\u4e48 Generator \u65b9\u6848\u9002\u5408\u201c\u987a\u5e8f\u7b1b\u5361\u5c14\u79ef\u201d<\/h2>\n<p>Generator \u65b9\u6848\u5929\u7136\u9002\u5408\uff1a<\/p>\n<ul>\n<li><strong>\u6309\u5b57\u5178\u5e8f<\/strong><\/li>\n<li><strong>\u6309 DFS \u987a\u5e8f<\/strong><\/li>\n<li><strong>\u7a33\u5b9a\u3001\u53ef\u590d\u73b0<\/strong><\/li>\n<\/ul>\n<p>\u4f46\u5b83\u6709\u4e00\u4e2a\u9650\u5236\uff1a<\/p>\n<blockquote>\n<p>\u4e0d\u9002\u5408\u201c\u76f4\u63a5\u968f\u673a\u8bbf\u95ee\u7b2c K \u4e2a\u7ec4\u5408\u201d<\/p>\n<\/blockquote>\n<p>\u8fd9\u6b63\u597d\u5f15\u51fa\u4e86\u53e6\u4e00\u7c7b\u65b9\u6848\u3002<\/p>\n<hr \/>\n<h2>\u968f\u673a\u7b1b\u5361\u5c14\u79ef\uff1a\u6df7\u5408\u8fdb\u5236\uff08Mixed-radix\uff09\u679a\u4e3e<\/h2>\n<h3>\u6838\u5fc3\u601d\u60f3<\/h3>\n<p>\u5047\u8bbe\uff1a<\/p>\n<pre><code>arrays = [\n  [A, B, C],   \/\/ 3\n  [1, 2],      \/\/ 2\n  [x, y, z]    \/\/ 3\n]\n<\/code><\/pre>\n<p>\u90a3\u4e48\u6bcf\u4e00\u4e2a\u7ec4\u5408\u90fd\u53ef\u4ee5\u770b\u6210\u4e00\u4e2a <strong>\u6df7\u5408\u8fdb\u5236\u6570<\/strong>\uff1a<\/p>\n<pre><code>[ i0, i1, i2 ]\n\u5176\u4e2d\uff1a\n0 \u2264 i0 &lt; 3\n0 \u2264 i1 &lt; 2\n0 \u2264 i2 &lt; 3<\/code><\/pre>\n<pre><code>static _generateRandomCombination = &lt;T&gt;(\n  arrays: T[][],\n  maxIndex: number[]\n): T[] =&gt; {\n  return maxIndex.map((len, i) =&gt;\n    arrays[i][Math.floor(Math.random() * len)]\n  );\n};<\/code><\/pre>\n<p>\u7279\u70b9\uff1a<\/p>\n<ul>\n<li>O(\u7ef4\u5ea6\u6570)<\/li>\n<li>\u4e0d\u9700\u8981\u751f\u6210\u5168\u96c6<\/li>\n<li>\u975e\u5e38\u9002\u5408\u968f\u673a\u91c7\u6837<\/li>\n<\/ul>\n<h3>\u53d7\u63a7\u968f\u673a\u91c7\u6837\u4e3b\u51fd\u6570<\/h3>\n<p>\u4ece\u7ed9\u5b9a\u7684\u591a\u7ef4\u6570\u7ec4\u4e2d\u968f\u673a\u751f\u6210\u6216\u8ba1\u7b97\u7b1b\u5361\u5c14\u79ef\u4e2d\u6307\u5b9a\u6570\u91cf\u7684\u6837\u672c\u7ec4\u5408\u3002<br \/>\n\u6839\u636e\u6837\u672c\u6570\u91cf\u4e0e\u603b\u7ec4\u5408\u6570\u91cf\u7684\u6bd4\u4f8b\uff0c\u81ea\u52a8\u9009\u62e9\u7b56\u7565\uff1a<\/p>\n<ul>\n<li>\u76f4\u63a5\u5c55\u5f00\u5b8c\u6574\u7b1b\u5361\u5c14\u79ef\uff08\u5c0f\u89c4\u6a21\uff09<\/li>\n<li>\u968f\u673a\u91c7\u6837 + \u53bb\u91cd\uff08\u5c0f\u6837\u672c\uff09<\/li>\n<li>\u968f\u673a\u91c7\u6837\uff08\u5141\u8bb8\u91cd\u590d\uff0c\u5927\u6837\u672c\uff09<\/li>\n<\/ul>\n<pre><code>static randomCartesianProduct = &lt;T&gt;(\n  arrays: T[][],\n  numSamples: number\n): T[][] =&gt; {\n  const maxIndex = arrays.map(arr =&gt; arr.length);\n  const totalCombinations = maxIndex.reduce((acc, val) =&gt; acc * val, 1);\n\n  \/\/ \u60c5\u51b5 1\uff1a\u7a7a\u95f4\u8f83\u5c0f\uff0c\u76f4\u63a5\u5c55\u5f00\n  if (numSamples &gt; 0.6 * totalCombinations || totalCombinations &lt; 10000) {\n    return this.randomChild(\n      this.cartesianProduct(...arrays),\n      numSamples\n    );\n  }\n\n  \/\/ \u60c5\u51b5 2\uff1a\u6837\u672c\u91cf\u8f83\u5c0f\uff0c\u53bb\u91cd\u91c7\u6837\n  else if (numSamples &lt; 0.2 * totalCombinations) {\n    const result: T[][] = [];\n    const seen = new Set&lt;string&gt;();\n\n    while (result.length &lt; numSamples) {\n      const combination = this._generateRandomCombination(arrays, maxIndex);\n      const key = JSON.stringify(combination);\n\n      if (!seen.has(key)) {\n        seen.add(key);\n        result.push(combination);\n      }\n    }\n    return result;\n  }\n\n  \/\/ \u60c5\u51b5 3\uff1a\u4e2d\u95f4\u533a\u95f4\uff0c\u5141\u8bb8\u91cd\u590d\uff0c\u6027\u80fd\u4f18\u5148\n  else {\n    const result: T[][] = [];\n    for (let i = 0; i &lt; numSamples; i++) {\n      result.push(this._generateRandomCombination(arrays, maxIndex));\n    }\n    return result;\n  }\n};<\/code><\/pre>\n<hr \/>\n<h2>\u5de5\u7a0b\u7ea7\u7b1b\u5361\u5c14\u79ef\u7b56\u7565\u603b\u7ed3<\/h2>\n<table>\n<thead>\n<tr>\n<th>\u9700\u6c42<\/th>\n<th>\u63a8\u8350\u65b9\u6848<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u987a\u5e8f\u3001\u53ef\u590d\u73b0<\/td>\n<td>Generator\uff08DFS + yield\uff09<\/td>\n<\/tr>\n<tr>\n<td>\u53ea\u53d6\u524d N \u4e2a<\/td>\n<td>Generator + \u622a\u65ad<\/td>\n<\/tr>\n<tr>\n<td>\u968f\u673a\u62bd\u6837<\/td>\n<td>\u6df7\u5408\u8fdb\u5236\u968f\u673a<\/td>\n<\/tr>\n<tr>\n<td>\u6837\u672c\u63a5\u8fd1\u5168\u96c6<\/td>\n<td>\u76f4\u63a5\u751f\u6210\u5168\u96c6\u518d shuffle<\/td>\n<\/tr>\n<tr>\n<td>\u6570\u636e\u91cf\u5de8\u5927<\/td>\n<td>Generator \/ Mixed-radix<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr \/>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u8fd9\u662f\u4e00\u5957\u4f7f\u7528\u5728<br \/>\n<a href=\"https:\/\/www.xfan.top\/index.php\/2024\/03\/18\/novelai-prompts-task-writer\/\">https:\/\/www.xfan.top\/index.php\/2024\/03\/18\/novelai-prompts-task-writer\/<\/a><br \/>\n\u4e2d\u7684\u8ba1\u7b97\u65b9\u6cd5\uff0c\u7528\u4ee5\u89e3\u51b3\u5927\u91cf\u63d0\u793a\u8bcd\u7684\u987a\u5e8f\u3001\u968f\u673a\u7ec4\u5408\u95ee\u9898\u3002<br \/>\n\u53c2\u4e0e\u7b1b\u5361\u5c14\u79ef\u8ba1\u7b97\u7684\u96c6\u5408\u662f\u4e0a\u4e00\u6b65\u6392\u5217\u3001\u7ec4\u5408\u751f\u6210\u7684\uff0c\u901a\u5e38\u6709\u5341\u51e0\u81f3\u51e0\u5341\u4e2a\u96c6\u5408\uff0c\u6bcf\u4e2a\u96c6\u5408\u53ef\u80fd\u5305\u542b\u591a\u8fbe\u6570\u5341\u4e2a\u5143\u7d20\uff0c\u6837\u672c\u7a7a\u95f4\u53ef\u80fd\u8d85\u51faNumber\u7c7b\u578b\u53ef\u5b58\u50a8\u7684\u4e0a\u9650\uff0c\u8ba1\u7b97\u76f8\u5173<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7b1b\u5361\u5c14\u79ef(\u76f4\u79ef)(Cartesian Product)\u7684\u5b9a\u4e49 \u7ed9\u5b9a\u591a\u4e2a\u96c6\u5408\uff1a A = [a1, a2] B = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22],"tags":[],"class_list":["post-3070","post","type-post","status-publish","format-standard","hentry","category-jsts"],"_links":{"self":[{"href":"https:\/\/www.xfan.top\/index.php\/wp-json\/wp\/v2\/posts\/3070","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xfan.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xfan.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xfan.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xfan.top\/index.php\/wp-json\/wp\/v2\/comments?post=3070"}],"version-history":[{"count":18,"href":"https:\/\/www.xfan.top\/index.php\/wp-json\/wp\/v2\/posts\/3070\/revisions"}],"predecessor-version":[{"id":3097,"href":"https:\/\/www.xfan.top\/index.php\/wp-json\/wp\/v2\/posts\/3070\/revisions\/3097"}],"wp:attachment":[{"href":"https:\/\/www.xfan.top\/index.php\/wp-json\/wp\/v2\/media?parent=3070"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xfan.top\/index.php\/wp-json\/wp\/v2\/categories?post=3070"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xfan.top\/index.php\/wp-json\/wp\/v2\/tags?post=3070"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}