{"id":653,"date":"2023-10-03T17:51:46","date_gmt":"2023-10-03T09:51:46","guid":{"rendered":"http:\/\/ggapa.net:81\/?p=653"},"modified":"2023-10-15T00:18:13","modified_gmt":"2023-10-14T16:18:13","slug":"usaco17jan-hoof-paper-scissor-g-%e7%9a%84%e4%b8%a4%e7%a7%8d%e8%a7%a3%e6%b3%95%e4%bb%a5%e5%8f%8a%e5%9c%a8%e6%92%b0%e5%86%99c%e4%bb%a3%e7%a0%81%e6%97%b6%e7%9a%84%e6%b3%a8%e6%84%8f%e4%ba%8b","status":"publish","type":"post","link":"http:\/\/ggapa.net:81\/2023\/10\/03\/usaco17jan-hoof-paper-scissor-g-%e7%9a%84%e4%b8%a4%e7%a7%8d%e8%a7%a3%e6%b3%95%e4%bb%a5%e5%8f%8a%e5%9c%a8%e6%92%b0%e5%86%99c%e4%bb%a3%e7%a0%81%e6%97%b6%e7%9a%84%e6%b3%a8%e6%84%8f%e4%ba%8b\/","title":{"rendered":"[USACO17JAN] Hoof, Paper, Scissor G \u7684\u4e24\u79cd\u89e3\u6cd5\u4ee5\u53ca\u5728\u64b0\u5199C++\u4ee3\u7801\u65f6\u7684\u6ce8\u610f\u4e8b\u9879"},"content":{"rendered":"

\u9898\u76ee\u4f20\u9001\u95e8<\/a><\/p>\n

\u5728\u7f16\u5199\u4ee3\u7801\u7684\u65f6\u5019\u591a\u5199\u5199\u6ce8\u91ca\uff01\uff01
\n\u591a\u5199\u5341\u79d2\u949f\u6ce8\u91ca\u5c11\u8c03\u8bd5\u5341\u5206\u949f\u7a0b\u5e8f\uff01\uff01<\/p>\n

\u8bb0\u5fc6\u5316\u641c\u7d22<\/h1>\n
\/*\nproblem:[USACO17JAN] Hoof, Paper, Scissor G\nURL:https:\/\/www.luogu.com.cn\/problem\/P3609\ntime:2023-10-03\n\u8bb0\u5fc6\u5316\u641c\u7d22\n*\/\n#include <iostream>\n#include <vector>\nusing namespace std;\nconst int maxn = 1e5 + 5;\n\nint n, k;\nint a[maxn], F[maxn][22][3]; \/\/f[i][j][k] \u524d i \u8f6e\uff0c\u53d8\u6362\u4e86 j \u6b21\uff0c\u6700\u540e\u4e00\u6b21\u51fa\u7684 k\n\/\/ \u8fd8\u6709\u4e00\u4e2a\u5c0f\u6280\u5de7\uff0c\u6570\u7ec4\u540d\u7528\u5927\u5199\uff0c\u4e3a\u4e86\u65b9\u4fbf\u9700\u8981\u591a\u6b21\u64cd\u4f5c\u7684\u6570\u7ec4\u5143\u7d20\u7528\u5c0f\u5199\u5b57\u6bcd\u4ee3\u66ff\n\nint dfs(int x, int b, int w) { \/\/ \u7b2c x \u8f6e\uff0c\u8fd8\u53ef\u4ee5\u53d8\u6362 b \u6b21\uff0c\u4e0a\u4e00\u8f6e\u51fa\u7684 w\n    if(!x || b < 0) return 0; \/\/\u8fb9\u754c\u6761\u4ef6\u5224\u65ad\n    int &f = F[x][b][w], p = -1; \n    if(f) return f; \n    for(int i = 0; i <= 2; i++) \n        p = max(p, dfs((x - 1), b - (i != w), i)); \/\/\u5411\u524d\u641c\u7d22\uff0c\u8ba1\u7b97\u9700\u4e0d\u9700\u8981\u6d88\u8017\u6b21\u6570\n    return f = p + (w == a[x]); \/\/\u8fd4\u56de\u80fd\u5426\u80dc\u5229\uff0c\u5e76\u4e14\u8d4b\u503c\n}\n\nint main() {\n    ios::sync_with_stdio(0); \/\/ IO \u4f18\u5316\n    cin.tie(0);\n    cout.tie(0);\n    cin >> n >> k;\n    string s = "PHS"; \/\/\u5c06\u5b57\u7b26\u8f6c\u5316\u4e3a int\uff0c\u65b9\u4fbf\u64cd\u4f5c\n    char c;\n    int ans = 0;\n    for(int i = 1; i <= n; i++) {\n        cin >> c;\n        a[i] = s.find(c); \/\/\u5b57\u7b26\u4e32 s \u4e2d\u7684\u5b57\u7b26\u5f88\u5c11\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u4ee5\u770b\u4f5c\u5e38\u6570\n    }\n    for(int j = 0; j <= 2; j++) ans = max(ans, dfs(n, k, j));\n    cout << ans << "\\n"; \/\/\u5efa\u8bae\u4e0d\u8981\u4f7f\u7528 endl \u5bb9\u6613\u51fa\u9505\n    return 0; \/\/\u534e\u4e3d\u6536\u573a\n}\n\/*\n\u7f16\u7a0b\u8bed\u8a00\nC++14 (GCC 9)\n\u4ee3\u7801\u957f\u5ea6\n1.05KB\n\u7528\u65f6\n415ms\n\u5185\u5b58\n32.14MB\n*\/<\/code><\/pre>\n

\u52a8\u6001\u89c4\u5212 + \u6eda\u52a8\u6570\u7ec4<\/h1>\n
\/*\nproblem:[USACO17JAN] Hoof, Paper, Scissor G\nURL:https:\/\/www.luogu.com.cn\/problem\/P3609\ndate:2023-10-03\n\u52a8\u6001\u89c4\u5212+\u6eda\u52a8\u6570\u7ec4\n*\/\n\n#include <iostream>\n#include <algorithm>\nusing namespace std;\n\nint n, k;\nint D[2][24][5]; \/\/ D[i][j][k] i \u4f5c\u4e3a\u6eda\u52a8\u6570\u7ec4\u6765\u4f7f\u7528\uff0cj \u4ee3\u8868\u6362\u4e86\u591a\u5c11\u6b21\u624b\u52bf\uff0ck \u4ee3\u8868\u4e0a\u4e00\u6b21\u51fa\u7684\u624b\u52bf\u662f\u4ec0\u4e48\n\nint main() {\n    ios::sync_with_stdio(0);\n    cin.tie(0);\n    cout.tie(0);\n    cin >> n >> k;\n    char ch;\n    string s = "HPS"; \/\/\u8bb0\u5f55\u4e00\u4e0b\uff0c\u65b9\u4fbf char \u8f6c\u6362\u4e3a int \n    for(int i = 1; i <= n; i++) {\n        cin >> ch; \/\/ 0 \u514b\u5236 1\uff0c1 \u514b\u5236 2\uff0c2 \u514b\u5236 0\n        int ci = i % 2, i1 = ci ^ 1;\/\/ci \u4ee3\u8868\u7684\u662f\u6570\u7ec4 D \u4f7f\u7528\u5230\u7684\u5c42\u6570\uff0c\u800c i1 \u4ee3\u8868\u7684\u662f\u4e0a\u4e00\u5c42\n        for(int j = 0; j <= k; j++) {\n            for(int q = 0; q <= 2; q++) {\n                int q1 = (q + 1) % 3, q2 = (q + 2) % 3, &d = D[ci][j][q];\n                \/\/\u4e0a\u9762\u90a3\u4e00\u884c q q1 q2 \u4ee3\u8868\u7684\u662f \u5176\u4ed6\u53ef\u80fd\u4f1a\u51fa\u7684\u624b\u52bf\n                if (q1 == (int)s.find(ch))\n                    d = max({D[i1][j][q], D[i1][j + 1][q1], D[i1][j + 1][q2]}) + 1; \/\/\u4e0d\u8981\u50bb\u4e4e\u4e4e\u7684\u5d4c\u5957 max\uff0c \u7528\u4e00\u4e2a {} \u5c31\u8db3\u591f\u4e86\n                else \/\/\u5982\u679c\u8f93\u4e86\n                    d = D[i1][j][q];\n            }\n        }\n    }\n    int ci = n % 2; \/\/\u786e\u5b9a\u73b0\u5728\u662f\u54ea\u4e00\u5c42\n    cout << max({D[ci][0][0], D[ci][0][1], D[ci][0][2]}); \/\/ max \u8981\u7528 algorithm \u5934\u6587\u4ef6\n    return 0; \/\/\u5b8c\u7f8e\u6536\u573a\n}\n\/*\n\u7f16\u7a0b\u8bed\u8a00\nC++14 (GCC 9)\n\u4ee3\u7801\u957f\u5ea6\n1.16KB\n\u7528\u65f6\n266ms\n\u5185\u5b58\n880.00KB\n*\/<\/code><\/pre>\n

\u603b\u7ed3<\/h1>\n

\u53ef\u4ee5\u5f88\u660e\u663e\u7684\u770b\u51fa\u6765\uff0c\u65b9\u6cd5\u4e8c\u7684\u5185\u5b58\u662f\u8fdc\u5c0f\u4e8e\u65b9\u6cd5\u4e00\u7684\u3002
\n\u5728\u5927\u578b\u8003\u8bd5\u7684\u65f6\u5019\u5f00\u5f88\u5927\u7684\u5185\u5b58\u5bb9\u6613\u5f71\u54cd\u8001\u7237\u673a\u7684\u5fc3\u60c5\uff0c\u5bfc\u81f4 TLE
\n\u6240\u4ee5\u8bf4\u5982\u679c\u611f\u89c9\u7a7a\u95f4\u4f1a\u5f88\u5927\u800c\u4e14\u62c5\u5fc3\u8d85\u65f6\u7684\u60c5\u51b5\u4e0b\u8fd8\u662f\u9009\u62e9\u6eda\u52a8\u6570\u7ec4\u5427qaq<\/p>\n","protected":false},"excerpt":{"rendered":"

\u9898\u76ee\u4f20\u9001\u95e8 \u5728\u7f16\u5199\u4ee3\u7801\u7684\u65f6\u5019\u591a\u5199\u5199\u6ce8\u91ca\uff01\uff01 \u591a\u5199\u5341\u79d2\u949f\u6ce8\u91ca\u5c11\u8c03\u8bd5\u5341\u5206\u949f\u7a0b\u5e8f\uff01\uff01 \u8bb0\u5fc6\u5316\u641c\u7d22 \/* proble […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"_links":{"self":[{"href":"http:\/\/ggapa.net:81\/wp-json\/wp\/v2\/posts\/653"}],"collection":[{"href":"http:\/\/ggapa.net:81\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/ggapa.net:81\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/ggapa.net:81\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/ggapa.net:81\/wp-json\/wp\/v2\/comments?post=653"}],"version-history":[{"count":1,"href":"http:\/\/ggapa.net:81\/wp-json\/wp\/v2\/posts\/653\/revisions"}],"predecessor-version":[{"id":654,"href":"http:\/\/ggapa.net:81\/wp-json\/wp\/v2\/posts\/653\/revisions\/654"}],"wp:attachment":[{"href":"http:\/\/ggapa.net:81\/wp-json\/wp\/v2\/media?parent=653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ggapa.net:81\/wp-json\/wp\/v2\/categories?post=653"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ggapa.net:81\/wp-json\/wp\/v2\/tags?post=653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}