• 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("
    ");
        for(int i=l2;i<=r2;i++)
            printf("%c",b[i]);
        printf("
    ");
        for(int i=l3;i<=r3;i++)
            printf("%c",b[i]);
        printf("
    ");
    }
    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
    ");
                    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
    ");
                    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
    ");
                    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
    ");
                    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
    ");
                    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
    ");
                    output(j+1,n-1,i,j,0,i-1);
                    return 0;;
                }
            }
        }
        printf("NO
    ");
        return 0;
    }
  • 相关阅读:
    hdu 3006 The Number of set(思维+壮压DP)
    Mysql-SQL优化-统计某种类型的个数
    canvas.clipPath canvas.clipRect() 无效的原因
    linux下alias命令具体解释
    使用带粒子效果的 CAEmitterLayer
    Wordpress 建站(一)
    一个有趣的问题:ls -l显示的内容中total究竟是什么?
    (转)奇妙的数据挖掘
    android几个高速打包命令
    hdu3336解读KMP算法的next数组
  • 原文地址:https://www.cnblogs.com/jhz033/p/7231272.html
Copyright © 2020-2023  润新知