[CTSC 2012] Cheat
[题目链接]
https://www.lydsy.com/JudgeOnline/problem.php?id=2806
[算法]
首先建立广义后缀自动机
注意到问题具有单调性 , 不妨对于每组询问二分答案mid
如何检验?
记fi表示前i个字符最多能选几个 , 有转移方程 :
fi = max{ fi - 1 , fj + i - j } (i - maxlen[i] <= j <= i - mid)
其中maxlen[i]表示第i个字符向前最多可匹配多少个字符
i - maxlen[i]单调递增 , i - mid同样单调递增
单调队列优化即可
时间复杂度 : O(NlogN)
[代码]
#include<bits/stdc++.h>
using namespace std;
#define N 1100010 int n , m , L;
int dp[N] , maxlen[N] , q[N];
char s[N]; #define rint register int struct Suffix_Automaton
{
int sz , last;
int father[N] , child[N][] , depth[N];
Suffix_Automaton()
{
sz = ;
last = ;
}
inline int new_node(int dep)
{
depth[++sz] = dep;
return sz;
}
inline void extend(int ch)
{
int np = new_node(depth[last] + );
int p = last;
while (child[p][ch] == )
{
child[p][ch] = np;
p = father[p];
}
if (child[p][ch] == np) father[np] = ;
else
{
int q = child[p][ch];
if (depth[q] == depth[p] + ) father[np] = q;
else
{
int nq = new_node(depth[p] + );
father[nq] = father[q];
father[np] = father[q] = nq;
memcpy(child[nq] , child[q] , sizeof(child[q]));
while (child[p][ch] == q)
{
child[p][ch] = nq;
p = father[p];
}
}
}
last = np;
}
inline void match()
{
int now = , mxlen = ;
for (rint i = ; i <= L; ++i)
{
int nxt = s[i] - '';
while (now != && !child[now][nxt]) now = father[now] , mxlen = depth[now];
if (child[now][nxt])
{
++mxlen;
now = child[now][nxt];
} else
{
mxlen = ;
now = ;
}
maxlen[i] = mxlen;
}
}
} SAM; inline void chkmin(int &x , int y)
{
x = min(x , y);
}
inline void chkmax(int &x , int y)
{
x = max(x , y);
}
inline bool check(int mid)
{
int l = , r = ;
for (rint i = ; i < mid; ++i) dp[i] = ;
for (rint i = mid; i <= L; ++i)
{
while (l <= r && dp[q[r]] - q[r] <= dp[i - mid] - i + mid) --r;
q[++r] = i - mid;
while (l <= r && q[l] < i - maxlen[i]) ++l;
dp[i] = dp[i - ];
if (l <= r) chkmax(dp[i] , dp[q[l]] - q[l] + i);
}
return dp[L] * >= * L;
} int main()
{ scanf("%d%d" , &n , &m);
int mxl = ;
for (rint i = ; i <= m; ++i)
{
scanf("%s" , s + );
L = strlen(s + );
chkmax(mxl , L);
for (rint j = ; j <= L; ++j) SAM.extend(s[j] - '');
SAM.extend();
}
for (rint i = ; i <= n; ++i)
{
scanf("%s" , s + );
int l = , r = strlen(s + ) , ans = ;
L = r;
chkmin(r , mxl);
int now = , mxlen = ;
SAM.match();
while (l <= r)
{
int mid = (l + r) >> ;
if (check(mid))
{
ans = mid;
l = mid + ;
} else r = mid - ;
}
printf("%d\n" , ans);
} return ;
}
[CTSC 2012] Cheat的更多相关文章
- [CTSC 2012][BZOJ 2806]Cheat
真是一道好题喵~ 果然自动机什么的就是要和 dp 搞基才是王道有木有! A:连 CTSC 都叫我们搞基,果然身为一个程序猿,加入 FFF 团是我此生最明智的选择.妹子什么闪边去,大家一起来搞基吧! Q ...
- [CTSC 2012]熟悉的文章
二分+单调队列优化dp+后缀自动机 //CTSC2012 熟悉的文章 #include <bits/stdc++.h> using namespace std; const int max ...
- IOS Application Security Testing Cheat Sheet
IOS Application Security Testing Cheat Sheet [hide] 1 DRAFT CHEAT SHEET - WORK IN PROGRESS 2 Int ...
- [bzoj2806][Ctsc2012]Cheat(后缀自动机(SAM)+二分答案+单调队列优化dp)
偷懒直接把bzoj的网页内容ctrlcv过来了 2806: [Ctsc2012]Cheat Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 1943 ...
- Windows server 2012 添加中文语言包(英文转为中文)(离线)
Windows server 2012 添加中文语言包(英文转为中文)(离线) 相关资料: 公司环境:亚马孙aws虚拟机 英文版Windows2012 中文SQL Server2012安装包,需要安装 ...
- Windows Server 2012 NIC Teaming介绍及注意事项
Windows Server 2012 NIC Teaming介绍及注意事项 转载自:http://www.it165.net/os/html/201303/4799.html Windows Ser ...
- 1.初始Windows Server 2012 R2 Hyper-V + 系统安装详细
干啥的?现在企业服务器都是分开的,比如图片服务器,数据库服务器,redis服务器等等,或多或少一个网站都会用到多个服务器,而服务器的成本很高,要是动不动采购几十台,公司绝对吃不消的,于是虚拟化技术出来 ...
- 0.Win8.1,Win10,Windows Server 2012 安装 Net Framework 3.5
后期会在博客首发更新:http://dnt.dkill.net 网站部署之~Windows Server | 本地部署:http://www.cnblogs.com/dunitian/p/482280 ...
- windows 2012 r2 can't find kb2919355
问题 解决: 1.手动安装了 Windows8.1-KB2919442-x64 2.手动下载 KB2919355 更新成功 Turns out to have been a result ...
随机推荐
- IP地址 网段的划分
IP和子网掩码我们都知道,IP是由四段数字组成,在此,我们先来了解一下3类常用的IP A类IP段 0.0.0.0 到127.255.255.255 B类IP段 128.0.0.0 到191.255. ...
- Dell 刀片服务器CentOS6.5mini开机20~30分钟宕机
今天查看系统日志发现大量的nf_conntrack: table full, dropping packet. 错误 cat /var/log/messages | moreJun 7 09:52: ...
- Python学习笔记18:标准库之多进程(multiprocessing包)
我们能够使用subprocess包来创建子进程.但这个包有两个非常大的局限性: 1) 我们总是让subprocess执行外部的程序,而不是执行一个Python脚本内部编写的函数. 2) 进程间仅仅通过 ...
- java.lang.NoSuchMethodError: org.jboss.logging.Logger.getMessageLogger(Ljava/lang/Class;Ljava/lang/String;)Ljava/lang/Object;
spring3_hibernate 集成报错信息 java.lang.NoSuchMethodError: org.jboss.logging.Logger.getMessageLogger(Ljav ...
- EasyRTMP Android安卓手机直播推流摄像头偏暗的问题解决
在我们测试EasyRTMP Android安卓手机推流的过程中发现有些设备预览时,明显偏暗!在稍微暗点的环境中几乎很难看清东西-额,这是怎么回事呢?又是安卓设备的兼容性问题,头疼! !!!好吧,停止抱 ...
- 时间写入文件名 nohup 原理 Command In Background your shell may have its own version of nohup
echo 123 > `date +%Y-%m-%d-%H.tmp` echo 123 > /home/`date +%Y-%m-%d-%H.tmp` nohup --help [root ...
- 24、Cocos2dx 3.0游戏开发找小三之网格动作:高炫酷的3D动作
重开发人员的劳动成果,转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/37596763 网格动作类似于动作特效,能够实现翻转. ...
- linux c编程:进程间通信
进程间的通信包括管道,共享内存,信号量通信,消息队列,套借口(socket)和全双工管道通信 首先来看下管道的用法:管道顾名思义,就如同下水道管道一样,当从管道一端流水到另一端的时候,水流的方向是单方 ...
- (扫盲)jQuery extend()和jQuery.fn.extend()的区别
1.认识jQuery extend()和jQuery.fn.extend() jQuery的API手册中,extend方法挂载在jQuery和jQuery.fn两个不同对象上方法,但在jQuery内部 ...
- Too many open files解决方案及原理
以下是我解决Too many open files异常时学习的知识的理解和总结,如有不正确指出,敬请指出! 此问题中文搜索雷同,你可以尝试以下关键字:"file descriptor lea ...