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

相关文章

springboot集成easypoi导出word换行处理过程

《springboot集成easypoi导出word换行处理过程》SpringBoot集成Easypoi导出Word时,换行符n失效显示为空格,解决方法包括生成段落或替换模板中n为回车,同时需确... 目录项目场景问题描述解决方案第一种:生成段落的方式第二种:替换模板的情况,换行符替换成回车总结项目场景s

C#使用Spire.Doc for .NET实现HTML转Word的高效方案

《C#使用Spire.Docfor.NET实现HTML转Word的高效方案》在Web开发中,HTML内容的生成与处理是高频需求,然而,当用户需要将HTML页面或动态生成的HTML字符串转换为Wor... 目录引言一、html转Word的典型场景与挑战二、用 Spire.Doc 实现 HTML 转 Word1

Java实现在Word文档中添加文本水印和图片水印的操作指南

《Java实现在Word文档中添加文本水印和图片水印的操作指南》在当今数字时代,文档的自动化处理与安全防护变得尤为重要,无论是为了保护版权、推广品牌,还是为了在文档中加入特定的标识,为Word文档添加... 目录引言Spire.Doc for Java:高效Word文档处理的利器代码实战:使用Java为Wo

使用Python实现Word文档的自动化对比方案

《使用Python实现Word文档的自动化对比方案》我们经常需要比较两个Word文档的版本差异,无论是合同修订、论文修改还是代码文档更新,人工比对不仅效率低下,还容易遗漏关键改动,下面通过一个实际案例... 目录引言一、使用python-docx库解析文档结构二、使用difflib进行差异比对三、高级对比方

Python从Word文档中提取图片并生成PPT的操作代码

《Python从Word文档中提取图片并生成PPT的操作代码》在日常办公场景中,我们经常需要从Word文档中提取图片,并将这些图片整理到PowerPoint幻灯片中,手动完成这一任务既耗时又容易出错,... 目录引言背景与需求解决方案概述代码解析代码核心逻辑说明总结引言在日常办公场景中,我们经常需要从 W

C#高效实现Word文档内容查找与替换的6种方法

《C#高效实现Word文档内容查找与替换的6种方法》在日常文档处理工作中,尤其是面对大型Word文档时,手动查找、替换文本往往既耗时又容易出错,本文整理了C#查找与替换Word内容的6种方法,大家可以... 目录环境准备方法一:查找文本并替换为新文本方法二:使用正则表达式查找并替换文本方法三:将文本替换为图

Java高效实现Word转PDF的完整指南

《Java高效实现Word转PDF的完整指南》这篇文章主要为大家详细介绍了如何用Spire.DocforJava库实现Word到PDF文档的快速转换,并解析其转换选项的灵活配置技巧,希望对大家有所帮助... 目录方法一:三步实现核心功能方法二:高级选项配置性能优化建议方法补充ASPose 实现方案Libre

Python批量替换多个Word文档的多个关键字的方法

《Python批量替换多个Word文档的多个关键字的方法》有时,我们手头上有多个Excel或者Word文件,但是领导突然要求对某几个术语进行批量的修改,你是不是有要崩溃的感觉,所以本文给大家介绍了Py... 目录工具准备先梳理一下思路神奇代码来啦!代码详解激动人心的测试结语嘿,各位小伙伴们,大家好!有没有想

Python实现Word转PDF全攻略(从入门到实战)

《Python实现Word转PDF全攻略(从入门到实战)》在数字化办公场景中,Word文档的跨平台兼容性始终是个难题,而PDF格式凭借所见即所得的特性,已成为文档分发和归档的标准格式,下面小编就来和大... 目录一、为什么需要python处理Word转PDF?二、主流转换方案对比三、五套实战方案详解方案1:

Python清空Word段落样式的三种方法

《Python清空Word段落样式的三种方法》:本文主要介绍如何用python-docx库清空Word段落样式,提供三种方法:设置为Normal样式、清除直接格式、创建新Normal样式,注意需重... 目录方法一:直接设置段落样式为"Normal"方法二:清除所有直接格式设置方法三:创建新的Normal样