Problem C
1 3 5 4 HENRY GAVIN MAGIC HENRY HGM HAG MAVIN
0 0 0 0 0 0 -1 -1
/*根据给的查询来建立字典树,然后在矩阵中看是否存在这个字符串*/
#include
#include
#include
#include
#include
using namespace std;
struct data
{
bool fg;
int num;
data *next[26];
} a[400000]; //由于多组case,每次动态分配内存后要释放内存很麻烦,我就用的数组的地址来分配给字典树的节点
data *root=&a[0];
int tot;
struct dat
{
int x,y;
};
const int zl[3][2]={0,1,1,1,1,0}; //方向增量,枚举三个方向
data *creat() //从数组中分配一个新的节点的地址,返回指针
{
data *p=&a[++tot];
p->fg=false;
for(int i=0;i<26;i++) p->next[i]=NULL;
p->num=0;
return p;
}
void insert(char *s,int k) //将序号为 k 的 s[] 这个单词插入字典树中
{
int len=strlen(s);
data *now=root;
for(int i=0;inext[p]==NULL) now->next[p]=creat();
now=now->next[p];
}
now->fg=true;
now->num=k;
}
int main()
{
int t,n,m,q;
scanf("%d",&t);
while(t--)
{
tot=0;
dat ans[10005];
memset(ans,-1,sizeof(ans)); //先假设都找不到
for(int i=0;i<26;i++) root->next[i]=NULL; //赋初值
root->fg=false;
root->num=0;
char s[601][601];
scanf("%d%d%d",&n,&m,&q); getchar(); //n行 m 列 q 个询问
for(int i=0;inext[p]==NULL) break;
now=now->next[p];
if(now->fg && ans[now->num].x==-1) //如果以前没有找到,当前的解就是最小的解
{
ans[now->num].x=i;
ans[now->num].y=j;
}
x+=zl[k][0]; y+=zl[k][1];
}
}
for(int i=1;i<=q;i++) //输出结果
printf("%d %d\n",ans[i].x,ans[i].y);
}
return 0;
}