[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 ...
随机推荐
- 企业级API设计
最近对service的API设计,在team内有些讨论,主要集中在API是足够抽象.通用好呢, 还是具体.易用好? 其实这个是要折衷的,通用的好处是以后更改API的可能性小,但坏处是想要通用,里面的字 ...
- 现在有一张半径为r的圆桌,其中心位于(x,y),现在他想把圆桌的中心移到(x1,y1)。每次移动一步,都必须在圆桌边缘固定一个点然后将圆桌绕这个点旋转。问最少需要移动几步。
// ConsoleApplication5.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<vector> ...
- 我眼中的Oracle Database Software 和 Oracle Database
我眼中的Oracle Database Software 和 Oracle Database 我喜欢用微软的office软件和word文档(确切的说是:自己写的word文档,能够把这个Word文档想象 ...
- x86 的 TSS 任务切换机制
转自:http://blog.chinaunix.net/uid-587665-id-2732907.html [0]写在前面 segment descriptors 构建保护模式下的最基本.最根本的 ...
- ASP.NET MVC 相关的社群与讨论区
ASP.NET MVC 官方论坛 http://forums.asp.net/1146.aspx 台湾微软MSDN论坛 --- ASP.NET 与 AJAX(ASP.NET AND AJAX)讨论区 ...
- 做一个合格的程序员之浅析Spring AOP源代码(十八) Spring AOP开发大作战源代码解析
事实上上一篇文章价值非常小,也有反复造轮子的嫌疑,网上AOP的实例非常多,不胜枚举,事实上我要说的并非这个,我想要说的就是上一节中spring的配置文件: 我们这边并没实用到我们上几节分析的哪几个AO ...
- Android Apk包下查看 sha1
用keytool工具查看sha1,格式如下:keytool -printcert -file Urovo.RSA文件路径(APK解压后在Meta-INF文件夹下)
- PHP操作:将数据库中的数据保存到Word、Excel中。
1.首先要把word.excel表放到文件的根目录下 2.定义了一个word类 <?php class word { function start() { ob_start(); ob_star ...
- jQuery学习笔记(9)--jquery中的事件--$(document).ready()
1.$(document).ready()方法是事件模块中最重要的一个函数,可以极大地提高web应用程序的相应速度. 2.执行时机:DOM就绪后就会执行,而javascript中window.onlo ...
- c++ get the pointer from the reference
int x = 5; int& y = x; int* xp = &x; int* yp = &y; xp is equal to yp. 也就是说,直接对reference取 ...