codeforces gym 101164 K Cutting 字符串hash

Posted xjhz

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了codeforces gym 101164 K Cutting 字符串hash相关的知识,希望对你有一定的参考价值。

题意:给你两个字符串a,b,不区分大小写,将b分成三段,重新拼接,问是否能得到A;

思路:暴力枚举两个断点,然后check的时候需要字符串hash,O(1)复杂度N*N;

题目链接:传送门

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define LL long long
#define pi (4*atan(1.0))
#define eps 1e-8
#define bug(x)  cout<<"bug"<<x<<endl;
const int N=5e3+10,M=2e6+10,inf=1e9+10;
const LL INF=1e18+10,mod=1e9+7;

string a,b,c;
unsigned int ma[N][N],mc[N][N];
int check(int l1,int r1,int l2,int r2,int l3,int r3)
{
    int s=0,e=r1-l1;
    if(ma[s][e]!=mc[l1][r1])return 0;
    s=e+1,e=s+r2-l2;
    if(ma[s][e]!=mc[l2][r2])return 0;
    s=e+1,e=s+r3-l3;
    if(ma[s][e]!=mc[l3][r3])return 0;
    return 1;
}
void output(int l1,int r1,int l2,int r2,int l3,int r3)
{
    for(int i=l1;i<=r1;i++)
        printf("%c",b[i]);
    printf("\n");
    for(int i=l2;i<=r2;i++)
        printf("%c",b[i]);
    printf("\n");
    for(int i=l3;i<=r3;i++)
        printf("%c",b[i]);
    printf("\n");
}
int main()
{
    cin>>a>>b;
    int n=a.size();c="";
    for(int i=0;i<n;i++)
    if(a[i]>=A&&a[i]<=Z)a[i]=a[i]-A+a;
    for(int i=0;i<n;i++)
    if(b[i]>=A&&b[i]<=Z)c+=b[i]-A+a;
    else c+=b[i];
    int tot=0;
    for(int i=0;i<n;i++)
    {
        unsigned int seed = 131; // 31 131 1313 13131 131313 etc..
        unsigned int has= 0;
        for(int j=i;j<n;j++)
        {
            has=has*seed+(a[j]);
            ma[i][j]=(has & 0x7FFFFFFF);
        }
    }
    for(int i=0;i<n;i++)
    {
        unsigned int seed = 131; // 31 131 1313 13131 131313 etc..
        unsigned int has= 0;
        for(int j=i;j<n;j++)
        {
            has=has*seed+(c[j]);
            mc[i][j]=(has & 0x7FFFFFFF);
        }
    }
    for(int i=1;i<=n-2;i++)
    {
        for(int j=i;j<=n-2;j++)
        {
            if(check(0,i-1,i,j,j+1,n-1))
            {
                printf("YES\n");
                output(0,i-1,i,j,j+1,n-1);
                return 0;
            }
            if(check(0,i-1,j+1,n-1,i,j))
            {
                printf("YES\n");
                output(0,i-1,j+1,n-1,i,j);
                return 0;
            }
            if(check(i,j,0,i-1,j+1,n-1))
            {
                printf("YES\n");
                output(i,j,0,i-1,j+1,n-1);
                return 0;;
            }
            if(check(i,j,j+1,n-1,0,i-1))
            {
                printf("YES\n");
                output(i,j,j+1,n-1,0,i-1);
                return 0;;
            }
            if(check(j+1,n-1,0,i-1,i,j))
            {
                printf("YES\n");
                output(j+1,n-1,0,i-1,i,j);
                return 0;
            }
            if(check(j+1,n-1,i,j,0,i-1))
            {
                printf("YES\n");
                output(j+1,n-1,i,j,0,i-1);
                return 0;;
            }
        }
    }
    printf("NO\n");
    return 0;
}

 

以上是关于codeforces gym 101164 K Cutting 字符串hash的主要内容,如果未能解决你的问题,请参考以下文章

codeforces gym 100548f

模拟NEERC15 G Generators(2015-2016 ACM-ICPC)(Codeforces GYM 100851)

codeforces gym 100357 H (DP 高精度)

codeforces gym 100357 K (表达式 模拟)

Codeforces Gym101234G Dreamoon and NightMarket(优先队列,子集和第k大)

codeforces gym 102268 300iq round