POJ 1204(AC自动机)

原创 2012年03月31日 10:05:11
Word Puzzles
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 6543   Accepted: 2486   Special Judge


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).


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.


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. 



#define maxn 1000001
#define maxn2 1002
#define maxn3 100
#define maxn4 2000000
#define kind 26
#define clear(a,b) memset(a,b,sizeof(a))

const int di[8][2] = {{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1}};
const char tranf[8] = {'E','F','G','H','A','B','C','D'};

char ma[maxn2][maxn2];
int X[maxn2],Y[maxn2],dir[maxn2];

struct AC_node{
  int a,b;
  struct AC_node *next[kind],*fail;
    fail = NULL;
    a = 0;
    b = 0;
  } *q[maxn];

char s1[maxn3],txt[maxn4];

void AC_trie(struct AC_node *T,char s[],int t)
  int k,i = strlen(s)-1;
  struct AC_node *op = T;
  while(i >= 0) {
    k = s[i] - 'A';
    if (op->next[k] == NULL) op->next[k] = new AC_node();
    op = op->next[k];
    op->b = t;

void AC_build(struct AC_node *T)
  int i,h = 0,t = 0;
  struct AC_node *op,*tt;
  T->fail = NULL;
  q[t++] = T;
  while (h < t) {
    op = q[h++];
    for(i = 0;i < kind;i++) {
      if (op->next[i]!=NULL) {
        if (op == T) op->next[i]->fail = T;
          else {
            tt = op->fail;
            while(tt != NULL) {
              if (tt->next[i] != NULL) {
                op->next[i]->fail = tt->next[i];
              tt = tt->fail;
            if (tt == NULL) op->next[i]->fail = T;
        q[t++] = op->next[i];

int AC_query(struct AC_node *T,int x,int y,int d)
  int i = 0,ans = 0,k;
  struct AC_node *op = T;
  while(ma[x][y]) {
    k = ma[x][y] - 'A';
    while(op->next[k] == NULL && op != T) op = op->fail;
    op = op->next[k];
    op = (op == NULL)?T:op;
    struct AC_node *tmp = op;
    while(tmp != T) {
      if (tmp->a > 0) {
        if (x < X[tmp->b] || (x == X[tmp->b] && y < Y[tmp->b])) {
          X[tmp->b] = x;
          Y[tmp->b] = y;
          dir[tmp->b] = d;
      tmp = tmp->fail;
  return ans;

int main()
  int j,m,n,i,k;

  for(i = 1;i <= m;i++,getchar())
    for(j = 1;j <= n;j++) scanf("%c",&ma[i][j]);
  struct AC_node *root = new AC_node();
  for(i = 1;i <= k;i++) {
      X[i] = maxn;
      Y[i] = maxn;
  for(i = 1;i <= m;i++) {AC_query(root,i,1,2);AC_query(root,i,n,6);}
  for(i = 1;i <= n;i++) {AC_query(root,1,i,4);AC_query(root,m,i,0);}
  for(i = 1;i <= n;i++) {AC_query(root,1,i,3);AC_query(root,1,i,5);AC_query(root,m,i,7);AC_query(root,m,i,1);}
  for(i = 1;i <= m;i++) {AC_query(root,i,1,1);AC_query(root,i,1,3);AC_query(root,i,n,5);AC_query(root,i,n,7);}
  for(i = 1;i <= k;i++) printf("%d %d %c\n",X[i]-1,Y[i]-1,tranf[dir[i]]);
  return 0;



POJ1204 Word Puzzles AC自动机

Problem Address:http://poj.org/problem?id=1204 【前言】 一道算是比较简单的AC自动机的题。 开始的时候,出于对第一份AC自动机代码的怀疑以及对上一份小变...

Poj 1204 简单AC自动机

题目链接:http://poj.org/problem?id=1204   题意:给出一个字符方阵和一系列字符串,试从方阵中找出字符串出现的位置,方阵中字符串按八个方向查询。   将待查询字符...

POJ 1204 AC自动机的初步认识+模板题

  • kk303
  • kk303
  • 2011-10-27 23:53
  • 3287

poj 1204 Word Puzzles AC自动机

http://poj.org/problem?id=1204 题意:给你一个N*M的由大写字符组成的矩阵,再给你M个长度不超过1000的字符串,要你确定这些字符串在大矩阵中的开始位置和方向。 思路...


Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 8153   Accepted: 3086   Special Judge...

POJ 1204 Word Puzzles (AC自动机)

POJ 1204 Word Puzzles (AC自动机)

POJ1204--Word Puzzles--AC自动机

Description Word puzzles are usually simple and very entertaining for all ages. They are so enter...

poj 1204 AC自动机

AC自动机的经典题 可以使用一个小技巧,将要查找的单词反过来建自动机,这样便于记录首字母的位置。 #include using namespace std; const int maxn=100...

poj 1204 Word Puzzles (AC自动机)

题目链接:   poj 1204 题目大意:   给出字典,再给出n个单词                   找出这些单词在单词表的什么位置,以什么顺序排放(8个方...

SPOJ WPUZZLES (POJ 1204) 413.Word Puzzles AC自动机

题目大意: 就是现在有一个游戏,给你一个L*C的字符块,只包含大写英文字母,现在你要在这个块中找到给出的所有的英语单词的位置和摆放方向。 其中要找的单词在块中只出现一次,(实际上多个要找的块不会重合,...