sgu392Cyclic Troubles

版权声明:转我原创记得说你是我的脑残粉哟 https://blog.csdn.net/zjy2015302395/article/details/77916204

题意:

n*m矩阵,每个点固定可以往左往右往上或往下走,每个点有个字母,
q个循环问会不会出现出现给定的串,其中(s)num表示s串循环了多少次

tip:

预处理每个点往后走1,2,4,7,16。。。步走到哪,,类似倍增的思想,(询问总长度就1e9,说明只有往后走2^31就够了)

void get_Hash(){
    for(int i = 0 ; i <= n+1 ; i++)
        for(int j = 0 ; j <= m+1 ; j++){
            get_dir(i,j,nexti,nextj);
            st[i][j][0] = make_pair(nexti,nextj);
            if(nexti <= 0 || nexti > n || nextj <= 0 || nextj > m)
                Hash[i][j][0] = 26+Y_radix;
            else    Hash[i][j][0] = a[nexti-1][nextj-1]-'a'+Y_radix;
        }
    for(int step = 1; step < 32 ; step++){
        for(int i = 0 ;i <= n+1 ; i++){
            for(int j =  0; j <= m+1 ; j++){
                pii next = st[i][j][step-1];
                st[i][j][step] = st[next.first][next.second][step-1];
                Hash[i][j][step] = Hash[i][j][step-1]*quick_pow(X_radix,quick_pow(2,step-1))+Hash[next.first][next.second][step-1];
//                printf("st[%d][%d][%d] = (%d ,%d)\n",i,j,step,st[i][j][step].first,st[i][j][step].second);
//                cout <<"Hash: "<<Hash[i][j][step]<<endl;
            }
        }
    }
}

再处理出询问串的长度,还有hash值

uii qur_Hash(int i,int j,int lenth){
    int len = lenth,cnt = 0,pos = 0,nowi = i,nowj = j,pnow = 0;
    uii tmp_Hash = 0;
    while(lenth){
        if(lenth & 1) w[cnt++] = pos;
        pos++;
        lenth >>= 1;
    }
    for(int k = cnt-1; k >= 0 ; k--){
        nexti = st[nowi][nowj][w[k]].first;
        nextj = st[nowi][nowj][w[k]].second;
        tmp_Hash = tmp_Hash *  quick_pow(X_radix,quick_pow(2,w[k]))+ Hash[nowi][nowj][w[k]];
        nowi = nexti,nowj = nextj;
    }
    return tmp_Hash;
}

枚举一遍已每个点为起点走这么长的hash值是否和刚刚处理的一样就可以了

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
typedef pair<int,int>pii;
typedef unsigned long long uii;
typedef pair<int,uii>piu;
const int maxn = 35;
const uii none = 0;
const uii X_radix = 1000000007;
const uii Y_radix = 1;
char s[maxn][maxn],a[maxn][maxn],r[2010];
uii Hash[maxn][maxn][32];
pii st[maxn][maxn][32];
int nexti,nextj,n,m,q;

void init(){
    scanf("%d%d",&n,&m);
    for(int i = 0 ; i < n ; i++)
        scanf("%s",s[i]);
    for(int i = 0 ; i < n ; i++)
        scanf("%s",a[i]);
}
void get_dir(int i,int j,int &nexti,int &nextj){
    if(i == 0 || i == n+1 || j == 0|| j == m+1){
        nexti = i,nextj = j;
        return ;
    }
    if(s[i-1][j-1] == '>')  nexti = i,   nextj = j+1;
    if(s[i-1][j-1] == '<')  nexti = i,   nextj = j-1;
    if(s[i-1][j-1] == 'v')  nexti = i+1, nextj = j;
    if(s[i-1][j-1] == '^')  nexti = i-1, nextj = j;
}

