您的位置:首页 > 其它

POJ1204-Word Puzzles

2017-10-29 08:58 447 查看

POJ1204-Word Puzzles

Word Puzzles

Time Limit: 5000MS

Memory Limit: 65536K

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

Source

Southwestern Europe 2002

题意是在字母表中找W个单词的位置,以正北为A,顺时针输出方向。

观察问题,发现是一个多串匹配的问题,即可用AC自动机求解。

AC自动机的程序过程:

(1)用模式串建立Trie树,用数组实现浪费空间,不过时间上更快一点。

(2)在Trie树构建类似KMP算法的pre后缀数组,又叫匹配失败指针

这一步可以通过队列实现。

这里注意要将将pre指向结点的标记继承,是一个对Trie树的加强,以保证将主串中所有的模式串找到。

(3)在主串上顺序遍历,同时在AC自动机漫游,就可以求出模式串在主串位置。

这道题只需要将四周的格子为起点,八个方向跑一遍AC自动机即可。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
const int MAXN = 1000 + 5;
const int d1[10] = {0, -1, -1, 0, 1, 1, 1, 0, -1};
const int d2[10] = {0, 0, 1, 1, 1, 0, -1, -1, -1};
typedef vector<pair<int, int> > vec;
#define fs first
#define se second

struct Trie{
int tree[100000][30], pre[100000], tot;
bool flag[100000];
queue<int> q;
vector<int> id[100000];

void init() {
memset(tree, 0, sizeof tree);
memset(flag, 0, sizeof flag);
tot = 1;
}

void insert(char *s, int id0) {
int cur = 1;
for (int i = 0; s[i]; ++i) {
int tep = s[i] - 'A' + 1;
if (tree[cur][tep])
cur = tree[cur][tep];
else {
tree[cur][tep] = ++tot;
cur = tot;
}
}
flag[cur] = 1;
id[cur].push_back(id0);
}

void buildPre() {
q.push(1);
while (!q.empty()) {
int cur = q.front();
q.pop();
for (int i = 1; i <= 26; ++i) {
int tep = tree[cur][i];
if (tep) {
int j = pre[cur];
while (j && !tree[j][i]) j = pre[j];
if (!j) pre[tep] = 1;
else pre[tep] = tree[j][i];
if (flag[pre[tep]]) {
flag[tep] = 1;
for (int i = 0; i < id[pre[tep]].size(); ++i)
id[tep].push_back(id[pre[tep]][i]);
}
q.push(tep);
}
}
}
}

vec AC_automation(char *s) {
int cur = 1;
vec res;
for (int i = 0; s[i]; ++i) {
int tep = s[i] - 'A' + 1;
while (cur && !tree[cur][tep]) cur = pre[cur];
if (!cur) cur = 1;
if (tree[cur][tep]) cur = tree[cur][tep];
if (flag[cur]) res.push_back(make_pair(cur, i));
}
return res;
}
}AC;

char s[MAXN][MAXN], p[MAXN], bri[MAXN];
int len[MAXN], ans[MAXN][5], L, C, W;

void solve(int x, int y) {
for (int i = 1; i <= 8; ++i) {
memset(bri, 0, sizeof bri);
int dx = x, dy = y, tot = -1;
while (true) {
bri[++tot] = s[dx][dy];
dx += d1[i];
dy += d2[i];
if (dx<1 || dx>L || dy<1 || dy>C) break;
}
vec res = AC.AC_automation(bri);
for (int j = 0; j < res.size(); ++j)
for (int k = 0; k < AC.id[res[j].fs].size(); ++k) {
int cur = AC.id[res[j].fs][k];
int ps = res[j].se - len[cur] + 1;
ans[cur][0] = x + d1[i] * ps - 1;
ans[cur][1] = y + d2[i] * ps - 1;
ans[cur][2] = 'A' + i - 1;
}
}
}

int main()
{
freopen("in.txt", "r", stdin);
while(scanf("%d%d%d", &L, &C, &W) != EOF) {
AC.init();
for (int i = 1; i <= L; ++i)
scanf("%s", &s[i][1]);
for (int i = 1; i <= W; ++i) {
scanf("%s", p);
AC.insert(p, i);
len[i] = strlen(p);
}
AC.buildPre();
for (int i = 1; i <= L; ++i) {
solve(i, 1);
solve(i, C);
}
for (int i = 1; i <= C; ++i) {
solve(1, i);
solve(L, i);
}
for (int i = 1; i <= W; ++i)
printf("%d %d %c\n", ans[i][0], ans[i][1], (char)ans[i][2]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  poj