FZU2128_最长子串
题目说给你一个长串,要你选一个最长子串,不包括任何一个给定串为子串。
建立一个自动机,每个点保存的信息为当前这个状态为结尾最长可以有多长?
然后。。。。就可以了。。。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define maxn 1000100
#define inf 99999999
using namespace std; char s0[maxn];
char s[];
int next[][],fail[],tag[],N;
int n,m,f[maxn],ans; int add()
{
N++;
for (int i=; i<; i++) next[N][i]=;
tag[N]=inf;
fail[N]=;
return N;
} void init()
{
N=-;
N=add();
} void insert()
{
int cur=,tep;
for (int i=; s[i]; i++)
{
tep=s[i]-'a';
if (next[cur][tep]==) next[cur][tep]=add();
cur=next[cur][tep];
}
tag[cur]=strlen(s);
} void AC_build()
{
int cur,child;
queue<int> Q;
Q.push();
while (!Q.empty())
{
cur=Q.front(),Q.pop();
for (int i=; i<; i++)
{
child=next[cur][i];
if (child)
{
Q.push(child);
if (cur==) fail[child]=;
else fail[child]=next[fail[cur]][i];
tag[child]=min(tag[child],tag[fail[child]]);
}
else next[cur][i]=next[fail[cur]][i];
}
}
} void query()
{
ans=;
f[]=;
int cur=,tep;
for (int i=; s0[i]; i++)
{
f[i]=f[i-]+;
tep=s0[i]-'a';
cur=next[cur][tep];
f[i]=min(f[i],tag[cur]);
ans=max(ans,f[i]);
}
} int main()
{
while (scanf("%s",s0+)!=EOF)
{
init();
scanf("%d",&n);
while (n--)
{
scanf("%s",s);
insert();
}
AC_build();
query();
printf("%d\n",ans-);
}
return ;
}
FZU2128_最长子串的更多相关文章
- [LeetCode] Longest Substring with At Most Two Distinct Characters 最多有两个不同字符的最长子串
Given a string S, find the length of the longest substring T that contains at most two distinct char ...
- POJ 3294 Life Forms 后缀数组+二分 求至少k个字符串中包含的最长子串
Life Forms Description You may have wondered why most extraterrestrial life forms resemble humans, ...
- LeetCode: 3_Longest Substring Without Repeating Characters | 求没有重复字符的最长子串的长度 | Medium
题目: Given a . For . 解题思路: 这个题让找一个字符串中具有不重复单词的最长子串的长度,如:ababc,子串为abc,长度为3.有这么几个方法: 方法一: 依赖字符串本身的一些特有函 ...
- [getLongestLength] 加和为0的最长子串长度
点击这里查看原文 假设一个数组仅仅由1和-1组成,求该数组的和为0的最长子串的长度. 例如: {1,-1,1,-1,1,1,1} 输出:4. 昨天机试的时候做到这道题,不会做,今天思考一下. 普通的解 ...
- POJ-3294-Life Forms(后缀数组-不小于 k 个字符串中的最长子串)
题意: 给定 n 个字符串,求出现在不小于 k 个字符串中的最长子串. 分析: 将 n 个字符串连起来,中间用不相同的且没有出现在字符串中的字符隔开,求后缀数组. 然后二分答案,将后缀分成若干组,判断 ...
- 求两个字符串最长子串的LCS算法 C语言实现(简短的实现函数)
/************************************************************************* > File Name: lcs.c > ...
- Problem 2128 最长子串(kmp+strstr好题经典)
Problem 2128 最长子串 Accept: 134 Submit: 523Time Limit: 3000 mSec Memory Limit : 65536 KB Probl ...
- fzu Problem 2128 最长子串(KMP + strstr 经典好题)
Problem Description 问题很简单,给你一个字符串s,问s的子串中不包含s1,s2...sn的最长串有多长. Input 输入包含多组数据.第一行为字符串s,字符串s的长度1到10 ...
- SPOJ PHRASES 每个字符串至少出现两次且不重叠的最长子串
Description You are the King of Byteland. Your agents have just intercepted a batch of encrypted ene ...
随机推荐
- 20155328 2016-2017-2 《Java程序设计》 课程总结
20155328 2016-2017-2 <Java程序设计> 课程总结 目录 一.每周作业链接汇总 二.实验报告链接汇总 三.代码托管链接 四.课堂项目实践 五.课程收获与不足 六.问卷 ...
- asp.net self host and urlacl(解决UnHandledException Message:拒绝访问的问题)
命令提示符(管理员权限)需要添加的代码: netsh http add urlacl url=http://*:9999/ user=Everyone listen=yes 其中: url:代码中的u ...
- c++ 全局变量
一.全局变量 声明 全局变量的定义和一般变量定义相同,不同的就是它的位置.一般会放在所有共享函数的前边. 作用 在函数间共享数据. 二.全局变量的运用 三.作业: 写出代码运行结果: ; //0 in ...
- Linux☞权限数字表示法
权限数字表示法: 1.Linux有三种访问权限: a.可读:r(Read) b.可写:w(Write) c.可执行:x(eXcute) 2.简单说说如何去看该文件的访问权限呢?一般我们执行命令,查看目 ...
- 08-base镜像
base 镜像有两层含义: 不依赖其他镜像,从 scratch 构建. 其他镜像可以之为基础进行扩展. 所以,能称作 base 镜像的通常都是各种 Linux 发行版的 Docker 镜像,比如 Ub ...
- TW实习日记:第八天
今天早上主要是接着做昨天的微信端网页预览附件,听同事说当打包代码放入服务器上后,就不存在跨域问题了,也就懒得自己写接口了,那么就希望自己能一次过吧...结果写着写着,发现开发文档中关于预览文件的方法, ...
- mongodb复制集部署文档
一.安装SNMP(新版mongodb需要此依赖安装) 安装snmp服务需要的rpm包: perl-Data-Dumper-2.145-3.el7.x86_64.rpm net-snmp-5.7.2-2 ...
- 420. Count and Say【LintCode java】
Description The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, ...
- PHP核心技术——魔术方法
魔术方法: 魔术方法是以两个下画线开头.具有特殊作用的一些方法,可以看做PHP的"语法糖". set和get方法: class Account{ private $user=1; ...
- 4星|《亿万》:FBI大战华尔街对冲基金大鳄
亿万:围剿华尔街大白鲨 全书尝试还原2008-2013年前后FBI指控赛克资本老板科恩通过内幕交易盈利的案件细节. 作者花了数年时间,采访了200多位当事人,阅读了海量的相关资料.书中交代了科恩的发家 ...