unsigned long long quick_pow(unsigned long long a,int b){
    unsigned long long ans = 1;
    while(b > 0){
        if(b&1) ans *= a;
        a *= a;
        b >>= 1;
    }
    //cout <<"ans = "<<ans <<endl;
    return ans;
}
uii sum_pow(uii a,int b){
    if(b == 0)  return 0;
    if(b == 1)  return 1;
    if(b % 2 == 0){
        uii ret = sum_pow(a,b/2);
        return ret * (quick_pow(a,b/2) + 1);
    }
    else{
        uii ret = sum_pow(a,b-1);
        return ret * a + 1;
    }
}
void get_Hash(){
    for(int i = 0 ; i <= n+1 ; i++)
        for(int j = 0 ; j <= m+1 ; j++){
            get_dir(i,j,nexti,nextj);
            st[i][j][0] = make_pair(nexti,nextj);
            if(nexti <= 0 || nexti > n || nextj <= 0 || nextj > m)
                Hash[i][j][0] = 26+Y_radix;
            else    Hash[i][j][0] = a[nexti-1][nextj-1]-'a'+Y_radix;
        }
    for(int step = 1; step < 32 ; step++){
        for(int i = 0 ;i <= n+1 ; i++){
            for(int j =  0; j <= m+1 ; j++){
                pii next = st[i][j][step-1];
                st[i][j][step] = st[next.first][next.second][step-1];
                Hash[i][j][step] = Hash[i][j][step-1]*quick_pow(X_radix,quick_pow(2,step-1))+Hash[next.first][next.second][step-1];
//                printf("st[%d][%d][%d] = (%d ,%d)\n",i,j,step,st[i][j][step].first,st[i][j][step].second);
//                cout <<"Hash: "<<Hash[i][j][step]<<endl;
            }
        }
    }
}
int find_num(int i,int len){
    int ans = 0;
    for(int j = i+1; j < len ; j++){
        if(r[j] >= '0' && r[j] <= '9')
            ans = ans*10+r[j]-'0';
        else    break;
    }
    return ans;
}
piu pre(){
    int len = strlen(r);
    int sum = 0,cnt = 0,flag = 0;
    uii ans_Hash = 0,res_Hash = 0;
    for(int i = 0 ; i < len; i++){
        if(r[i] == '('){
            cnt = 0;flag = 1;res_Hash = 0;
        }
        else if(r[i] == ')'){
            int num = find_num(i,len);
            sum += num*cnt;
            ans_Hash = ans_Hash * quick_pow(X_radix,cnt*num) + res_Hash * sum_pow(quick_pow(X_radix,cnt),num);
            cnt = 0;flag = 0;
        }
        else if(flag && r[i] >= 'a' && r[i] <= 'z'){
            cnt++;
            res_Hash = res_Hash *X_radix + r[i]-'a'+Y_radix;
            //
        }
        else if(r[i] >= 'a' && r[i] <= 'z'){
            sum++;
            ans_Hash = ans_Hash * X_radix + r[i]-'a'+Y_radix;
        }
    }
    return make_pair(sum,ans_Hash);
}
int w[35];
uii qur_Hash(int i,int j,int lenth){
    int len = lenth,cnt = 0,pos = 0,nowi = i,nowj = j,pnow = 0;
    uii tmp_Hash = 0;
    while(lenth){
        if(lenth & 1) w[cnt++] = pos;
        pos++;
        lenth >>= 1;
    }
    for(int k = cnt-1; k >= 0 ; k--){
        nexti = st[nowi][nowj][w[k]].first;
        nextj = st[nowi][nowj][w[k]].second;
        tmp_Hash = tmp_Hash *  quick_pow(X_radix,quick_pow(2,w[k]))+ Hash[nowi][nowj][w[k]];
        nowi = nexti,nowj = nextj;
    }
    return tmp_Hash;
}
void sov(){
    scanf("%d",&q);
    while(q--){
        scanf("%s",r);
        piu tmp = pre();
        //cout <<tmp.first <<"  "<< tmp.second<<endl;
        int lenth = tmp.first,flag = 0;
        uii to_ans = tmp.second;

        for(int i = 1 ; i <= n ; i++){
            for(int j = 1 ; j  <= m ; j++){
                //if(i != 0 || j != 1)    continue;
                //cout <<qur_Hash(i,j,lenth-1)<<endl;
                //cout <<(a[i][j]-'a'+Y_radix)*quick_pow(X_radix,lenth-1)<<endl;
                if( (uii)(a[i-1][j-1]-'a'+Y_radix)*quick_pow(X_radix,lenth-1)+qur_Hash(i,j,lenth-1) == to_ans){
                    printf("YES (%d,%d)\n",i,j);
                    flag = 1;
                    break;
                }
            }
            if(flag)    break;
        }
        if(flag == 0)   printf("NO\n");
    }
}
int main(){

    init();
    get_Hash();
    sov();
}
/*
2 4
>>>v
<^<<
abcd
efgh
6
abcdhgf
bcdhgf
(bcdhgf)100
a(bcdhgf)100bc
b(cdhgfbc)1d
hello
*/
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页