洛谷传送门
BZOJ传送门
题目描述
标点符号的出现晚于文字的出现,所以以前的语言都是没有标点的。现在你要处理的就是一段没有标点的文章。
一段文章 是由若干小写字母构成。一个单词 也是由若干小写字母构成。一个字典 是若干个单词的集合。我们称一段文章 在某个字典 下是可以被理解的,是指如果文章 可以被分成若干部分,且每一个部分都是字典 中的单词。
例如字典
中包括单词{‘is’, ‘name’, ‘what’, ‘your’}
,则文章‘whatisyourname’
是在字典
下可以被理解的,因为它可以分成
个单词:‘what’, ‘is’, ‘your’, ‘name’
,且每个单词都属于字典
,而文章‘whatisyouname’
在字典
下不能被理解,但可以在字典
下被理解。这段文章的一个前缀‘whatis’
,也可以在字典
下被理解,而且是在字典
下能够被理解的最长的前缀。
给定一个字典 ,你的程序需要判断若干段文章在字典 下是否能够被理解。并给出其在字典 下能够被理解的最长前缀的位置。
输入输出格式
输入格式:
输入文件第一行是两个正整数 和 ,表示字典 中有 个单词,且有 段文章需要被处理。之后的 行每行描述一个单词,再之后的 行每行描述一段文章。
其中 ,每个单词长度不超过 ,每段文章长度不超过 。
输出格式:
对于输入的每一段文章,你需要输出这段文章在字典 可以被理解的最长前缀的位置。
输入输出样例
输入样例#1:
4 3
is
name
what
your
whatisyourname
whatisyouname
whaisyourname
输出样例#1:
14 (整段文章’whatisyourname’都能被理解)
6 (前缀’whatis’能够被理解)
0 (没有任何前缀能够被理解)
解题分析
考虑到模式串数量很少, 直接匹配出哪些位置可以被模式串覆盖, 然后建图连边, 记录可以到达的最远位置即可。
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <queue>
#define R register
#define IN inline
#define W while
#define gc getchar()
#define MX 1205000
std::queue <int> q;
int n, cnt, root, ct, m;
bool vis[MX]; char buf[MX];
int son[550][26], fail[550], head[MX], l[550];
struct Edge {int to, nex;} edge[MX];
template <class T> IN T max(T a, T b) {return a > b ? a : b;}
IN void add(R int from, R int to) {edge[++cnt] = {to, head[from]}, head[from] = cnt;}
IN void insert(char *str)
{
R int len = std::strlen(str), now = root, id;
for (R int i = 0; i < len; ++i)
{
id = str[i] - 'a';
if (!son[now][id]) son[now][id] = ++ct;
now = son[now][id];
}
l[now] = len;
}
IN void build()
{
fail[0] = -1;
for (R int i = 0; i < 26; ++i) if (son[0][i]) q.push(son[0][i]);
R int now;
W (!q.empty())
{
now = q.front(); q.pop();
for (R int i = 0; i < 26; ++i)
{
if (son[now][i]) fail[son[now][i]] = son[fail[now]][i], q.push(son[now][i]);
else son[now][i] = son[fail[now]][i];
}
}
}
IN void query(char *str)
{
R int len = std::strlen(str), now = root, cur, id, ans = 0;
int a, b;
for (R int i = len + 1; ~i; --i) vis[i] = false, head[i] = 0;
cnt = 0;
for (R int i = 0; i < len; ++i)
{
id = str[i] - 'a';
now = cur = son[now][id];
W (~fail[cur])
{
if (l[cur]) add(i - l[cur] + 1, i + 1);
cur = fail[cur];
}
}
q.push(0);
W (!q.empty())
{
now = q.front(); q.pop(); ans = max(ans, now);
for (R int i = head[now]; i; i = edge[i].nex)
if (!vis[edge[i].to]) vis[edge[i].to] = true, q.push(edge[i].to);
}
printf("%d\n", ans);
}
int main(void)
{
scanf("%d%d", &n, &m);
for (R int i = 1; i <= n; ++i) scanf("%s", buf), insert(buf);
build();
for (R int i = 1; i <= m; ++i) scanf("%s", buf), query(buf);
}