WPUZZLES - Word Puzzles
no tags
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.
Problem
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 the input contains a number T ≤ 10 which indicates the number of test cases to follow. Each test case starts with a line consisting of three positive numbers: The number of lines of the word puzzle, 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 consisting of C uppercase letters, contain the word puzzle. Then at last the W words are input one per line. You can assume that each word can be found exactly once in the word puzzle.
Output
For each test case your program should output W lines: For each word (using the same order as the words were input) print 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 defined above. Each value in the triplet must be separated by one space only.
Print one blank line between test cases.
Example
Input:
1
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
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
题目链接:SPOJ WPUZZLES
把询问的串加入字典树建立AC自动机,节点末位标上询问$id$,然后把输入的表拿各个起点的各个合法方向去匹配,然后把对应$id$的$ans$数组更新一下即可,方向数组$0-7$代表了从$12$点方向即$A~H$开始顺时针的八个方向
代码:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
using namespace std;
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 1000010;
struct Trie
{
int nxt[26];
int fail, id;
void init()
{
for (int i = 0; i < 26; ++i)
nxt[i] = -1;
fail = 0;
id = -1;
}
} L[N];
int sz;
char s[1010][1010], temp[1010];
int ans[1010][3];
int dir[8][2] = { -1, 0, -1, 1, 0, 1, 1, 1, 1, 0, 1, -1, 0, -1, -1, -1};
int n, m, Len[1010];
namespace ac
{
void init()
{
sz = 0;
L[sz++].init();
}
void ins(char s[], int len, int x)
{
int u = 0;
for (int i = 0; i < len; ++i)
{
int v = s[i] - 'A';
if (L[u].nxt[v] == -1)
{
L[sz].init();
L[u].nxt[v] = sz++;
}
u = L[u].nxt[v];
}
L[u].id = x;
}
void build()
{
L[0].fail = 0;
queue<int>Q;
for (int i = 0; i < 26; ++i)
{
int v = L[0].nxt[i];
if (~v)
{
Q.push(v);
L[v].fail = 0;
}
else
L[0].nxt[i] = 0;
}
while (!Q.empty())
{
int u = Q.front();
Q.pop();
for (int i = 0; i < 26; ++i)
{
int v = L[u].nxt[i];
if (~v)
{
L[v].fail = L[L[u].fail].nxt[i];
Q.push(v);
}
else
L[u].nxt[i] = L[L[u].fail].nxt[i];
}
}
}
void query(int x, int y, int k)
{
int u = 0;
while (x >= 0 && x < n && y >= 0 && y < m)
{
int v = s[x][y] - 'A';
u = L[u].nxt[v];
if (L[u].id != -1)
{
int id = L[u].id;
ans[id][0] = x - (Len[id] - 1) * dir[k][0];
ans[id][1] = y - (Len[id] - 1) * dir[k][1];
ans[id][2] = k;
}
x += dir[k][0];
y += dir[k][1];
}
}
}
int main(void)
{
int T, i, q;
scanf("%d", &T);
while (T--)
{
scanf("%d%d%d", &n, &m, &q);
for (i = 0; i < n; ++i)
scanf("%s", s[i]);
ac::init();
for (i = 0; i < q; ++i)
{
scanf("%s", temp);
Len[i] = strlen(temp);
ac::ins(temp, Len[i], i);
}
ac::build();
for (i = 0; i < n; ++i)
{
ac::query(i, 0, 1);
ac::query(i, 0, 2);
ac::query(i, 0, 3);
ac::query(i, m - 1, 7);
ac::query(i, m - 1, 6);
ac::query(i, m - 1, 5);
}
for (i = 0; i < m; ++i)
{
ac::query(0, i, 5);
ac::query(0, i, 4);
ac::query(0, i, 3);
ac::query(n - 1, i, 7);
ac::query(n - 1, i, 0);
ac::query(n - 1, i, 1);
}
for (i = 0; i < q; ++i)
printf("%d %d %c\n", ans[i][0], ans[i][1], ans[i][2] + 'A');
if(T)
puts("");
}
return 0;
}