[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 ...
随机推荐
- MFC——9.多线程与线程同步
Lesson9:多线程与线程同步 程序.进程和线程是操作系统的重点,在计算机编程中.多线程技术是提高程序性能的重要手段. 本文主要解说操作系统中程序.进程和线程之间的关系,并通过相互排斥对象和事件对象 ...
- 原创 | 我被面试官给虐懵了,竟然是因为我不懂Spring中的@Configuration
GitHub 3.7k Star 的Java工程师成神之路 ,不来了解一下吗? GitHub 3.7k Star 的Java工程师成神之路 ,真的不来了解一下吗? GitHub 3.7k Star 的 ...
- ReentrentLock重入锁
ReentrentLock lock=new ReentrentLock(); lock.lock(); //锁的代码 finally{ lock.unlock(); } ReentrentLock ...
- android 在githup中的资源整理(转)
1.Github开源Android组件资源整理(一) 个性化控件(View) 2.Github开源Android组件资源整理(二)ActionBar和Menu 3. Github开源Android组件 ...
- python中装饰器你真的理解吗?
def w1(func): print('装饰器1....') def w1_in(): print('w1_in.....') func() return w1_in def w2(func): p ...
- 我的Android进阶之旅------>Android中ListView中嵌套(ListView)控件时item的点击事件不起作的问题解决方法
开发中常常需要自己定义Listview,去继承BaseAdapter,在adapter中按照需求进行编写,问题就出现了,可能会发生点击每一个item的时候没有反应,无法获取的焦点. 如果你的自定义Li ...
- 【Xcode学C-4】进制知识、位运算符、变量存储细节以及指针的知识点介绍
一.进制知识 (1)默认是十进制.八进制前面加0.即int num1=015;是13.十六进制前面加0x/0X.即int num1=0xd.结果是13.二进制前面是0b/0B,即int num1=0b ...
- COPY SAP 标准gui状态
[转]如何COPY SAP标准gui状态 1.可以自己建立 2.找到合适的ALV程序,然后找到合适的 gui_statu,进行copy. 但是这个是系统有过自定义开发会方便很多,如果没有,那要找标准程 ...
- Java for LeetCode 108 Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 解题 ...
- [2018-10-10]记录一下Vue的一个问题
最近用vue typescript SPA 做管理后台(ABP官网导出的vue项目模板),遇到一个错误,找了好久,虽然有相关资料,但发现都没解决,这里自己记录一下. Failed to mount c ...