# sgu392Cyclic Troubles

## 题意：

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

## tip：

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)
}
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];
//                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;
}
}
}
}

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;
}

#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;
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)
}
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];
//                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;
cnt = 0;flag = 0;
}
else if(flag && r[i] >= 'a' && r[i] <= 'z'){
cnt++;
//
}
else if(r[i] >= 'a' && r[i] <= 'z'){
sum++;
}
}
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;
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
*/