UESTC Training for Data Structures——C

Problem  C

Problem Description

  Recently,our God Wu invented a little game named "God Wu's Puzzle" . If you dont know it, what a pity !
In the game, God Wu will give you a rectangular grid of letters, in which several words are hidden. Each word may begin anywhere in the puzzle, and may be oriented in any straight line horizontally, vertically, or diagonally. However, the words must all go down, right, or down-right. A dictionary is also given to you, indicating the words to be found in the grid.
  You task is to find the locations of each word within the grid.If you cannot do it,God Wu will give you some color to see see!

Input

The first line is a single integer T,the number of test cases.(T<=25)
For each of the test cases:
The first line is three integers R  C and M separated by whitespaces. R (20 ≤ R ≤ 500) is the number of rows of the grid. C (20 ≤ C ≤ 500) is the number of columns of the grid.M(M<=10000) is the number of words to be found.
The following R lines, each line will contains exactly C characters without anything else. Each character is in the range 'A' - 'Z'.
The following M lines, each line contains a unique word in the dictionary. Each word will contain between 1 and 20 characters ( also in the range 'A' - 'Z').

Output

For each word, output the "ROW COL"(quotes for clarity) pair, where ROW is the 0-based row in which the first letter of the word is found, and COL is the 0-based column in which the first letter of the word is found. If the same word can be found more than once, the location in the lowest-indexed row should be returned. If there is still a tie, return the location with the lowest-indexed column. If a word cannot be found in the grid, return "-1 -1" for the word.

Sample Input

1
3 5 4
HENRY
GAVIN
MAGIC
HENRY
HGM
HAG
MAVIN

Sample Output

0 0
0 0
0 0
-1 -1

 

/*根据给的查询来建立字典树,然后在矩阵中看是否存在这个字符串*/

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
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;i<len;i++)
    {
        int p=s[i]-'A';
        if(now->next[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;i<n;i++) gets(s[i]);
        for(int i=1;i<=q;i++)
        {
            gets(s[600]);
            insert(s[600],i);
        }
        for(int i=0;i<n;i++)  //行
          for(int j=0;j<m;j++)  //列
            for(int k=0;k<3;k++)  //枚举三个方向
            {
                data *now=root;
                int x=i,y=j;
                while(x<n && y<m)
                {
                    int p=s[x][y]-'A';
                    if(now->next[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;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
互联网络程序设计是指在互联网上进行程序开发和设计的过程。UESTC则是我国的一所著名高校——电子科技大学。 互联网络程序设计 uestc包含了两个主要的方面:互联网络和程序设计。互联网络是指将多个计算机网络通过通信链路互相连接起来,实现信息共享和资源共享的网络系统。程序设计是指根据需求和目标,通过编写代码和设计算法,实现计算机程序的过程。 互联网络程序设计 uestc的学习内容主要包括以下几个方面: 1. 网络知识:学习互联网络的基本概念、原理和协议,如TCP/IP协议、HTTP协议等。掌握网络编程的基本技术,能够编写网络应用程序。 2. 数据通信:学习数据通信的基本原理和技术,包括数据传输的方式、数据压缩和加密等。了解网络安全和数据保护的基本知识。 3. 程序设计:学习编程语言和开发工具,如Java、C++和Python等。掌握常用的编程技巧和方法,能够设计和实现复杂的网络应用程序。 4. Web开发:学习Web开发的基本知识和技术,包括HTML、CSS、JavaScript等。能够设计和实现交互式的Web应用程序。 5. 数据库技术:学习数据库的基本原理和技术,如SQL语言和数据库管理系统。能够设计和管理数据库,实现数据的存储和检索。 通过学习互联网络程序设计 uestc,可以掌握互联网应用开发的基本技能,具备设计和实现网络应用程序的能力。这对于目前互联网行业的人才需求来说是非常重要的,也为学生提供了广阔的就业和创业机会。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值