{"id":92,"date":"2023-03-23T21:07:00","date_gmt":"2023-03-23T13:07:00","guid":{"rendered":"https:\/\/solost.cn\/?p=92"},"modified":"2023-03-24T20:22:39","modified_gmt":"2023-03-24T12:22:39","slug":"185","status":"publish","type":"post","link":"https:\/\/solost.cn\/?p=92","title":{"rendered":"\u7b2c\u5341\u4e8c\u5c4a\u84dd\u6865\u676f\u7701\u8d5bc\/c++B\u7ec4-\u62ec\u53f7\u5e8f\u5217"},"content":{"rendered":"<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2026\/11\/image-11.png\" alt=\"image.png\" title=\"image.png\"><\/p>\n<h2>\u9898\u76ee\u8fde\u63a5<\/h2>\n<p><a href=\"https:\/\/www.acwing.com\/problem\/content\/description\/3423\/\">\u9898\u76ee\u94fe\u63a5<\/a><\/p>\n<h2>\u53c2\u8003\u94fe\u63a5<\/h2>\n<p><a href=\"https:\/\/blog.csdn.net\/weixin_60896526\/article\/details\/125436735?spm=1001.2101.3001.6661.1&utm_medium=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-125436735-blog-122726632.235%5Ev27%5Epc_relevant_recovery_v2&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-125436735-blog-122726632.235%5Ev27%5Epc_relevant_recovery_v2&utm_relevant_index=1\">\u53c2\u8003\u94fe\u63a5<\/a><\/p>\n<p><a href=\"https:\/\/www.acwing.com\/solution\/content\/75383\/\">\u53c2\u8003\u94fe\u63a5<\/a><\/p>\n<h2>\u9898\u89e3<\/h2>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2026\/11\/image1-4.png\" alt=\"image(1).png\" title=\"image(1).png\"><\/p>\n<p>\u601d\u8def\u5e94\u8be5\u662f\u8ba1\u7b97\u51fa\u8981\u52a0\u7684\u5de6\u62ec\u53f7\u6570\u76ee\uff0c\u7136\u540e\u518d\u955c\u50cf\u5b57\u7b26\u4e32\u8ba1\u7b97\u51fa\u9700\u8981\u6dfb\u52a0\u53f3\u62ec\u53f7\u7684\u6570\u76ee\uff0c\u76f8\u4e58\u5373\u53ef\u3002\u56e0\u4e3a\u8ba9\u4ed6\u53d8\u6210\u4e00\u4e2a\u5408\u6cd5\u7684\u5b57\u7b26\u4e32\u9700\u8981\u5de6\u62ec\u53f7\u7684\u6570\u76ee\u5927\u4e8e\u7b49\u4e8e\u53f3\u62ec\u53f7\u7684\u5b57\u7b26\u4e32\uff0c\u6211\u4eec\u7b2c\u4e00\u6b21\u53ef\u4ee5\u8ba1\u7b97\u51fa\u4f7f\u5f97\u53f3\u62ec\u53f7\u4e00\u5b9a\u6709\u7684\u5339\u914d\u7684\u65b9\u6cd5\u6570\u518d\u4e58\u53e6\u4e00\u79cd\u60c5\u51b5\u5373\u53ef\u3002<\/p>\n<blockquote>\n<p><code>f[i][j]<\/code>\u7684\u72b6\u6001\u8868\u793a<\/p>\n<p>(1)\u96c6\u5408\uff1a\u53ea\u8003\u8651\u524d i \u90e8\u5206\uff0c\u5de6\u62ec\u53f7\u6bd4\u53f3\u62ec\u53f7\u591a j \u4e2a\u7684\u6240\u6709\u65b9\u6848\u7684\u96c6\u5408(\u5373\u4e0d\u540c\u6570\u91cf\u7684\u5de6\u62ec\u53f7\u7684\u65b9\u6848\u6570)<\/p>\n<p>(2)\u5c5e\u6027\uff1a\u6570\u91cf<\/p>\n<p>\u72b6\u6001\u8ba1\u7b97\uff1a<\/p>\n<p>(1) \u82e5<code>str[i] == &#039;(&#039;<\/code>\uff0c\u6613\u63a8\u7b97\u51fa\u8f6c\u79fb\u65b9\u7a0b\u4e3a<code>f[i][j] = f[i - 1][j - 1]<\/code><\/p>\n<p>(2) \u82e5<code>str[i] == &#039;)&#039;<\/code>\uff0c\u5219\u5728\u53ea\u8003\u8651\u524d i - 1\u90e8\u5206\u65f6\uff0c\u6b64\u65f6\u5de6\u62ec\u53f7\u6570\u91cf\u6700\u591a\u6bd4\u53f3\u62ec\u53f7\u591a j + 1\u4e2a\uff0c\u624d\u53ef\u80fd\u5728\u7b2c i \u90e8\u5206\u901a\u8fc7\u6dfb\u52a0\u6216\u8005\u4e0d\u52a0\u5de6\u62ec\u53f7\uff0c\u8fbe\u5230\u5de6\u62ec\u53f7\u7684\u6570\u91cf\u6bd4\u53f3\u62ec\u53f7\u591a j \u4e2a\uff0c\u6545\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e3a<code>f[i][j] = f[i - 1][j + 1] + f[i - 1][j] + ...... + f[i - 1][0]<\/code>\uff0c\u901a\u8fc7\u5b8c\u5168\u80cc\u5305\u7684\u601d\u7ef4\u4f18\u5316\u8fd9\u4e00\u6bb5\u8f6c\u79fb\u65b9\u7a0b\uff0c\u4f7f\u5f97\u8be5\u90e8\u5206\u603b\u590d\u6742\u5ea6\u7531 <code>O(n^3) <\/code>\u964d\u4e3a<code>O(n^2)<\/code>\uff0c\u4f18\u5316\u7684\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e3a<code>f[i][j] = f[i - 1][j + 1] + f[i][j - 1]<\/code> (\u4e3a\u4e86\u9632\u6b62\u8d8a\u754c\uff0c<code>f[i][0]<\/code>\u9700\u8981\u7279\u5224)\u3002<\/p>\n<\/blockquote>\n<pre><code class=\"lang-cpp\">#include &lt;algorithm&gt;\n#include &lt;bitset&gt;\n#include &lt;cmath&gt;\n#include &lt;cstdio&gt;\n#include &lt;cstdlib&gt;\n#include &lt;cstring&gt;\n#include &lt;fstream&gt;\n#include &lt;iostream&gt;\n#include &lt;queue&gt;\n#include &lt;set&gt;\n#include &lt;stack&gt;\n#include &lt;unordered_map&gt;\n#include &lt;vector&gt;\n\nusing namespace std;\n#define x first\n#define y second\n#define endl &#039;\\n&#039;\n#define IOS\nios_base::sync_with_stdio(0);\ncin.tie(0);\ncout.tie(0);\ntypedef long long ll;\ntypedef pair&lt;int, int&gt; PII;\ntypedef unsigned long long ull;\nconst int INF = 0x3f3f3f3f, mod = 1000000007;\nconst int N = 2e5 + 10;\n\nint f[5010][5010];\n\nint op(string s)\n{\n    memset(f, 0, sizeof(f));\n    f[0][0] = 1;\n    int n = s.size();\n    s = &#039; &#039; + s;\n    for (int i = 1; i &lt;= n; i++) {\n        if (s[i] == &#039;(&#039;) {\n            for (int j = 1; j &lt;= n; j++) {\n                f[i][j] = f[i - 1][j - 1];\n            }\n        } else {\n            f[i][0] = (f[i - 1][1] + f[i - 1][0]) % mod;\n            for (int j = 1; j &lt;= n; j++) {\n                f[i][j] = (f[i - 1][j + 1] + f[i][j - 1]) % mod;\n            }\n        }\n    }\n    for (int i = 0; i &lt;= n; i++) {\n        if (f[n][i])\n            return f[n][i];\n    }\n    return 0;\n}\n\nvoid solve()\n{\n    string s;\n    cin &gt;&gt; s;\n    int l = op(s);\n    reverse(s.begin(), s.end());\n    for (auto&amp; x : s) {\n        if (x == &#039;)&#039;)\n            x = &#039;(&#039;;\n        else\n            x = &#039;)&#039;;\n    }\n    int r = op(s);\n    cout &lt;&lt; (1ll * l * r) % mod &lt;&lt; endl;\n}\nint main()\n{\n    \/\/ IOS;\n    int t = 1;\n    \/\/ cin &gt;&gt; t;\n    while (t--) {\n        solve();\n    }\n    return 0;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u8fde\u63a5 \u9898\u76ee\u94fe\u63a5 \u53c2\u8003\u94fe\u63a5 \u53c2\u8003\u94fe\u63a5 \u53c2\u8003\u94fe\u63a5 \u9898\u89e3 \u601d\u8def\u5e94\u8be5\u662f\u8ba1\u7b97\u51fa\u8981\u52a0\u7684\u5de6\u62ec\u53f7\u6570\u76ee\uff0c\u7136\u540e\u518d\u955c\u50cf\u5b57\u7b26\u4e32\u8ba1\u7b97\u51fa\u9700\u8981\u6dfb\u52a0\u53f3\u62ec\u53f7\u7684\u6570\u76ee\uff0c\u76f8\u4e58\u5373\u53ef\u3002\u56e0\u4e3a\u8ba9\u4ed6\u53d8\u6210\u4e00\u4e2a\u5408\u6cd5\u7684\u5b57\u7b26\u4e32\u9700\u8981\u5de6\u62ec\u53f7\u7684\u6570\u76ee\u5927\u4e8e\u7b49\u4e8e\u53f3\u62ec\u53f7\u7684\u5b57\u7b26\u4e32\uff0c\u6211\u4eec\u7b2c\u4e00\u6b21\u53ef\u4ee5\u8ba1\u7b97\u51fa\u4f7f\u5f97\u53f3\u62ec\u53f7\u4e00\u5b9a\u6709\u7684\u5339\u914d\u7684\u65b9\u6cd5\u6570\u518d\u4e58\u53e6\u4e00\u79cd\u60c5\u51b5\u5373\u53ef\u3002 f[i][j]\u7684\u72b6\u6001\u8868\u793a (1)\u96c6\u5408\uff1a\u53ea\u8003\u8651\u524d i \u90e8\u5206\uff0c\u5de6\u62ec\u53f7\u6bd4\u53f3\u62ec\u53f7\u591a j \u4e2a\u7684\u6240\u6709\u65b9\u6848\u7684\u96c6\u5408(\u5373\u4e0d\u540c\u6570\u91cf\u7684\u5de6\u62ec\u53f7\u7684\u65b9\u6848\u6570) (2)\u5c5e\u6027\uff1a\u6570\u91cf \u72b6\u6001\u8ba1\u7b97\uff1a (1) \u82e5str[i] == &#039;(&#039;\uff0c\u6613\u63a8\u7b97\u51fa\u8f6c\u79fb\u65b9\u7a0b\u4e3af[i][j] = f[i - 1][j - 1] (2) \u82e5str[i] == &#039;)&#039;\uff0c\u5219\u5728\u53ea\u8003\u8651\u524d i - 1\u90e8\u5206\u65f6\uff0c\u6b64\u65f6\u5de6\u62ec\u53f7\u6570\u91cf\u6700\u591a\u6bd4\u53f3\u62ec\u53f7\u591a j + 1\u4e2a\uff0c\u624d\u53ef\u80fd\u5728\u7b2c i \u90e8\u5206\u901a\u8fc7\u6dfb\u52a0\u6216\u8005\u4e0d\u52a0\u5de6\u62ec\u53f7\uff0c\u8fbe\u5230\u5de6\u62ec\u53f7\u7684\u6570\u91cf\u6bd4\u53f3\u62ec\u53f7\u591a j \u4e2a\uff0c\u6545\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e3af[i][j] = f[i - 1][j + 1] + f[i - 1][j] + ...... + f[i - 1][0]\uff0c\u901a\u8fc7\u5b8c\u5168\u80cc\u5305\u7684\u601d\u7ef4\u4f18\u5316\u8fd9\u4e00\u6bb5\u8f6c\u79fb\u65b9\u7a0b\uff0c\u4f7f\u5f97\u8be5\u90e8\u5206\u603b\u590d\u6742\u5ea6\u7531 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[40,18],"class_list":["post-92","post","type-post","status-publish","format-standard","hentry","category-solution","tag-dp","tag-18"],"_links":{"self":[{"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/posts\/92","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=92"}],"version-history":[{"count":0,"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/posts\/92\/revisions"}],"wp:attachment":[{"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=92"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=92"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=92"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}