UVa 10010 Where's Waldorf? (八方向字符串匹配&模板题)

Time limit: 3.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=96&page=show_problem&problem=951

Given a m by n grid of letters, ( ), and a list of words, find the location in the grid at which the word can be found. A word matches a straight, uninterrupted line of letters in the grid. A word can match the letters in the grid regardless of case (i.e. upper and lower case letters are to be treated as the same). The matching can be done in any of the eight directions either horizontally, vertically or diagonally through the grid.

Input

The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.

The input begins with a pair of integers, m followed by n, in decimal notation on a single line. The next m lines contain n letters each; this is the grid of letters in which the words of the list must be found. The letters in the grid may be in upper or lower case. Following the grid of letters, another integer k appears on a line by itself ( ). The next k lines of input contain the list of words to search for, one word per line. These words may contain upper and lower case letters only (no spaces, hyphens or other non-alphabetic characters).

Output

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.

For each word in the word list, a pair of integers representing the location of the corresponding word in the grid must be output. The integers must be separated by a single space. The first integer is the line in the grid where the first letter of the given word can be found (1 represents the topmost line in the grid, and m represents the bottommost line). The second integer is the column in the grid where the first letter of the given word can be found (1 represents the leftmost column in the grid, and n represents the rightmost column in the grid). If a word can be found more than once in the grid, then the location which is output should correspond to the uppermost occurence of the word (i.e. the occurence which places the first letter of the word closest to the top of the grid). If two or more words are uppermost, the output should correspond to the leftmost of these occurences. All words can be found at least once in the grid.

Sample Input

1

8 11
abcDEFGhigg
hEbkWalDork
FtyAwaldORm
FtsimrLqsrc
byoArBeDeyv
Klcbqwikomk
strEBGadhrb
yUiqlxcnBjf
4
Waldorf
Bambi
Betty
Dagbert

Sample Output

2 5
2 3
1 2
7 8

完整代码:

/*0.022s*/

#include<cstdio>
#include<cctype>
#include<cstring>  

int m, n, curm, curn;
char grid[100][100], word[100];  

///x,y表方向
inline bool match(int x, int y)
{
    int i, j, k;
    for (k = 0, i = curm, j = curn; i >= 0 && i < m && j >= 0 && j < n && word[k]; i += x, j += y, k++)
        if (word[k] != grid[i][j]) return false;
    if (word[k] == 0) return true;///说明word读完了
    return false;
}  

///题目说了一定能找到~
inline void find()
{
    for (curm = 0; curm < m; curm++)
        for (curn = 0; curn < n; curn++)
            for (int x = -1; x <= 1; x++)
                for (int y = -1; y <= 1; y++)
                    if ((x || y) && match(x, y)) return;
}  

int main(void)
{
    int N, k, i, j, len;
    while (~scanf("%d", &N))
    {
        while (N--)
        {
            scanf("%d%d", &m, &n);
            for (i = 0; i < m; i++)
            {
                getchar();
                for (j = 0; j < n; j++)
                    grid[i][j] = toupper(getchar());
            }
            scanf("%d\n", &k);
            while (k--)
            {
                gets(word);
                len = strlen(word);
                for (i = 0; i < len; ++i)
                    word[i] = toupper(word[i]);
                find();
                printf("%d %d\n", curm + 1, curn + 1);
            }
            if (N) putchar('\n');
        }
    }
    return 0;
}

查看本栏目更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索word
, grid
, line
, in
, webbroswer&amp;amp;nbsp;word
, of
The
uva10010、waldorf astoria、blair waldorf、waldorf酒店、waldorf,以便于您获取更多的相关知识。

时间: 2024-08-02 23:14:46

UVa 10010 Where's Waldorf? (八方向字符串匹配&amp;模板题)的相关文章

UVa 10010 Where&#039;s Waldorf?

#include<stdio.h> #include<string.h> #define maxn 57 long test,m,n,k; char r[maxn][maxn]; const long xd[]={-1,-1,0,1,1,1,0,-1},yd[]={0,1,1,1,0,-1,-1,-1}; void search(const char *a,long &x,long &y) { long pos,xx,yy; for(long i=1;i<=m

UVa 489 Hangman Judge:模拟&amp;amp;字符串匹配

489 - Hangman Judge Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=94&page=show_problem&problem=430 In ``Hangman Judge,'' you are to write a program that judges a series of Hangman g

UVa 409 Excuses, Excuses! (字符串匹配)

409 - Excuses, Excuses! Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=96&page=show_problem&problem=350 Judge Ito is having a problem with people subpoenaed for jury duty giving rath

strlen-ACM简单的字符串匹配,但老是OJ通不过,求大神指点

问题描述 ACM简单的字符串匹配,但老是OJ通不过,求大神指点 Description 给出两个字符串S和T,请判断T是否为S的子串.本题请用"简单匹配法"来做. 使用strstr函数,判cheat Input 第一行是一个整数N,说明有多少个测试用例. 接下来是N个测试用例,每个测试用例占2行:第一行是字符串S,第二行是字符串T,字符串中不含空格. 1 ≤ strlen(S) , strlen( T ) ≤ 10000 Output 对每个测试用例,输出一行结果:是否子串,是则输出&

c-文件中字符串匹配问题

问题描述 文件中字符串匹配问题 判断文件中是否存在某一字符串,若存在则退出,若不存在则添加???求用c编写的代码,,,各位大虾帮帮忙,万分感谢!!!!! 解决方案 先读出文件的内容,然后通过 strstr 等类似的功能函数完成字符串是否存在的判断 解决方案二: 参考一下这个 C语言程序,在文件中查找指定字符串出现的次数http://wenda.haosou.com/q/1382138287067435

UVa 10405:Longest Common Subsequence,最长公共子序列模板题

[链接] http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=114&page=show_problem&problem=1346 [原题] Problem C: Longest Common Subsequence Sequence 1: Sequence 2: Given two sequences of characters, print the length of

UVa 10034:Freckles (最小生成树模板题)

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=975 题目: Problem A: Freckles In an episode of the Dick Van Dyke show, little Richie connects the freckles on his Dad's back to fo

字符串匹配的KMP算法

字符串匹配是计算机的基本任务之一. 举例来说,有一个字符串"BBC ABCDAB ABCDABCDABDE",我想知道,里面是否包含另一个字符串"ABCDABD"? 许多算法可以完成这个任务,Knuth-Morris-Pratt算法(简称KMP)是最常用的之一.它以三个发明者命名,起头的那个K就是著名科学家Donald Knuth. 这种算法不太容易理解,网上有很多解释,但读起来都很费劲.直到读到Jake Boxer的文章,我才真正理解这种算法.下面,我用自己的语言

[算法系列之十二]字符串匹配之蛮力匹配

引言 字符串匹配是数据库开发和文字处理软件的关键.幸运的是所有现代编程语言和字符串库函数,帮助我们的日常工作.不过理解他们的原理还是比较重要的. 字符串算法主要可以分为几类.字符串匹配就是其中之一.当我们提到字符串匹配算法,最基本的方法就是所谓的蛮力解法,这意味着我们需要检查每一个文本串中的字符是否和匹配串相匹配.一般来说我们有文本串和一个匹配串(通常匹配串短于文本串).我们需要做的就是回答这个匹配串是否出现在文本串中. 概述 字符串蛮力匹配法的原理非常简单.我们必须检查匹配串的第一个字符与文本