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

相关文章

C#实现将Office文档(Word/Excel/PDF/PPT)转为Markdown格式

《C#实现将Office文档(Word/Excel/PDF/PPT)转为Markdown格式》Markdown凭借简洁的语法、优良的可读性,以及对版本控制系统的高度兼容性,逐渐成为最受欢迎的文档格式... 目录为什么要将文档转换为 Markdown 格式使用工具将 Word 文档转换为 Markdown(.

Python实现自动化Word文档样式复制与内容生成

《Python实现自动化Word文档样式复制与内容生成》在办公自动化领域,高效处理Word文档的样式和内容复制是一个常见需求,本文将展示如何利用Python的python-docx库实现... 目录一、为什么需要自动化 Word 文档处理二、核心功能实现:样式与表格的深度复制1. 表格复制(含样式与内容)2

Python实现一键PDF转Word(附完整代码及详细步骤)

《Python实现一键PDF转Word(附完整代码及详细步骤)》pdf2docx是一个基于Python的第三方库,专门用于将PDF文件转换为可编辑的Word文档,下面我们就来看看如何通过pdf2doc... 目录引言:为什么需要PDF转Word一、pdf2docx介绍1. pdf2docx 是什么2. by

如何Python使用设置word的页边距

《如何Python使用设置word的页边距》在编写或处理Word文档的过程中,页边距是一个不可忽视的排版要素,本文将介绍如何使用Python设置Word文档中各个节的页边距,需要的可以参考下... 目录操作步骤代码示例页边距单位说明应用场景与高级用China编程途小结在编写或处理Word文档的过程中,页边距是一个

Python使用python-docx实现自动化处理Word文档

《Python使用python-docx实现自动化处理Word文档》这篇文章主要为大家展示了Python如何通过代码实现段落样式复制,HTML表格转Word表格以及动态生成可定制化模板的功能,感兴趣的... 目录一、引言二、核心功能模块解析1. 段落样式与图片复制2. html表格转Word表格3. 模板生

Java如何根据word模板导出数据

《Java如何根据word模板导出数据》这篇文章主要为大家详细介绍了Java如何实现根据word模板导出数据,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... pom.XML文件导入依赖 <dependency> <groupId>cn.afterturn</groupId>

Python实现word文档内容智能提取以及合成

《Python实现word文档内容智能提取以及合成》这篇文章主要为大家详细介绍了如何使用Python实现从10个左右的docx文档中抽取内容,再调整语言风格后生成新的文档,感兴趣的小伙伴可以了解一下... 目录核心思路技术路径实现步骤阶段一:准备工作阶段二:内容提取 (python 脚本)阶段三:语言风格调

Java利用docx4j+Freemarker生成word文档

《Java利用docx4j+Freemarker生成word文档》这篇文章主要为大家详细介绍了Java如何利用docx4j+Freemarker生成word文档,文中的示例代码讲解详细,感兴趣的小伙伴... 目录技术方案maven依赖创建模板文件实现代码技术方案Java 1.8 + docx4j + Fr

vue使用docxtemplater导出word

《vue使用docxtemplater导出word》docxtemplater是一种邮件合并工具,以编程方式使用并处理条件、循环,并且可以扩展以插入任何内容,下面我们来看看如何使用docxtempl... 目录docxtemplatervue使用docxtemplater导出word安装常用语法 封装导出方

Java利用poi实现word表格转excel

《Java利用poi实现word表格转excel》这篇文章主要为大家详细介绍了Java如何利用poi实现word表格转excel,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 一、每行对象类需要针对不同的表格进行对应的创建。package org.example.wordToEx