POJ1204 Word Puzzles

传送门

这题果然是AC自动机的大好题!

题目的大意是,给定一个l*c的大网格,每个格子里有一个字符,每个格子可以向八个方向形成字符串,问给定的字符串在哪里能被匹配以及在网格中出现的方向(A代表北,然后依次顺时针转)

解题思路还好想,而且特别暴力:把给定的字符串建成一个AC自动机,之后对于大网格,把八个方向上所能形成的每一个串(一行,列,对角线)都视为文本串放进去匹配,然后记录一下匹配到的位置。

然而听着你就不想写对不对?!

不过其实还好,把AC自动机建出来之后,我们没必要写8种情况,我们开一个像宽搜一样记录当前方向下一步与当前值的横纵坐标之差,之后我们在匹配的时候,我们把起始点的坐标和匹配方向传入,在里面像bfs一样一直往下匹配,匹配到了就把答案记录一下。

传入起始点的话我们需要枚举每一个边上的点,枚举每一个方向。

还有就是这个题的数据范围明显暧昧不清,都不告诉单词有多长,一开始我数组开小RE了,然后调大(大概600000)才过的。

我不会告诉你我一开始没搜出来是因为方向写反了

代码其实不长,真的,才120行。

 

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<set>
#include<vector>
#include<queue>
#define pb push_back
#define rep(i,a,n) for(int i = a;i <= n;i++)
#define per(i,n,a) for(int i = n;i >= a;i--)
#define enter putchar('\n')using namespace std;
typedef long long ll;
const int M = 40005;
const int N = 600005;
const ll mod = 1000000007;int read()
{int ans = 0,op = 1;char ch = getchar();while(ch < '0' || ch > '9'){if(ch == '-') op = -1;ch = getchar();}while(ch >= '0' && ch <= '9'){ans *= 10;ans += ch - '0';ch = getchar();}return ans * op;
}queue <int> q;struct ans
{int id,x,y,fx,fy,nd;char di;
}a[1005];char s[1005][1005],b[1005];
int l,C,w,dx[9] = {0,-1,-1,0,1,1,1,0,-1},dy[9] = {0,0,1,1,1,0,-1,-1,-1};struct ACG
{int c[N][26],val[N],id[N],cnt,fail[N],le[N];void insert(char *p,int d){int l = strlen(p),u = 0;rep(i,0,l-1){int v = p[i] - 'A';if(!c[u][v]) c[u][v] = ++cnt;u = c[u][v];}val[u]++,id[u] = d,le[u] = l-1;}void getfail(){rep(i,0,25) if(c[0][i]) fail[c[0][i]] = 0,q.push(c[0][i]);while(!q.empty()){int k = q.front();q.pop();rep(i,0,25){if(c[k][i]) fail[c[k][i]] = c[fail[k]][i],q.push(c[k][i]);else c[k][i] = c[fail[k]][i];}}}void query(int posx,int posy,int dir){int kx = posx,ky = posy,u = 0;while(1){int v = s[kx][ky] - 'A';u = c[u][v];for(int t = u;t && val[t] != -1;t = fail[t]){if(val[t]){int px = kx - le[t] * dx[dir],py = ky - le[t] * dy[dir];a[id[t]].x = px,a[id[t]].y = py,a[id[t]].di = dir + 'A' - 1;a[id[t]].fx = posx,a[id[t]].fy = posy,a[id[t]].nd = dir;val[t] = -1;}}kx += dx[dir],ky += dy[dir];if(kx < 0 || kx > l-1 || ky < 0 || ky > C-1) break;}}
}AC;
int main()
{l = read(),C = read(),w = read();rep(i,0,l-1) scanf("%s",s[i]);rep(i,1,w) scanf("%s",b),AC.insert(b,i);AC.getfail();rep(i,0,l-1){AC.query(i,0,3),AC.query(i,C-1,7);//k = 0AC.query(i,0,2),AC.query(i,C-1,6);//k = 1AC.query(i,0,4),AC.query(i,C-1,8);//k = -1
    }rep(i,0,C-1){AC.query(0,i,5),AC.query(l-1,i,1);//k = INFAC.query(0,i,6),AC.query(l-1,i,2);//k = 1AC.query(0,i,4),AC.query(l-1,i,8);//k = -1
    }rep(i,1,w) printf("%d %d %c\n",a[i].x,a[i].y,a[i].di);//%d %d %d\n",a[i].x,a[i].y,a[i].di,a[i].fx,a[i].fy,a[i].nd);return 0;
}

 

