{"id":1408,"date":"2024-02-17T23:13:14","date_gmt":"2024-02-17T15:13:14","guid":{"rendered":"http:\/\/www.forillusion.com\/?p=1408"},"modified":"2025-02-14T11:39:10","modified_gmt":"2025-02-14T03:39:10","slug":"winter-algorithm-training2-string-algorithm","status":"publish","type":"post","link":"https:\/\/www.forillusion.com\/index.php\/winter-algorithm-training2-string-algorithm\/","title":{"rendered":"\u5bd2\u5047\u7b97\u6cd5\u8bad\u7ec3\uff08\u4e8c\uff09\u5b57\u7b26\u4e32\u7b97\u6cd5"},"content":{"rendered":"\n<p><div class=\"has-toc have-toc\"><\/div><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">#Algo0201. \u3010\u6a21\u677f\u3011\u5b57\u7b26\u4e32\u54c8\u5e0c<\/h2>\n\n\n\n<p><a href=\"https:\/\/hydro.ac\/d\/ahuacm\/p\/Algo0201\" target=\"_blank\" rel=\"noreferrer noopener\" rel=\"nofollow\" >\u9898\u76ee\u63cf\u8ff0<\/a><\/p>\n\n\n\n<p>\u9898\u76ee\u9700\u8981\u6c42\u51fa\u7ed9\u5b9a\u5b57\u7b26\u4e32\u4e2d\u662f\u5426\u6709\u91cd\u590d\u7684\uff0c\u5229\u7528\u6876\u5b58\u50a8\u5b57\u7b26\u4e32\u7684Hash\uff0c\u5e76\u5224\u65ad\u8be5Hash\u662f\u5426\u5b58\u5728\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>char s&#91;2001];\nint n,ans;\nconst int N=100005;\nint a&#91;43961944];\n\nstruct Hash\n{\n    const int Mod1=43961944,u1=131;\n    int h1&#91;N],b1&#91;N];\n    void build(char *s)\n    {\n        b1&#91;0]=1;\n        for (int i=1;i&lt;=strlen(s+1);i++)\n        {\n            b1&#91;i]=b1&#91;i-1]*u1%Mod1;\n            h1&#91;i]=(h1&#91;i-1]*u1+s&#91;i]-48)%Mod1;\n        }\n    }\n\n    int getHash(int l,int r)\n    {\n        return (h1&#91;r]-h1&#91;l-1]*b1&#91;r-l+1]%Mod1+Mod1)%Mod1;\n    }\n}Hash;\n\nint main()\n{\n    CI n;\n    F(i,1,n)\n    {\n        CI (s+1);\n        Hash.build(s);\n        int h=Hash.getHash(1,strlen(s+1));\n        if (a&#91;h]==0) \n        {\n            a&#91;h]=1;\n            ans++;\n        }\n    }\n    CO ans L;    \n    return 0;\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">#Algo0202. \u3010\u6a21\u677f\u3011KMP\u5b57\u7b26\u4e32\u5339\u914d<\/h2>\n\n\n\n<p><a href=\"https:\/\/hydro.ac\/d\/ahuacm\/p\/Algo0202\" target=\"_blank\" rel=\"noreferrer noopener\" rel=\"nofollow\" >\u9898\u76ee\u63cf\u8ff0<\/a><\/p>\n\n\n\n<p>\u672c\u9898\u5229\u7528KMP\u7b97\u6cd5\uff0c\u8f93\u51fa\u6a21\u5f0f\u4e32\u5339\u914d\u7684\u4f4d\u7f6e\uff0c\u5e76\u5728\u7ed3\u5c3e\u6253\u5370next\u6570\u7ec4\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int n,m,nxt&#91;1000006];\r\nchar a&#91;1000006],b&#91;1000006];\r\nint main()\r\n{\r\n    \/\/ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);\r\n    CI (a+1)>>(b+1);\r\n    n=strlen(a+1);\r\n    m=strlen(b+1);\r\n    nxt&#91;1]=0;\r\n    int j=0;\r\n    for (int i=2;i&lt;=m;i++)\r\n    {\r\n        while (b&#91;i]!=b&#91;j+1]&amp;&amp;j>0)\r\n            j=nxt&#91;j];\r\n        if (b&#91;i]==b&#91;j+1])\r\n        {\r\n            j++;\r\n            nxt&#91;i]=j;\r\n        }\r\n    }\r\n\r\n    j=0;\r\n    for (int i=0;i&lt;n;i++)\r\n    {\r\n        while (a&#91;i+1]!=b&#91;j+1]&amp;&amp;j>0)\r\n            j=nxt&#91;j];\r\n        if (a&#91;i+1]==b&#91;j+1]) j++;\r\n        if (j==m) cout&lt;&lt;i+1-m+1&lt;&lt;endl;\r\n    }\r\n    for (int i=1;i&lt;=m;i++)\r\n        cout&lt;&lt;nxt&#91;i]&lt;&lt;\" \";\r\n    cout&lt;&lt;endl;\r\n    return 0;\r\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">#Algo0203. Censoring S<\/h2>\n\n\n\n<p><a href=\"https:\/\/hydro.ac\/d\/ahuacm\/p\/Algo0203\" target=\"_blank\" rel=\"noreferrer noopener\" rel=\"nofollow\" >\u9898\u76ee\u63cf\u8ff0<\/a><\/p>\n\n\n\n<p>\u672c\u9898\u5229\u7528KMP\u67e5\u627e\u6a21\u5f0f\u4e32\u7684\u540c\u65f6\uff0c\u7528\u4e00\u4e2a\u6808\u6765\u8bb0\u5f55\u7b54\u6848\uff0c\u5e76\u7528top\u6307\u9488\u6307\u5411\u6808\u9876\uff0c\u540c\u65f6\u7528\u4e00\u4e2a\u6808f\u6765\u8bb0\u5f55\u5b57\u7b26\u4e32\u6bcf\u4f4d\u7684j\u6307\u9488\u4f4d\u7f6e\u3002\u5f53\u5339\u914d\u5230\u6a21\u5f0f\u4e32\u540e\uff0ctop\u6570\u7ec4\u540e\u79fb\u6a21\u5f0f\u4e32\u7684\u4f4d\u6570\uff0c\u540c\u65f6\u5c06j\u6307\u9488\u8c03\u6574\u5230f\u6808\u6808\u9876\u8bb0\u5f55\u7684\u4f4d\u7f6e\u5904\u3002\u8fd9\u6837\u5c31\u53ef\u4ee5\u5728\u5339\u914d\u5230\u4e00\u4e2a\u5b57\u7b26\u4e32\u540e\uff0c\u5c06j\u6307\u9488\u8fd8\u539f\u5230\u4e0a\u4e00\u6b21\u5339\u914d\u7684\u60c5\u51b5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int n,m,nxt&#91;1000006],f&#91;1000006];\r\nchar a&#91;1000006],b&#91;1000006],c&#91;1000006];\r\n\r\nint main()\r\n{\r\n    \/\/ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);\r\n    CI (a+1)>>(b+1);\r\n    n=strlen(a+1);\r\n    m=strlen(b+1);\r\n    nxt&#91;1]=0;\r\n    int j=0;\r\n    for (int i=2;i&lt;=m;i++)\r\n    {\r\n        while (b&#91;i]!=b&#91;j+1]&amp;&amp;j>0)\r\n            j=nxt&#91;j];\r\n        if (b&#91;i]==b&#91;j+1])\r\n        {\r\n            j++;\r\n            nxt&#91;i]=j;\r\n        }\r\n    }\r\n\r\n    int top=0;\r\n    for (int i=0;i&lt;n;i++)\r\n    {\r\n        c&#91;++top]=a&#91;i+1];\r\n        \/\/ cout&lt;&lt;top&lt;&lt;c&#91;top]&lt;&lt;endl;\r\n        while ((a&#91;i+1]!=b&#91;j+1])&amp;&amp;j>0)\r\n            j=nxt&#91;j];\r\n\r\n        if (a&#91;i+1]==b&#91;j+1]) j++;\r\n        f&#91;top]=j;\r\n        if (j==m) top-=m,j=f&#91;top];\r\n    }\r\n    c&#91;++top]='\\0';\r\n    cout&lt;&lt;(c+1)&lt;&lt;endl;\r\n    return 0;\r\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">#Algo0204. \u65e0\u7ebf\u4f20\u8f93<\/h2>\n\n\n\n<p><a href=\"https:\/\/hydro.ac\/d\/ahuacm\/p\/Algo0204\" target=\"_blank\" rel=\"noreferrer noopener\" rel=\"nofollow\" >\u9898\u76ee\u63cf\u8ff0<\/a><\/p>\n\n\n\n<p>\u5b57\u7b26\u4e32\u6709\u4e00\u4e2a\u5faa\u73af\u8282\u6784\u6210\uff0c\u6c42\u5faa\u73af\u8282\u7684\u6700\u5c0f\u957f\u5ea6\u3002\u8f93\u51fa\u8fd9\u4e2a\u5b57\u7b26\u4e32\u7684next\u6570\u7ec4\uff0c\u4e0d\u96be\u53d1\u73b0\uff0c\u5f53\u5faa\u73af\u8282\u5f00\u59cb\u5faa\u73af\u65f6\uff0cnext\u503c\u4ece1\u9010\u4e2a\u589e\u52a0\uff0c\u5e76\u5728\u672b\u5c3e\u5904\uff0cnext\u503c\u8868\u793a\u8be5\u6570\u7ec4\u5faa\u73af\u4e86\u591a\u5c11\u4f4d\u3002\u5c06\u5b57\u7b26\u4e32\u603b\u957f\u5ea6\u51cf\u53bb\u6700\u540e\u4e00\u4f4d\u7684next\u5c31\u80fd\u5f97\u5230\u5faa\u73af\u8282\u7684\u957f\u5ea6\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int n,m,nxt&#91;1000006],ans=0;\r\nchar b&#91;1000006];\r\nint main()\r\n{\r\n    \/\/ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);\r\n    CI m;\r\n    CI (b+1);\r\n    m=strlen(b+1);\r\n    nxt&#91;1]=0;\r\n    int j=0;\r\n    for (int i=2;i&lt;=m;i++)\r\n    {\r\n        while (b&#91;i]!=b&#91;j+1]&amp;&amp;j>0)\r\n            j=nxt&#91;j];\r\n        if (b&#91;i]==b&#91;j+1])\r\n        {\r\n            j++;\r\n            nxt&#91;i]=j;\r\n        }\r\n    }\r\n\r\n    cout&lt;&lt;m-nxt&#91;m]&lt;&lt;endl;\r\n    return 0;\r\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>#Algo0201. \u3010\u6a21\u677f\u3011\u5b57\u7b26\u4e32\u54c8\u5e0c \u9898\u76ee\u63cf\u8ff0 \u9898\u76ee\u9700\u8981\u6c42\u51fa\u7ed9\u5b9a\u5b57\u7b26\u4e32\u4e2d\u662f\u5426\u6709\u91cd\u590d\u7684\uff0c\u5229\u7528\u6876\u5b58\u50a8\u5b57\u7b26\u4e32\u7684Hash\uff0c\u5e76\u5224\u65ad\u8be5Ha &#8230;<\/p>","protected":false},"author":1,"featured_media":1411,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,35],"tags":[12,34,22,39],"class_list":["post-1408","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-3","category-35","tag-12","tag-34","tag-22","tag-39"],"_links":{"self":[{"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/posts\/1408","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/comments?post=1408"}],"version-history":[{"count":1,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/posts\/1408\/revisions"}],"predecessor-version":[{"id":1727,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/posts\/1408\/revisions\/1727"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/media\/1411"}],"wp:attachment":[{"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/media?parent=1408"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/categories?post=1408"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/tags?post=1408"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}