Time Limit: 5000MS | Memory Limit: 65536K | |||
Total Submissions: 5727 | Accepted: 2173 | Special Judge |
Description
Input
Output
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
题意:有W个单词,一个字符矩阵(L*C),从8个方向查找矩阵,找出每个 单词在矩阵出现的起始位置,与及其方向。
源代码:(1594MS)
#include<iostream> #include<string.h> using namespace std; const int MAX=1005; const int NODEMAX=26; char word[MAX][MAX],s[MAX][MAX]; int L,C,W,dir[8][2]={{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1}}; struct Node { int c,r; char dir; }rec[MAX]; struct TrieNode { int num,count; bool vis; struct TrieNode *fail; //失败指针 struct TrieNode *next[NODEMAX];//26个字母指针域 TrieNode() { num=0; count=0; fail=NULL; memset(next,NULL,sizeof(next)); } }; TrieNode *q[500005];//队列 TrieNode *pRoot; void InsertTrie(TrieNode *pRoot,char s[],int num)//插入单词 { TrieNode *p=pRoot; if(p==NULL) p=pRoot=new TrieNode(); int i=0; while(s[i]) { int k=s[i]-'A'; if(p->next[k]==NULL) p->next[k]=new TrieNode(); i++; p=p->next[k]; } p->num=num; p->count++; } void build_ac_automation(TrieNode *pRoot) { int head=0,tail=0; TrieNode *p=pRoot; p->fail=NULL; q[tail++]=pRoot; while(head!=tail) { TrieNode *tmp=q[head++]; for(int i=0;i<NODEMAX;i++)//设置tmp的孩子的fail指针 if(tmp->next[i]!=NULL) { if(tmp==pRoot)//根的孩子的fail指针指向根 tmp->next[i]->fail=pRoot; else { p=tmp->fail;//递增的 while(p!=NULL) { if(p->next[i]!=NULL) { tmp->next[i]->fail=p->next[i]; break; } p=p->fail; } if(p==NULL) tmp->next[i]->fail=pRoot;//找到,设置为根 } q[tail++]=tmp->next[i]; //入队 } } } int Search(TrieNode *pRoot,char ch,int r,int c,int di) { int cnt=0,k,i; TrieNode *p,*tmp; p=pRoot; i=0; while(s[r][c]) { k=s[r][c]-'A'; while(p->next[k]==NULL && p!=pRoot)//往p的失败指针回找,直至找到或返回根 p=p->fail; p=p->next[k]; //一种是不通过根找到,另外或者可以通过根找到,或者为空 if(p==NULL) p=pRoot; //空,根本找不到 //可以往下找 tmp=p; while(tmp!=pRoot && tmp->count!=-1)//以s[i]结尾的单词,通过tmp指针全部找出 { cnt += tmp->count; if(tmp->count>0) { int temp=tmp->num; int len=strlen(word[temp]); rec[temp].dir=ch; rec[temp].r=r-dir[di][0]*(len-1); rec[temp].c=c-dir[di][1]*(len-1); } tmp->count=-1; tmp=tmp->fail; } r += dir[di][0]; c += dir[di][1]; } return cnt; } void Solve() { int i,j; //....A.. for(j=1;j<=C;j++) Search(pRoot,'A',L,j,0); //....B... for(i=1;i<=L;i++) Search(pRoot,'B',i,1,1); for(j=2;j<=C;j++) Search(pRoot,'B',L,j,1); //.....C...... for(i=1;i<=L;i++) Search(pRoot,'C',i,1,2); //.....D..... for(i=1;i<=L;i++) Search(pRoot,'D',i,1,3); for(j=2;j<=C;j++) Search(pRoot,'D',1,j,3); //....E... for(j=1;j<=C;j++) Search(pRoot,'E',1,j,4); //.....F.... for(j=1;j<=C;j++) Search(pRoot,'F',1,j,5); for(i=1;i<=L;i++) Search(pRoot,'F',i,C,5); //.....G...... for(i=1;i<=L;i++) Search(pRoot,'G',i,C,6); //.....H..... for(i=1;i<=L;i++) Search(pRoot,'H',i,C,7); for(j=1;j<=C;j++) Search(pRoot,'H',L,j,7); for(i=1;i<=W;i++) cout<<rec[i].r-1<<" "<<rec[i].c-1<<" "<<rec[i].dir<<endl; } int main() { int i,j; while(scanf("%d%d%d",&L,&C,&W)!=EOF) { pRoot=new TrieNode(); memset(s,0,sizeof(s)); for(i=1;i<=L;i++) { getchar(); for(j=1;j<=C;j++) s[i][j]=getchar(); } getchar(); for(i=1;i<=W;i++) { gets(word[i]); InsertTrie(pRoot,word[i],i); } build_ac_automation(pRoot); Solve(); } return 0; }