• Gym 100500B Conference Room(最小表示法,哈希)


    #include <bits/stdc++.h>
    using namespace std;
    const int maxn=1005;
    const int base=37;
    typedef unsigned long long ULL;
    set<ULL>save;
    int n;
    int a[4][maxn],b[maxn];
    int id[maxn<<1];
    int ave;
    int MinmumRepresentation(int *s,int n){    //..............最小表示法。
        int i=0,j=1,k=0,t;
        while(i<n&&j<n&&k<n){
            t=s[(i+k)>=n?i+k-n:i+k]-s[(j+k)>=n?j+k-n:j+k];
            if(!t)k++;
            else {
                if(t>0)i=i+k+1;
                else j=j+k+1;
                if(i==j)++j;
                k=0;
            }
        }
        return(i<j?i:j);
    }
    ULL gethash(int *s,int n){
        int pos=MinmumRepresentation(s,n);
        ULL res=0;
        for(int i=0;i<n;i++)res=res*base+s[id[pos+i]];
        return res;
    }
    
    bool ok(int d){
        for(int j=0;j<n;j++){
            b[j]=ave-a[2][j]-a[3][id[j+d]];
        }
        return save.find(gethash(b,n))!=save.end();
    }
    int main(){
    //    freopen("in","r",stdin);
        int t,cas=1;
        cin>>t;
        while(t--){
            printf("Case %d: ",cas++);
            scanf("%d",&n);
            ULL sum=0;
            for(int i=0;i<4;i++)
            for(int j=0;j<n;j++){
                scanf("%d",a[i]+j);
                sum+=a[i][j];
            }
            if(sum%n){
                puts("No");
                continue;
            }
            ave=sum/n;
            save.clear();
            for(int i=0;i<2*n;i++)id[i]=i%n;
            for(int i=0;i<n;i++){
                for(int j=0;j<n;j++){
                    b[j]=a[0][j]+a[1][id[i+j]];
                }
                save.insert(gethash(b,n));
            }
            bool flag=0;
            for(int i=0;i<n;i++){
                if(ok(i)){
                    flag=1;
                    puts("Yes");
                    break;
                }
            }
            if(!flag)puts("No");
        }
        return 0;
    }
    

      

  • 相关阅读:
    173. Binary Search Tree Iterator
    199. Binary Tree Right Side View
    230. Kth Smallest Element in a BST
    236. Lowest Common Ancestor of a Binary Tree
    337. House Robber III
    449. Serialize and Deserialize BST
    508. Most Frequent Subtree Sum
    513. Find Bottom Left Tree Value
    129. Sum Root to Leaf Numbers
    652. Find Duplicate Subtrees
  • 原文地址:https://www.cnblogs.com/wshh/p/4521242.html
Copyright © 2020-2023  润新知