转载于:https://www.cnblogs.com/captain1/p/9770070.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/388254.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

普通话测试系统_普通话

普通话测试系统Traduzido/adaptado do original por Vincius Barqueiro a partir do texto original “Writing Alt Text for Data Visualization”, escrito por Amy Cesal e publicado no blog Nightingale.Traduzido / adaptado由 VinciusBarqueiro 提供原始 文本“为数据可…

Mac OS 被XCode搞到无法正常开机怎么办?

Mac OS 被XCode搞到无法正常开机怎么办&#xff1f; 第一天拿到这台air的时候&#xff0c;迫不及待地把从别处搜集来的XCode 3.2.5iOS SDK 4.1的dmg安装了上来&#xff0c;结果系统直接崩溃&#xff0c;再开机就不能正常开机&#xff0c;总是碰到kernel panic。这不坑爹吗…… …

美国队长3:内战_隐藏的宝石:寻找美国最好的秘密线索

美国队长3:内战There are plenty of reasons why one would want to find solitude in the wilderness, from the therapeutic effects of being immersed in nature, to not wanting to contribute to trail degradation and soil erosion on busier trails.人们有很多理由想要…

Java入门第三季——Java中的集合框架(中):MapHashMap

1 package com.imooc.collection;2 3 import java.util.HashSet;4 import java.util.Set;5 6 /**7 * 学生类8 * author Administrator9 * 10 */ 11 public class Student { 12 13 public String id; 14 15 public String name; 16 17 public Set<…

【译】 WebSocket 协议第八章——错误处理(Error Handling)

概述 本文为 WebSocket 协议的第八章&#xff0c;本文翻译的主要内容为 WebSocket 错误处理相关内容。 错误处理&#xff08;协议正文&#xff09; 8.1 处理 UTF-8 数据错误 当终端按照 UTF-8 的格式来解析一个字节流&#xff0c;但是发现这个字节流不是 UTF-8 编码&#xff0c…

升级xcode5.1 iOS 6.0后以前的横屏项目 变为了竖屏

升级xcode5.1 iOS 6.0后以前的横屏项目 变为了竖屏&#xff0c;以下为解决办法&#xff1a; 在AppDelegate 的初始化方法 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions中 将 [window addSubview: viewCon…

动漫数据推荐系统

Simple, TfidfVectorizer and CountVectorizer recommendation system for beginner.简单的TfidfVectorizer和CountVectorizer推荐系统&#xff0c;适用于初学者。 目标 (The Goal) Recommendation system is widely use in many industries to suggest items to customers. F…

Wait Event SQL*Net more data to client

oracle 官方给的说法是 C.3.152 SQL*Net more data to client The server process is sending more data/messages to the client. The previous operation to the client was also a send. Wait Time: The actual time it took for the send to complete 意味着server process…

1.3求根之牛顿迭代法

目录 目录前言&#xff08;一&#xff09;牛顿迭代法的分析1.定义2.条件3.思想4.误差&#xff08;二&#xff09;代码实现1.算法流程图2.源代码&#xff08;三&#xff09;案例演示1.求解&#xff1a;\(f(x)x^3-x-10\)2.求解&#xff1a;\(f(x)x^2-1150\)3.求解&#xff1a;\(f…

libzbar.a armv7

杨航最近在学IOS&#xfeff;&#xfeff; http://download.csdn.net/download/lzwxyz/5546365 我现在用的是这个&#xff1a;http://www.federicocappelli.net/2012/10/05/zbar-library-for-iphone-5-armv7s/ 点它的HERE开始下载 下载的libzbar.a库&#xff0c;如何查看 …

Alex Hanna博士:Google道德AI小组研究员

Alex Hanna博士是社会学家和研究科学家&#xff0c;致力于Google的机器学习公平性和道德AI。 (Dr. Alex Hanna is a sociologist and research scientist working on machine learning fairness and ethical AI at Google.) Before that, she was an Assistant Professor at th…

三位对我影响最深的老师

我感觉&#xff0c;教过我的老师们&#xff0c;不论他们技术的好坏对我都是有些许影响的。但是让人印象最深的好像只有寥寥几位。 第一位就是小学六年级下册教过我的语文老师。他是临时从一个贫困小学调任过来的&#xff0c;不怎么管班级&#xff0c;班里同学都在背地里说他不会…

安全开发 | 如何让Django框架中的CSRF_Token的值每次请求都不一样

前言 用过Django 进行开发的同学都知道&#xff0c;Django框架天然支持对CSRF攻击的防护&#xff0c;因为其内置了一个名为CsrfViewMiddleware的中间件&#xff0c;其基于Cookie方式的防护原理&#xff0c;相比基于session的方式&#xff0c;更适合目前前后端分离的业务场景&am…

UNITY3D 脑袋顶血顶名

&#xfeff;&#xfeff;杨航最近在学Unity3D&#xfeff;&#xfeff; using UnityEngine; using System.Collections; public class NPC : MonoBehaviour { //主摄像机对象 public Camera camera; //NPC名称 private string name "我是doud…

一个项目的整个测试流程

最近一直在进行接口自动化的测试工作&#xff0c;同时对于一个项目的整个测试流程进行了梳理&#xff0c;希望能对你有用~~~ 需求分析&#xff1a; 整体流程图&#xff1a; 需求提取 -> 需求分析 -> 需求评审 -> 更新后的测试需求跟踪xmind 分析流程&#xff1a; 1. 需…

python度量学习_Python的差异度量

python度量学习Hi folks, welcome back to my new edition of the blog, thank you so much for your love and support, I hope you all are doing well. In today’s learning, we will try to understand about variance and the measures involved in it. Although the blo…

多个摄像机之间的切换

杨航最近在学Unity3D&#xfeff;&#xfeff; Unity3D入门 第捌章: 多个摄像机之间的切换 内容描述&#xff1a;这章&#xff0c;我们来学习一下同个场景中多个摄像机怎么切换。 接着我们创建一个空对象 GameObject -> Create Empty 命名为CamearController&#xff0…

Kubernetes的共享GPU集群调度

问题背景 全球主要的容器集群服务厂商的Kubernetes服务都提供了Nvidia GPU容器调度能力&#xff0c;但是通常都是将一个GPU卡分配给一个容器。这可以实现比较好的隔离性&#xff0c;确保使用GPU的应用不会被其他应用影响&#xff1b;对于深度学习模型训练的场景非常适合&#x…

django-celery定时任务以及异步任务and服务器部署并且运行全部过程

Celery 应用Celery之前&#xff0c;我想大家都已经了解了&#xff0c;什么是Celery&#xff0c;Celery可以做什么&#xff0c;等等一些关于Celery的问题&#xff0c;在这里我就不一一解释了。 应用之前&#xff0c;要确保环境中添加了Celery包。 pip install celery pip instal…

网页视频15分钟自动暂停_在15分钟内学习网页爬取

网页视频15分钟自动暂停什么是网页抓取&#xff1f; (What is Web Scraping?) Web scraping, also known as web data extraction, is the process of retrieving or “scraping” data from a website. This information is collected and then exported into a format that …