HDU 1875 Word Puzzle

2023-12-10 04:38
文章标签 word hdu puzzle 1875

本文主要是介绍HDU 1875 Word Puzzle,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

链接:

http://acm.hdu.edu.cn/showproblem.php?pid=1857


题目:

Word Puzzle

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 427    Accepted Submission(s): 76


Problem Description
Did you heard of a little game named "Word Puzzle" ? If you didn't, what a pity !
In the game, you will be given 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.


Input
There is only one test case.

The first line is two integers R and C separated by a whitespace. R (20 ≤ R ≤ 500) is the number of rows of the grid. C (20 ≤ C ≤ 500) is the number of columns of the grid. 

The following R lines, each line will contains exactly C characters without anything else. Each character is in the range 'A' - 'Z'. 

A blank line will be followed after the grid.

The following 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'). The dictionary consists of at most 10000 words.

-1 means the end of dictionary.


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
  
3 5 HENRY GAVIN MAGICHENRY HGM HAG MAVIN -1

Sample Output
  
0 0 0 0 0 0 -1 -1

Author
XrtGavin@TJU

Source
HDU 2007 Programming Contest - Final


题目大意:
给一个字母矩阵, 矩阵里可以按照横着,竖着,斜着(左上--->右下方向), 组成单词。
然后询问一些单词,是否存在矩阵中。如果存在,输出它的起点位置(开始位置在越上面,越左边越好)。


分析与总结:
做这题想了足足有两天啊,我容易吗我哭.

1.  初看, 觉得水题一枚, 直接枚举矩阵的起点,然后把矩阵中所有可能的单词都进行建Trie树, 在Trie中要保存起点。然后很开心的提交了,结果Memory Limit Exceeded

2. 然后就一直想啊想啊想.....无线纠结中

3. 终于,我头脑上面的灯泡亮了一下,发现询问的单词只有1W个,那么我们其实就可以把询问单词建成一个Trie树, 然后再枚举矩阵里面的单词,看枚举的单词是否在Trie中,在的话就把位置保存下来。
全部枚举完之后, 在输出在Trie中的询问单词所保存的位置信息。

4. 之后再看看ACM官方标志图片上的那个闪亮的灯泡, 似乎有所感悟...做ACM追求的不就是灯泡亮的那一刻吗?
    如果做ACM题, 灯泡都不亮一下就做出来了,乐趣就少了很多。





代码:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;const int KIND = 26;
const int MAXN = 1000000;
int cnt_node;
int R,C;
char map[505][505];
char word[10005][22];struct node{bool isword;int r,c;node* next[KIND];void init(){r=c=-1;isword=false;memset(next, 0, sizeof(next));}
}Heap[MAXN];inline node* new_node(){Heap[cnt_node].init();return &Heap[cnt_node++];
}
// 把询问的单词建Trie树
void insert(node* root, char *str){for(char *p=str; *p; ++p){int ch=*p-'A';if(root->next[ch]==NULL)root->next[ch] = new_node();root=root->next[ch];} root->isword=true;
}
// 对矩阵中枚举的单词查找是否再Trie树中
void search(node* root, char *str, int row, int col){for(char *p=str; *p; ++p){int ch=*p-'A';if(root->next[ch]==NULL)return ;root=root->next[ch];if(root->isword && root->r==-1 && root->c==-1){root->r=row, root->c=col; }}if(root->isword && root->r==-1 && root->c==-1){root->r=row, root->c=col;}
}
// 输出询问的单词在矩阵中的位置
void output(node* root, char *str){for(char *p=str; *p; ++p){int ch=*p-'A';if(root->next[ch]==NULL) return;root=root->next[ch];}if(root->isword)printf("%d %d\n", root->r, root->c);
}int main(){// Trie initcnt_node=0;node* root=new_node();scanf("%d%d%*c",&R,&C);for(int i=0; i<R; ++i){gets(map[i]);}gets(word[0]); //消除空格int pos=0;while(gets(word[pos])){if(word[pos][0]=='-') break;insert(root, word[pos++]);}char str[30];for(int i=0; i<R; ++i){for(int j=0; j<C; ++j){int end_r,end_c=j;// 竖下if(i+20<R)end_r=i+20;else end_r=R;memset(str, 0, sizeof(str));for(int k=i,p=0; k<end_r; ++k)str[p++]=map[k][j];search(root, str, i, j);// 横着if(j+20<C) end_c=j+20;elseend_c=C;memset(str, 0, sizeof(str));for(int k=j,p=0; k<end_c; ++k)str[p++]=map[i][k];search(root, str, i, j);// 斜着int r=i, c=j,p=0;memset(str, 0, sizeof(str));while(r<end_r && c<end_c){str[p++]=map[r][c];++r, ++c;}search(root,str,i,j);}}for(int i=0; i<pos; ++i){output(root, word[i]);}return 0;
}





 ——  生命的意义,在于赋予它意义士。

          原创 http://blog.csdn.net/shuangde800 , By   D_Double  (转载请标明)







