【JZOJ 4910】子串
来源:互联网 发布:淘宝药店 提交需求 编辑:程序博客网 时间:2024/06/11 00:01
Description
Solution
有一个很显然的性质:如果a是吧的子串,b是c的子串,那么a一定是c的子串,
所以我们开一个栈,从后往前做,如当前串为栈顶子串就加入栈,否则就记录ans并弹出栈顶。
复杂度:
Code
#include <iostream>#include <cstdio>#include <cstdlib>#define fo(i,a,b) for(int i=a;i<=b;i++)#define fod(i,a,b) for(int i=a;i>=b;i--)using namespace std;const int N=501,M=2002;int n,m,ans,b0;int a[N][M];int nx[N][M];int za[N];bool OK(int Q,int W){ if(a[Q][0]<a[W][0])return 0; int q=0; fo(i,1,a[Q][0]) { while(q&&a[Q][i]!=a[W][q+1])q=nx[W][q]; if(a[Q][i]==a[W][q+1])q++; if(q>=a[W][0])return 1; } return q>=a[W][0];}int main(){ freopen("sub.in","r",stdin); freopen("sub.out","w",stdout); int q,w,_; scanf("%d",&_); while(_--) { scanf("%d",&n); fo(i,1,n) { char ch;a[i][0]=0; for(ch=getchar();ch<'a'||ch>'z';ch=getchar()); for(;ch<='z'&&'a'<=ch;ch=getchar())a[i][++a[i][0]]=ch-97; } fo(i,1,n) { q=0; fo(j,2,n) { while(q&&a[i][q+1]!=a[i][j])q=nx[i][q]; if(a[i][q+1]==a[i][j])q++; nx[i][j]=q; } } za[za[0]=1]=n; ans=-1; fod(i,n-1,1) { while(za[0]&&!OK(za[za[0]],i)) { ans=max(ans,za[za[0]]); za[0]--; } za[++za[0]]=i; } printf("%d\n",ans); } return 0;}
0 0
- 【JZOJ 4910】子串
- jzoj 1569. 【普及模拟】公共子串
- JZOJ 4910. 【NOIP2017模拟12.3】子串
- JZOJ 4910. 【NOIP2017模拟12.3】子串
- JZOJ 5132 子序列
- 【JZOJ 5132】 子序列
- jzoj 1569. 【普及模拟】公共子串 解题报告
- JZOJ.4486 【GDOI 2016 Day1】第二题 最长公共子串
- 【JZOJ 4486】【GDOI 2016 Day1】第二题 最长公共子串
- 【JZOJ 4915】最长不下降子序列
- JZOJ 3742. 【TJOI2014】上升子序列
- 【JZOJ 4676】 模板串
- 【挖坑记】JZOJ 4721 最长公共子序列
- JZOJ 4889 最长公共回文子序列(搜索)
- JZOJ 4920 降雷皇(最长上升子序列、线段树)
- JZOJ 4915. 【GDOI2017模拟12.9】最长不下降子序列
- JZOJ.2682 最长双回文串
- jzoj P2153【2017.7.7普及】串
- PostgreSql数据库安装详解
- HBase的应用程序
- Ubuntu下安装Shadowsocks-qt5
- 020 Valid Parentheses
- JAVA设计模式之适配器模式
- 【JZOJ 4910】子串
- 深度探索C语言函数可变长参数
- mysql多源复制
- IoC容器笔记2
- Android自定义View入门级
- docker相关技术
- Prime Ring Problem
- python学习_基础(一)
- 闪屏