{"id":58,"date":"2024-12-10T12:37:37","date_gmt":"2024-12-10T04:37:37","guid":{"rendered":"https:\/\/blog.wendianli.cn\/?p=58"},"modified":"2024-12-10T12:37:37","modified_gmt":"2024-12-10T04:37:37","slug":"%e5%88%a4%e6%96%ad%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%8c%85%e5%90%ab%e5%ad%97%e6%af%8d%ef%bc%8c%e6%95%b0%e5%ad%97%ef%bc%8c%e7%89%b9%e6%ae%8a%e7%ac%a6%e5%8f%b7","status":"publish","type":"post","link":"https:\/\/blog.notiq.cn\/index.php\/2024\/12\/10\/%e5%88%a4%e6%96%ad%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%8c%85%e5%90%ab%e5%ad%97%e6%af%8d%ef%bc%8c%e6%95%b0%e5%ad%97%ef%bc%8c%e7%89%b9%e6%ae%8a%e7%ac%a6%e5%8f%b7\/","title":{"rendered":"\u5224\u65ad\u5b57\u7b26\u4e32\u5305\u542b\u5b57\u6bcd\uff0c\u6570\u5b57\uff0c\u7279\u6b8a\u7b26\u53f7"},"content":{"rendered":"<h2>\u4f7f\u7528c++11\u65b0\u7279\u6027\uff1a<code>any_of<\/code><\/h2>\n<h3>1.1\u51fd\u6570\u539f\u578b\uff1a<\/h3>\n<pre><code class=\"line-numbers\">template&lt;class InputIt, class UnaryPredicate&gt;\nbool any_of(InputIt first, InputIt last, UnaryPredicate p);\n<\/code><\/pre>\n<h4>\u53c2\u6570\u89e3\u91ca<\/h4>\n<ol>\n<li><strong>InputIt first<\/strong>\uff1a<\/li>\n<\/ol>\n<ul>\n<li>\u8fd9\u662f\u8f93\u5165\u8303\u56f4\u7684\u8d77\u59cb\u8fed\u4ee3\u5668\uff0c\u6307\u5411\u8981\u68c0\u67e5\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002<\/li>\n<\/ul>\n<ol start=\"2\">\n<li><strong>InputIt last<\/strong>\uff1a<\/li>\n<\/ol>\n<ul>\n<li>\u8fd9\u662f\u8f93\u5165\u8303\u56f4\u7684\u7ed3\u675f\u8fed\u4ee3\u5668\uff0c\u6307\u5411\u8981\u68c0\u67e5\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u4e4b\u540e\u7684\u4f4d\u7f6e\uff08\u5373\u8303\u56f4\u662f <code>[first, last)<\/code>\uff09\u3002<\/li>\n<\/ul>\n<ol start=\"3\">\n<li><strong>UnaryPredicate p<\/strong>\uff1a<\/li>\n<\/ol>\n<ul>\n<li>\u8fd9\u662f\u4e00\u4e2a\u4e00\u5143\u8c13\u8bcd\u51fd\u6570\u6216\u51fd\u6570\u5bf9\u8c61\uff0c\u7528\u4e8e\u5bf9\u8303\u56f4\u5185\u7684\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u68c0\u67e5\u3002\u8c13\u8bcd\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u5143\u7d20\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\uff0c\u8868\u793a\u8be5\u5143\u7d20\u662f\u5426\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u3002<\/li>\n<\/ul>\n<p><strong>\u5173\u4e8e\u7b2c\u4e09\u4e2a\u53c2\u6570\u7684\u4f7f\u7528<\/strong><\/p>\n<ol>\n<li><strong>\u4f7f\u7528\u6807\u51c6\u5e93\u51fd\u6570<\/strong>\uff1a<\/li>\n<li><strong>\u4f7f\u7528 lambda \u8868\u8fbe\u5f0f<\/strong>\uff1a<\/li>\n<li><strong>\u4f7f\u7528\u51fd\u6570\u6307\u9488<\/strong>\uff1a<\/li>\n<li><strong>\u4f7f\u7528\u51fd\u6570\u5bf9\u8c61<\/strong>\uff1a<\/li>\n<\/ol>\n<h4>\u8fd4\u56de\u503c<\/h4>\n<ul>\n<li>\u5982\u679c\u8303\u56f4\u5185\u81f3\u5c11\u6709\u4e00\u4e2a\u5143\u7d20\u6ee1\u8db3\u8c13\u8bcd\u51fd\u6570 <code>p<\/code>\uff0c\u5219\u8fd4\u56de <code>true<\/code>\u3002<\/li>\n<li>\u5426\u5219\uff0c\u8fd4\u56de <code>false<\/code><\/li>\n<\/ul>\n<h3>1.2\u53c2\u6570\u793a\u4f8b<\/h3>\n<pre><code class=\"line-numbers\">#include &lt;iostream&gt;\n#include &lt;vector&gt;\n#include &lt;algorithm&gt; \/\/ \u5305\u542b any_of\n#include &lt;cctype&gt; \/\/ \u5305\u542b isdigit \u548c isalpha\n\nusing namespace std;\n\n\/\/ \u81ea\u5b9a\u4e49\u51fd\u6570\u5bf9\u8c61\nstruct IsSpecialChar {\n    bool operator()(char c) const {\n        return !isalnum(c); \/\/ \u975e\u5b57\u6bcd\u548c\u6570\u5b57\u7684\u5b57\u7b26\u5373\u4e3a\u7279\u6b8a\u5b57\u7b26\n    }\n};\n\n\/\/ \u81ea\u5b9a\u4e49\u51fd\u6570\nbool isSpecialChar(char c) {\n    return !isalnum(c);\n}\n\nint main() {\n    string str = \"Hello123!\";\n    \/\/ \u4f7f\u7528\u6807\u51c6\u5e93\u51fd\u6570\n    bool hasDigit = any_of(str.begin(), str.end(), ::isdigit);\n    if (hasDigit) {\n        cout &lt;&lt; \"String contains a digit.\" &lt;&lt; endl;\n    } else {\n        cout &lt;&lt; \"String does not contain any digit.\" &lt;&lt; endl;\n    }\n\n    \/\/ \u4f7f\u7528 lambda \u8868\u8fbe\u5f0f\n    bool hasAlpha = any_of(str.begin(), str.end(), [](char c) {\n        return isalpha(c);\n    });\n    if (hasAlpha) {\n        cout &lt;&lt; \"String contains a letter.\" &lt;&lt; endl;\n    } else {\n        cout &lt;&lt; \"String does not contain any letter.\" &lt;&lt; endl;\n    }\n\n    \/\/ \u4f7f\u7528\u51fd\u6570\u6307\u9488\n    bool hasSpecialChar1 = any_of(str.begin(), str.end(), isSpecialChar);\n    if (hasSpecialChar1) {\n        cout &lt;&lt; \"String contains a special character (using function pointer).\" &lt;&lt; endl;\n    } else {\n        cout &lt;&lt; \"String does not contain any special character (using function pointer).\" &lt;&lt; endl;\n    }\n\n    \/\/ \u4f7f\u7528\u51fd\u6570\u5bf9\u8c61\n    bool hasSpecialChar2 = any_of(str.begin(), str.end(), IsSpecialChar());\n    if (hasSpecialChar2) {\n        cout &lt;&lt; \"String contains a special character (using function object).\" &lt;&lt; endl;\n    } else {\n        cout &lt;&lt; \"String does not contain any special character (using function object).\" &lt;&lt; endl;\n    }\n\n    return 0;\n}\n<\/code><\/pre>\n<h3>1.3\u9898\u76ee\u793a\u4f8b<\/h3>\n<p>\u6839\u636e\u590d\u6742\u7a0b\u5ea6\u4e0d\u540c\uff0c\u5bc6\u7801\u5b89\u5168\u5ea6\u88ab\u5206\u4e3a\u9ad8\u3001\u4e2d\u3001\u4f4e\u4e09\u6863\u3002<\/p>\n<ul>\n<li>\u9ad8\uff1a\u7531\u4e0a\u8ff0 64 \u79cd\u5b57\u7b26\u7ec4\u6210\uff0c\u957f\u5ea6\u5927\u4e8e\u7b49\u4e8e 6 \u4e2a\u5b57\u7b26\uff0c\u5305\u542b\u5b57\u6bcd\u3001\u6570\u5b57\u548c\u7279\u6b8a\u5b57\u7b26\uff0c\u540c\u4e00\u4e2a\u5b57\u7b26\u51fa\u73b0\u4e0d\u8d85\u8fc7 2 \u6b21\uff1b<\/li>\n<li>\u4e2d\uff1a\u7531\u4e0a\u8ff0 64 \u79cd\u5b57\u7b26\u7ec4\u6210\uff0c\u957f\u5ea6\u5927\u4e8e\u7b49\u4e8e 6 \u4e2a\u5b57\u7b26\uff0c\u5305\u542b\u5b57\u6bcd\u3001\u6570\u5b57\u548c\u7279\u6b8a\u5b57\u7b26\uff0c\u4e14\u672a\u8fbe\u5230\u9ad8\u5b89\u5168\u5ea6\u8981\u6c42\uff1b<\/li>\n<li>\u4f4e\uff1a\u7531\u4e0a\u8ff0 64 \u79cd\u5b57\u7b26\u7ec4\u6210\uff0c\u957f\u5ea6\u5927\u4e8e\u7b49\u4e8e 6 \u4e2a\u5b57\u7b26\uff0c\u4e14\u672a\u8fbe\u5230\u4e2d\u5b89\u5168\u5ea6\u8981\u6c42\uff1b<\/li>\n<\/ul>\n<p>\u5c0f P \u4e3a\u81ea\u5df1\u51c6\u5907\u4e86 <em>n<\/em> \u4e2a\u5019\u9009\u5bc6\u7801\uff0c\u8bd5\u7f16\u5199\u7a0b\u5e8f\u5e2e\u5c0f P \u81ea\u52a8\u5224\u522b\u6bcf\u4e2a\u5bc6\u7801\u7684\u5b89\u5168\u7ea7\u522b\u3002\u4fdd\u8bc1\u8fd9 n<em>n<\/em> \u4e2a\u5bc6\u7801\u90fd\u81f3\u5c11\u6ee1\u8db3\u4f4e\u5b89\u5168\u5ea6\u8981\u6c42\uff0c\u5f53\u5b89\u5168\u5ea6\u4e3a\u9ad8\u3001\u4e2d\u3001\u4f4e\u65f6\u5206\u522b\u8f93\u51fa <code>2<\/code>\u3001<code>1<\/code>\u3001<code>0<\/code> \u5373\u53ef\u3002<\/p>\n<p><strong>\u8f93\u5165<\/strong><\/p>\n<ul>\n<li>\u4ece\u6807\u51c6\u8f93\u5165\u8bfb\u5165\u6570\u636e\u3002<\/li>\n<li>\u8f93\u5165\u5171 n+1 \u884c\u3002<\/li>\n<li>\u7b2c\u4e00\u884c\u5305\u542b\u4e00\u4e2a\u6b63\u6574\u6570 n\uff0c\u8868\u793a\u5f85\u5224\u522b\u7684\u5bc6\u7801\u4e2a\u6570\uff1b<\/li>\n<li>\u63a5\u4e0b\u6765 n \u884c\uff0c\u6bcf\u884c\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u8868\u793a\u4e00\u4e2a\u5b89\u5168\u5ea6\u81f3\u5c11\u4e3a\u4f4e\u7684\u5019\u9009\u5bc6\u7801\u3002<\/li>\n<\/ul>\n<p><strong>\u8f93\u51fa<\/strong><\/p>\n<ul>\n<li>\u8f93\u51fa\u5230\u6807\u51c6\u8f93\u51fa\u3002<\/li>\n<li>\u8f93\u51fa\u5171 n \u884c\uff0c\u6bcf\u884c\u8f93\u51fa\u4e00\u4e2a\u6574\u6570 <code>2<\/code>\u3001<code>1<\/code> \u6216 <code>0<\/code>\uff0c\u8868\u793a\u5bf9\u5e94\u5bc6\u7801\u7684\u5b89\u5168\u5ea6\u3002<\/li>\n<\/ul>\n<p><strong>\u4ee3\u7801<\/strong><\/p>\n<pre><code class=\"line-numbers\">#include &lt;bits\/stdc++.h&gt;\n\nusing namespace std;\n\n\/\/ \u5224\u65ad\u5b57\u7b26\u4e32\u662f\u5426\u7b26\u5408\u7279\u5b9a\u6761\u4ef6\nint judge(string s) {\n    \/\/ \u68c0\u67e5\u5b57\u7b26\u4e32\u662f\u5426\u5305\u542b '#' \u6216 '*'\uff0c\u5e76\u4e14\u5305\u542b\u6570\u5b57\u548c\u5b57\u6bcd\n    if ((s.find(\"#\") != string::npos || s.find(\"*\") != string::npos) &amp;&amp; \n        any_of(s.begin(), s.end(), ::isdigit) &amp;&amp; \n        any_of(s.begin(), s.end(), ::isalpha)) {\n\n        \/\/ \u68c0\u67e5\u5b57\u7b26\u4e32\u4e2d\u662f\u5426\u6709\u4efb\u4f55\u5b57\u7b26\u51fa\u73b0\u8d85\u8fc7\u4e24\u6b21\n        for (int i = 0; i &lt; s.size(); i++) {\n            if (count(s.begin(), s.end(), s[i]) &gt; 2)\n                return 1; \/\/ \u5982\u679c\u6709\u5b57\u7b26\u51fa\u73b0\u8d85\u8fc7\u4e24\u6b21\uff0c\u8fd4\u56de 1\n        }\n        return 2; \/\/ \u5982\u679c\u6ca1\u6709\u5b57\u7b26\u51fa\u73b0\u8d85\u8fc7\u4e24\u6b21\uff0c\u8fd4\u56de 2\n    } else {\n        return 0; \/\/ \u5982\u679c\u4e0d\u7b26\u5408\u5305\u542b '#' \u6216 '*'\uff0c\u6570\u5b57\u548c\u5b57\u6bcd\u7684\u6761\u4ef6\uff0c\u8fd4\u56de 0\n    }\n}\n\nint main(int argc, char const *argv[]) {\n    int n;\n    cin &gt;&gt; n; \/\/ \u8bfb\u53d6\u9700\u8981\u5224\u65ad\u7684\u5b57\u7b26\u4e32\u603b\u6570\n    string str;\n    while (n--) {\n        cin &gt;&gt; str; \/\/ \u8bfb\u53d6\u9700\u8981\u5224\u65ad\u7684\u5b57\u7b26\u4e32\n        cout &lt;&lt; judge(str) &lt;&lt; endl; \/\/ \u8f93\u51fa\u5224\u65ad\u7ed3\u679c\n    }\n    return 0;\n}\n\n<\/code><\/pre>\n<p>\u9898\u76ee\u6765\u6e90\uff1a<a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/sim.csp.thusaac.com\/contest\/35\/problem\/0\">TUOJ<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528c++11\u65b0\u7279\u6027\uff1aany_of 1.1\u51fd\u6570\u539f\u578b\uff1a template&lt;class InputIt, c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-58","post","type-post","status-publish","format-standard","hentry","category-c"],"_links":{"self":[{"href":"https:\/\/blog.notiq.cn\/index.php\/wp-json\/wp\/v2\/posts\/58","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.notiq.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.notiq.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.notiq.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.notiq.cn\/index.php\/wp-json\/wp\/v2\/comments?post=58"}],"version-history":[{"count":0,"href":"https:\/\/blog.notiq.cn\/index.php\/wp-json\/wp\/v2\/posts\/58\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.notiq.cn\/index.php\/wp-json\/wp\/v2\/media?parent=58"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.notiq.cn\/index.php\/wp-json\/wp\/v2\/categories?post=58"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.notiq.cn\/index.php\/wp-json\/wp\/v2\/tags?post=58"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}