【 POJ - 1204 Word Puzzles】(Trie+爆搜|AC自动机)
Word Puzzles
Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 10782 Accepted: 4076 Special Judge Description
Word puzzles are usually simple and very entertaining for all ages. They are so entertaining that Pizza-Hut company started using table covers with word puzzles printed on them, possibly with the intent to minimise their client's perception of any possible delay in bringing them their order.Even though word puzzles may be entertaining to solve by hand, they may become boring when they get very large. Computers do not yet get bored in solving tasks, therefore we thought you could devise a program to speedup (hopefully!) solution finding in such puzzles.
The following figure illustrates the PizzaHut puzzle. The names of the pizzas to be found in the puzzle are: MARGARITA, ALEMA, BARBECUE, TROPICAL, SUPREMA, LOUISIANA, CHEESEHAM, EUROPA, HAVAIANA, CAMPONESA.
Your task is to produce a program that given the word puzzle and words to be found in the puzzle, determines, for each word, the position of the first letter and its orientation in the puzzle.You can assume that the left upper corner of the puzzle is the origin, (0,0). Furthemore, the orientation of the word is marked clockwise starting with letter A for north (note: there are 8 possible directions in total).
Input
The first line of input consists of three positive numbers, the number of lines, 0 < L <= 1000, the number of columns, 0 < C <= 1000, and the number of words to be found, 0 < W <= 1000. The following L input lines, each one of size C characters, contain the word puzzle. Then at last the W words are input one per line.Output
Your program should output, for each word (using the same order as the words were input) a triplet defining the coordinates, line and column, where the first letter of the word appears, followed by a letter indicating the orientation of the word according to the rules define above. Each value in the triplet must be separated by one space only.Sample Input
- 20 20 10
- QWSPILAATIRAGRAMYKEI
- AGTRCLQAXLPOIJLFVBUQ
- TQTKAZXVMRWALEMAPKCW
- LIEACNKAZXKPOTPIZCEO
- FGKLSTCBTROPICALBLBC
- JEWHJEEWSMLPOEKORORA
- LUPQWRNJOAAGJKMUSJAE
- KRQEIOLOAOQPRTVILCBZ
- QOPUCAJSPPOUTMTSLPSF
- LPOUYTRFGMMLKIUISXSW
- WAHCPOIYTGAKLMNAHBVA
- EIAKHPLBGSMCLOGNGJML
- LDTIKENVCSWQAZUAOEAL
- HOPLPGEJKMNUTIIORMNC
- LOIUFTGSQACAXMOPBEIO
- QOASDHOPEPNBUYUYOBXB
- IONIAELOJHSWASMOUTRK
- HPOIYTJPLNAQWDRIBITG
- LPOINUYMRTEMPTMLMNBO
- PAFCOPLHAVAIANALBPFS
- MARGARITA
- ALEMA
- BARBECUE
- TROPICAL
- SUPREMA
- LOUISIANA
- CHEESEHAM
- EUROPA
- HAVAIANA
- CAMPONESA
Sample Output
- 0 15 G
- 2 11 C
- 7 18 A
- 4 8 C
- 16 13 B
- 4 15 E
- 10 3 D
- 5 1 E
- 19 7 C
- 11 11 H
- 【分析】
听说TRIE+爆搜能过,于是我就爆搜了。
代码如下:
- #include<cstdio>
- #include<cstdlib>
- #include<cstring>
- #include<iostream>
- #include<algorithm>
- #include<queue>
- using namespace std;
- #define Maxn 1010
- #define Maxl 1010
- char s[Maxl][Maxl];
- char ss[Maxl][Maxl];
- int dx[]={,,,,-,-,-,,},
- dy[]={,,-,-,-,,,,};
- int ans[Maxn][],dr[Maxn];
- int n,m;
- struct node
- {
- int son[],cnt,fail;
- int num,rt,p;
- }t[Maxn*];int tot;
- void upd(int x)
- {
- t[x].cnt=;t[x].p=;
- memset(t[x].son,,sizeof(t[x].son));
- }
- void read_trie(int x)
- {
- scanf("%s",ss[x]+);
- int len=strlen(ss[x]+);
- int now=;
- for(int j=;j<=len;j++) ss[][j]=ss[x][len-j+];
- for(int j=;j<=len;j++)
- {
- int ind=ss[][j]-'A'+;
- if(!t[now].son[ind])
- t[now].son[ind]=++tot,upd(tot);
- now=t[now].son[ind];
- if(j==len) t[now].cnt++,t[now].p=x;
- }
- }
- void dfs(int x,int y,int dir,int now)
- {
- if(t[now].p!=)
- {
- int q=t[now].p;
- ans[q][]=x;ans[q][]=y;dr[q]=dir;
- t[now].p=;
- }
- if(x+dx[dir]<||x+dx[dir]>n||y+dy[dir]<||y+dy[dir]>m) return;
- int a=t[now].son[s[x+dx[dir]][y+dy[dir]]-'A'+];
- if(a!=)
- dfs(x+dx[dir],y+dy[dir],dir,a);
- }
- void init()
- {
- int q;
- scanf("%d%d%d",&n,&m,&q);
- for(int i=;i<=n;i++)
- {
- scanf("%s",s[i]+);
- }
- tot=;upd();
- for(int i=;i<=q;i++)
- {
- read_trie(i);
- }
- for(int i=;i<=n+;i++)
- for(int j=;j<=m+;j++)
- {
- for(int k=;k<=;k++)
- {
- dfs(i,j,k,);
- }
- }
- for(int i=;i<=q;i++)
- {
- printf("%d %d %c\n",ans[i][]-,ans[i][]-,dr[i]+'A'-);
- }
- }
- int main()
- {
- init();
- return ;
- }
[POJ1204]
2016-06-19 14:09:06
后来还是打了一下AC自动机,嗯,感觉for那里还是超级慢,因为重叠那部分不好搞,就这样吧。
代码如下:
- #include<cstdio>
- #include<cstdlib>
- #include<cstring>
- #include<iostream>
- #include<algorithm>
- #include<queue>
- using namespace std;
- #define Maxn 1010
- #define Maxl 1010
- char s[Maxl][Maxl];
- char ss[Maxl][Maxl];
- int dx[]={,,,,-,-,-,,},
- dy[]={,,-,-,-,,,,};
- int ans[Maxn][],dr[Maxn];
- int n,m,qy;
- struct node
- {
- int son[],cnt,fail;
- int num,rt;
- }t[Maxn*];int tot;
- int p[Maxn];
- void upd(int x)
- {
- t[x].cnt=;/*t[x].p=0;*/
- memset(t[x].son,,sizeof(t[x].son));
- }
- void read_trie(int x)
- {
- scanf("%s",ss[x]+);
- int len=strlen(ss[x]+);
- int now=;
- for(int j=;j<=len;j++) ss[][j]=ss[x][len-j+];
- for(int j=;j<=len;j++)
- {
- int ind=ss[][j]-'A'+;
- if(!t[now].son[ind])
- t[now].son[ind]=++tot,upd(tot);
- now=t[now].son[ind];
- if(j==len) t[now].cnt++,p[x]=now;
- }
- }
- queue<int > q;
- void build_AC()
- {
- int i,j,x,y;
- while(!q.empty()) q.pop();
- q.push();
- while(!q.empty())
- {
- x=q.front();
- y=t[x].fail;
- for(j=;j<=;j++)
- {
- if(t[x].son[j])
- {
- q.push(t[x].son[j]);
- t[t[x].son[j]].fail=x?t[y].son[j]:x;
- }
- else t[x].son[j]=t[y].son[j];
- }
- q.pop();
- }
- }
- void check(int x,int y,int dir,int now)
- {
- if(now==) return;
- for(int i=;i<=qy;i++) if(p[i]==now)
- {
- ans[i][]=x;
- ans[i][]=y;
- dr[i]=dir;
- }
- now=t[now].fail;
- check(x,y,dir,now);
- }
- void dfs(int x,int y,int dir,int now)
- {
- now=t[now].son[s[x][y]-'A'+];
- check(x,y,dir,now);
- x=x+dx[dir],y=y+dy[dir];
- if(x<||x>n||y<||y>m) return;
- dfs(x,y,dir,now);
- }
- void init()
- {
- scanf("%d%d%d",&n,&m,&qy);
- for(int i=;i<=n;i++)
- {
- scanf("%s",s[i]+);
- }
- tot=;upd();
- for(int i=;i<=qy;i++)
- {
- read_trie(i);
- }
- build_AC();
- for(int i=;i<=n;i++)
- for(int k=;k<=;k++)
- {
- dfs(i,,k,);
- dfs(i,m,k,);
- }
- for(int j=;j<=m;j++)
- for(int k=;k<=;k++)
- {
- dfs(,j,k,);
- dfs(n,j,k,);
- }
- for(int i=;i<=qy;i++)
- {
- printf("%d %d %c\n",ans[i][]-,ans[i][]-,dr[i]+'A'-);
- }
- }
- int main()
- {
- init();
- return ;
- }
[POJ1204-2]
2016-06-21 17:14:17
【 POJ - 1204 Word Puzzles】(Trie+爆搜|AC自动机)的更多相关文章
- [POJ 1204]Word Puzzles(Trie树暴搜&AC自己主动机)
Description Word puzzles are usually simple and very entertaining for all ages. They are so entertai ...
- [poj] 1204 Word Puzzles || AC自动机
原题 给个X*Y的字符串矩阵,W个询问,对每个询问输出这个串出现的位置及方向,共有8个方向,顺时针开始分别用A~H表示 AC自动机的板子题. 对于待匹配串建一个自动机,然后从矩阵的四周分别沿八个方向跑 ...
- POJ 1204 Word Puzzles | AC 自动鸡
题目: 给一个字母矩阵和几个模式串,矩阵中的字符串可以有8个方向 输出每个模式串开头在矩阵中出现的坐标和这个串的方向 题解: 我们可以把模式串搞成AC自动机,然后枚举矩阵最外围一层的每个字母,向八个方 ...
- POJ 1204 Word Puzzles(AC自动机)
这题的数据卡在,如下: 5 5 3 ABCDE FGHIJ KLMNO PQRST UVWXY PQR RS RST puzzle中间的行中可以包含要查询的多个单词.这个问题很好解决,SearchDf ...
- poj 1204 Word Puzzles(字典树)
题目链接:http://poj.org/problem?id=1204 思路分析:由于题目数据较弱,使用暴力搜索:对于所有查找的单词建立一棵字典树,在图中的每个坐标,往8个方向搜索查找即可: 需要注意 ...
- [知识点]Trie树和AC自动机
// 此博文为迁移而来,写于2015年5月27日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102w1s8.html 1.前 ...
- 从Trie谈到AC自动机
ZJOI的SAM让我深受打击,WJZ大神怒D陈老师之T3是SAM裸题orz...我还怎么混?暂且写篇`从Trie谈到AC自动机`骗骗经验. Trie Trie是一种好玩的数据结构.它的每个结点存的是字 ...
- POJ 题目1204 Word Puzzles(AC自己主动机,多个方向查询)
Word Puzzles Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 10244 Accepted: 3864 S ...
- PKU 1204 Word Puzzles(AC自动机)
题目大意:原题链接 给定一个字符串矩阵和待查找的单词,可以朝8个不同的方向查找,输出待查找单词第一个字母在矩阵中出现的位置和该单词被查到的方向. A~H代表8个不同的方向,A代表正北方向,其他依次以4 ...
随机推荐
- 一些as的配置
打开Android Studio首选项对话框(Mac用户选择Android Studio菜单,Windows用户选择File → Settings菜单).分别展开Editor和Code Style选项 ...
- Razor的理解
[原创]Razor非常智能非常实用,不了解的人可能会觉得有没有都无所谓,其实不然,起初对Razor不是太了解,现在想想Razor就是来标示出C#语法的,但是HTML和C#混合输出时到底@这个小老鼠到底 ...
- javascript 【js‘s word】
http://mp.weixin.qq.com/s?__biz=MjM5MzY2NDY0Ng==&mid=214013689&idx=1&sn=21e03f6c7bf73893 ...
- 15第十五章UDF用户自定义函数(转载)
15第十五章UDF用户自定义函数 待补上 原文链接 本文由豆约翰博客备份专家远程一键发布
- 初学时的shell
学习期间写过一些shell脚本, 测试过程:vi test.sh 后把程序写入其中,保存退出.然后改变文件属性:chmod +x test.sh 最后执行:./test.shfor语句测试:1)#!/ ...
- tomcat的server.xml详解
Tomcat服务器是由一系列可配置的组件构成,其核心组件是Catalina Servlet容器,它是所有其他Tomcat组件的顶层容器.Tomcat的组件可以在<CATALINA_HOME& ...
- Web.xml配置详解之context-param (加载spring的xml,然后初始化bean看的)
http://www.cnblogs.com/goody9807/p/4227296.html(很不错啊) 容器先加载spring的xml,然后初始化bean时,会为bean赋值,包括里面的占位符
- 案例:latch: cache buffers chains event tuning
前两天对oracle数据库(single instance)进行了迁移升级从10.2.0.4 升到11.2.0.3,有一个项目迁完后第二天,cpu负载升到了130更高(16cpus). 向用户询问后使 ...
- iOS 获取URL中的参数
- (NSString *)getParamByName:(NSString *)name URLString:(NSString *)url { NSError *error; NSString * ...
- ZOJ 3725 Painting Storages(DP+排列组合)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5048 Sample Input 4 3 Sample Output ...