【 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+爆搜能过,于是我就爆搜了。

 

代码如下:

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cstring>
 4 #include<iostream>
 5 #include<algorithm>
 6 #include<queue>
 7 using namespace std;
 8 #define Maxn 1010
 9 #define Maxl 1010
10 
11 char s[Maxl][Maxl];
12 char ss[Maxl][Maxl];
13 int dx[10]={0,1,1,0,-1,-1,-1,0,1},
14     dy[10]={0,0,-1,-1,-1,0,1,1,1};
15 int ans[Maxn][2],dr[Maxn];
16 int n,m;
17 
18 struct node
19 {
20     int son[30],cnt,fail;
21     int num,rt,p;
22 }t[Maxn*1000];int tot;
23 
24 void upd(int x)
25 {
26     t[x].cnt=0;t[x].p=0;
27     memset(t[x].son,0,sizeof(t[x].son));
28 }
29 
30 void read_trie(int x)
31 {
32     scanf("%s",ss[x]+1);
33     int len=strlen(ss[x]+1);
34     int now=0;
35     for(int j=1;j<=len;j++) ss[0][j]=ss[x][len-j+1];
36     for(int j=1;j<=len;j++)
37     {
38         int ind=ss[0][j]-'A'+1;
39         if(!t[now].son[ind])
40          t[now].son[ind]=++tot,upd(tot); 
41         now=t[now].son[ind];
42         if(j==len) t[now].cnt++,t[now].p=x;
43     }
44 }
45 
46 void dfs(int x,int y,int dir,int now)
47 {
48     if(t[now].p!=0)
49     {
50         int q=t[now].p;
51         ans[q][0]=x;ans[q][1]=y;dr[q]=dir;
52         t[now].p=0;
53     }
54     if(x+dx[dir]<1||x+dx[dir]>n||y+dy[dir]<1||y+dy[dir]>m) return;
55     int a=t[now].son[s[x+dx[dir]][y+dy[dir]]-'A'+1];
56     if(a!=0)
57         dfs(x+dx[dir],y+dy[dir],dir,a);
58 }
59 
60 void init()
61 {
62     int q;
63     scanf("%d%d%d",&n,&m,&q);
64     for(int i=1;i<=n;i++)
65     {
66         scanf("%s",s[i]+1);
67     }
68     tot=0;upd(0);
69     for(int i=1;i<=q;i++)
70     {
71         read_trie(i);
72     }
73     for(int i=0;i<=n+1;i++)
74      for(int j=0;j<=m+1;j++)
75      {
76          for(int k=1;k<=8;k++)
77          {
78             dfs(i,j,k,0);
79          }
80      }
81     for(int i=1;i<=q;i++)
82     {
83         printf("%d %d %c\n",ans[i][0]-1,ans[i][1]-1,dr[i]+'A'-1);
84     }
85 }
86 
87 int main()
88 {
89     init();
90     return 0;
91 }
[POJ1204]

 

2016-06-19 14:09:06

 


 


 

后来还是打了一下AC自动机,嗯,感觉for那里还是超级慢,因为重叠那部分不好搞,就这样吧。

 

代码如下:

  1 #include<cstdio>
  2 #include<cstdlib>
  3 #include<cstring>
  4 #include<iostream>
  5 #include<algorithm>
  6 #include<queue>
  7 using namespace std;
  8 #define Maxn 1010
  9 #define Maxl 1010
 10 
 11 char s[Maxl][Maxl];
 12 char ss[Maxl][Maxl];
 13 int dx[10]={0,1,1,0,-1,-1,-1,0,1},
 14     dy[10]={0,0,-1,-1,-1,0,1,1,1};
 15 int ans[Maxn][2],dr[Maxn];
 16 int n,m,qy;
 17 
 18 struct node
 19 {
 20     int son[30],cnt,fail;
 21     int num,rt;
 22 }t[Maxn*1000];int tot;
 23 
 24 int p[Maxn];
 25 
 26 void upd(int x)
 27 {
 28     t[x].cnt=0;/*t[x].p=0;*/
 29     memset(t[x].son,0,sizeof(t[x].son));
 30 }
 31 
 32 void read_trie(int x)
 33 {
 34     scanf("%s",ss[x]+1);
 35     int len=strlen(ss[x]+1);
 36     int now=0;
 37     for(int j=1;j<=len;j++) ss[0][j]=ss[x][len-j+1];
 38     for(int j=1;j<=len;j++)
 39     {
 40         int ind=ss[0][j]-'A'+1;
 41         if(!t[now].son[ind])
 42          t[now].son[ind]=++tot,upd(tot); 
 43         now=t[now].son[ind];
 44         if(j==len) t[now].cnt++,p[x]=now;
 45     }
 46 }
 47 
 48 queue<int > q;
 49 void build_AC()
 50 {
 51     int i,j,x,y;
 52     while(!q.empty()) q.pop();
 53     q.push(0);
 54     while(!q.empty())
 55     {
 56         x=q.front();
 57         y=t[x].fail;
 58         for(j=1;j<=26;j++)
 59         {
 60             if(t[x].son[j])
 61             {
 62                 q.push(t[x].son[j]);
 63                 t[t[x].son[j]].fail=x?t[y].son[j]:x;
 64             }
 65             else t[x].son[j]=t[y].son[j];
 66         }
 67         q.pop();
 68     }
 69 }
 70 
 71 void check(int x,int y,int dir,int now)
 72 {
 73     if(now==0) return;
 74     for(int i=1;i<=qy;i++) if(p[i]==now)
 75     {
 76         ans[i][0]=x;
 77         ans[i][1]=y;
 78         dr[i]=dir;
 79     }
 80     now=t[now].fail;
 81     check(x,y,dir,now);
 82 }
 83 
 84 void dfs(int x,int y,int dir,int now)
 85 {
 86     now=t[now].son[s[x][y]-'A'+1];
 87     check(x,y,dir,now);
 88     x=x+dx[dir],y=y+dy[dir];
 89     if(x<1||x>n||y<1||y>m) return;
 90     dfs(x,y,dir,now);
 91     
 92 }
 93 
 94 void init()
 95 {
 96     scanf("%d%d%d",&n,&m,&qy);
 97     for(int i=1;i<=n;i++)
 98     {
 99         scanf("%s",s[i]+1);
100     }
101     tot=0;upd(0);
102     for(int i=1;i<=qy;i++)
103     {
104         read_trie(i);
105     }
106     build_AC();
107     for(int i=1;i<=n;i++)
108      for(int k=1;k<=8;k++)
109       {
110           dfs(i,1,k,0);
111           dfs(i,m,k,0);
112       }
113      for(int j=1;j<=m;j++)
114       for(int k=1;k<=8;k++)
115        {
116           dfs(1,j,k,0);
117           dfs(n,j,k,0);
118        }
119     for(int i=1;i<=qy;i++)
120     {
121         printf("%d %d %c\n",ans[i][0]-1,ans[i][1]-1,dr[i]+'A'-1);
122     }
123 }
124 
125 int main()
126 {
127     init();
128     return 0;
129 }
[POJ1204-2]

 

2016-06-21 17:14:17


 

posted @ 2016-06-18 10:32  konjak魔芋  阅读(323)  评论(0编辑  收藏  举报