这篇关于HDU 1875 Word Puzzle的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/476036

相关文章

Python自动化提取多个Word文档的文本

《Python自动化提取多个Word文档的文本》在日常工作和学习中,我们经常需要处理大量的Word文档,本文将深入探讨如何利用Python批量提取Word文档中的文本内容,帮助你解放生产力,感兴趣的小... 目录为什么需要批量提取Word文档文本批量提取Word文本的核心技术与工具安装 Spire.Doc

C#高效实现在Word文档中自动化创建图表的可视化方案

《C#高效实现在Word文档中自动化创建图表的可视化方案》本文将深入探讨如何利用C#,结合一款功能强大的第三方库,实现在Word文档中自动化创建图表,为你的数据呈现和报告生成提供一套实用且高效的解决方... 目录Word文档图表自动化:为什么选择C#?从零开始:C#实现Word文档图表的基本步骤深度优化:C

Qt实现对Word网页的读取功能

《Qt实现对Word网页的读取功能》文章介绍了几种在Qt中实现Word文档(.docx/.doc)读写功能的方法,包括基于QAxObject的COM接口调用、DOCX模板替换及跨平台解决方案,重点讨论... 目录1. 核心实现方式2. 基于QAxObject的COM接口调用(Windows专用)2.1 环境

使用Python将PDF表格自动提取并写入Word文档表格

《使用Python将PDF表格自动提取并写入Word文档表格》在实际办公与数据处理场景中,PDF文件里的表格往往无法直接复制到Word中,本文将介绍如何使用Python从PDF文件中提取表格数据,并将... 目录引言1. 加载 PDF 文件并准备 Word 文档2. 提取 PDF 表格并创建 Word 表格

Python轻松实现Word到Markdown的转换

《Python轻松实现Word到Markdown的转换》在文档管理、内容发布等场景中,将Word转换为Markdown格式是常见需求,本文将介绍如何使用FreeSpire.DocforPython实现... 目录一、工具简介二、核心转换实现1. 基础单文件转换2. 批量转换Word文件三、工具特性分析优点局

Java使用Spire.Doc for Java实现Word自动化插入图片

《Java使用Spire.DocforJava实现Word自动化插入图片》在日常工作中,Word文档是不可或缺的工具,而图片作为信息传达的重要载体,其在文档中的插入与布局显得尤为关键,下面我们就来... 目录1. Spire.Doc for Java库介绍与安装2. 使用特定的环绕方式插入图片3. 在指定位

Java利用Spire.Doc for Java实现在模板的基础上创建Word文档

《Java利用Spire.DocforJava实现在模板的基础上创建Word文档》在日常开发中,我们经常需要根据特定数据动态生成Word文档,本文将深入探讨如何利用强大的Java库Spire.Do... 目录1. Spire.Doc for Java 库介绍与安装特点与优势Maven 依赖配置2. 通过替换

C#实现插入与删除Word文档目录的完整指南

《C#实现插入与删除Word文档目录的完整指南》在日常的办公自动化或文档处理场景中,Word文档的目录扮演着至关重要的角色,本文将深入探讨如何利用强大的第三方库Spire.Docfor.NET,在C#... 目录Spire.Doc for .NET 库:Word 文档处理利器自动化生成:C# 插入 Word

Python实现Word文档自动化的操作大全(批量生成、模板填充与内容修改)

《Python实现Word文档自动化的操作大全(批量生成、模板填充与内容修改)》在职场中,Word文档是公认的好伙伴,但你有没有被它折磨过?批量生成合同、制作报告以及发放证书/通知等等,这些重复、低效... 目录重复性文档制作,手动填充模板,效率低下还易错1.python-docx入门:Word文档的“瑞士

使用Java填充Word模板的操作指南

《使用Java填充Word模板的操作指南》本文介绍了Java填充Word模板的实现方法,包括文本、列表和复选框的填充,首先通过Word域功能设置模板变量,然后使用poi-tl、aspose-words... 目录前言一、设置word模板普通字段列表字段复选框二、代码1. 引入POM2. 模板放入项目3.代码