POJ 1204(AC自动机)

原创 2012年03月31日 10:05:11
Word Puzzles
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 6543   Accepted: 2486   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. 

这是我做的第二道AC自动机的题(第一道是HD2222)。。没想到能1Y。。舒坦。。。我是分方向分横纵枚举。。。

#include<stdio.h>
#include<string.h>

#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;
  AC_node(){
    fail = NULL;
    a = 0;
    b = 0;
    clear(next,NULL);
    }
  } *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];
    i--;
    }
    op->a++;
    op->b = t;
}

void AC_build(struct AC_node *T)
{
  int i,h = 0,t = 0;
  struct AC_node *op,*tt;
  clear(q,NULL);
  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];
                break;
                }
              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;
      }
    x+=di[d][0];
    y+=di[d][1];
    }
  return ans;
}

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

  scanf("%d%d%d",&m,&n,&k);
  clear(ma,0);
  getchar();
  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;
      scanf("%s",s1);
      AC_trie(root,s1,i);
    }
  AC_build(root);
  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(AC自动机)

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自动机

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

Poj 1204 简单AC自动机

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

poj 1204 Word Puzzles AC自动机

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

POJ 题目1204 Word Puzzles(AC自动机,多个方向查询)

Word Puzzles Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 10244   ...

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

前一向在搞字典树就是为了搞AC自动机...AC自动机的用处..或者说最一般的用处就是给一个字典..找一篇文章中哪些单词出现过的一种较优的方法...而AC自动机的结构或者说方法简单的说就是字典树+KMP...
  • kk303
  • kk303
  • 2011年10月27日 23:53
  • 3338

POJ1204--Word Puzzles--AC自动机

Description Word puzzles are usually simple and very entertaining for all ages. They are so enter...
  • a305657
  • a305657
  • 2014年03月14日 19:27
  • 789

poj 1204 AC自动机

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

poj 1204 Word Puzzles (AC自动机)

题目链接:   poj 1204 题目大意:   给出字典,再给出n个单词                   找出这些单词在单词表的什么位置,以什么顺序排放(8个方向,A表示正北,顺时针方向依次...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:POJ 1204(AC自动机)
举报原因:
原因补充:

(最多只允许输入30个字)