poj 1204——Word Puzzles
来源:互联网 发布:风景线打印软件注册 编辑:程序博客网 时间:2024/04/27 20:47
题意:在一个矩阵中找到模式串
思路:计算矩形四周的最长的串
错误:第一次提交的时候竟然忘记写失配函数了。。。
代码如下:
#include<iostream>#include<cstdio>#include<queue>#include<cstring>using namespace std;const int maxnode=1000*2001+5;const int sigma_size=26;const int dir[8][2]={-1,0,-1,1,0,1,1,1,1,0,1,-1,0,-1,-1,-1};char s[1001][2005];char sav[1005][1005];int idx(char c){return c-'A';}int f[maxnode];int last[maxnode]; int L,C,W;struct Ans{ int x; int y; int dd;};Ans ans[1005];Ans ss;struct Trie{ int ch[maxnode][sigma_size]; int val[maxnode]; int sz; void init() { memset(ch[0],0,sizeof(ch[0])); sz=1; memset(val,0,sizeof(val)); memset(f,0,sizeof(f)); memset(last,0,sizeof(last)); } void insert(const char *s,int v) { int u=0;int n=strlen(s); for(int i=0;i<n;++i) { int c=idx(s[i]); if(!ch[u][c]) { memset(ch[sz],0,sizeof(ch[sz])); val[sz]=0; ch[u][c]=sz++; } u=ch[u][c]; } val[u]=v; } void getFail() { queue<int> q; f[0]=0; for(int c=0;c<sigma_size;++c) { int u=ch[0][c]; if(u){f[u]=0;q.push(u);last[u]=0;} } while(!q.empty()) { int r=q.front();q.pop(); for(int c=0;c<sigma_size;++c) { int u=ch[r][c]; if(!u)continue; q.push(u); int v=f[r]; while(v && !ch[v][c])v=f[v]; f[u]=ch[v][c]; last[u] = val[f[u]]? f[u] : last[f[u]]; } } } void search(const int ii,const int jj,const int d,char *T) { int j=0; while(1) { int x=ii+dir[d][0]*j; int y=jj+dir[d][1]*j; if(!judge(x,y))break; T[j++]=sav[x][y]; } T[j]='\0'; int n=j; j=0; for(int i=0;i<n;++i) { int c=idx(T[i]); while(j&&!ch[j][c])j=f[j]; j=ch[j][c]; if(val[j])print(ii,jj,i,d,j); else if(last[j])print(ii,jj,i,d,last[j]); } } void print(const int x,const int y,const int i,const int d,int j) { if(j) { if(val[j]) {// cout<<val[j]<<endl; int v=val[j]; int n=strlen(s[v]); ans[v].x=x+(i-n+1)*dir[d][0]; ans[v].y=y+(i-n+1)*dir[d][1]; ans[v].dd=d; val[j]=0; } print(x,y,i,d,last[j]); } } bool judge(int x,int y){ if(x<0)return 0; if(y<0)return 0; if(x>=L)return 0; if(y>=C)return 0; return 1;}};Trie tr;int main(){// freopen("data.txt","r",stdin); tr.init(); scanf("%d%d%d",&L,&C,&W); for(int i=0;i<L;++i) { scanf("%s",sav[i]); } for(int i=0;i<W;++i) { scanf("%s",s[i+1]); tr.insert(s[i+1],i+1); } tr.getFail(); char T[1005]; for(int i=0;i<L;++i) { for(int d=0;d<8;++d) { tr.search(i,0,d,T); tr.search(i,C-1,d,T); } } for(int i=0;i<C;++i) { for(int d=0;d<8;++d) { tr.search(0,i,d,T); tr.search(L-1,i,d,T); } } for(int i=1;i<=W;++i) { printf("%d %d %c\n",ans[i].x,ans[i].y,'A'+ans[i].dd); } return 0;}
0 0
- poj 1204——Word Puzzles
- poj 1204 Word Puzzles
- poj 1204 Word Puzzles
- poj 1204 Word Puzzles
- POJ-1204-Word Puzzles
- poj 1204 Word Puzzles
- poj 1204 Word Puzzles
- 【POJ】1204 Word Puzzles
- ACM POJ 1204 (Word Puzzles)
- POJ 1204 Word Puzzles 笔记
- poj 1204 Word Puzzles 字典树
- poj 1204:Word Puzzles(AC自动机)
- poj 1204 Word Puzzles AC自动机
- poj 1204 Word Puzzles(字典树)
- POJ 1204 Word Puzzles AC自动机
- poj - 1204 - Word Puzzles(AC自动机)
- poj 1204 Word Puzzles (AC自动机)
- poj 1204 Word Puzzles 字典树 + 暴搜
- Lync Server 2010控制面板中启用并配置用户
- hadoop2.x常用端口、定义方法及默认端口、hadoop1.X端口对比
- greenrobot-common
- jQuery.validator验证无效的可能原因
- SQL Server 权限管理
- poj 1204——Word Puzzles
- 基础知识以及数组
- 【解惑】编程到底有什么用?
- 使用Lex和Yacc开发C语言的编译器
- 学习编程有什么前景?
- servlet web3.0文件上传
- 编程是什么?
- poj-1990
- 网络分析工具Wireshark Network Analyzer