CF873F Forbidden Indices 后缀自动机+水题
刷刷水~
Code:
#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 200005
#define setIO(s) freopen(s".in","r",stdin)
using namespace std;
char str1[N],str2[N];
int tot,last,n,c[N<<1],rk[N<<1];
struct Node
{
int ch[26],f,len,cnt;
}t[N<<1];
void extend(int c,int flag)
{
int np=++tot,p=last;
last=np,t[np].len=t[p].len+1;
for(;p&&!t[p].ch[c];p=t[p].f) t[p].ch[c]=np;
if(!p) t[np].f=1;
else
{
int q=t[p].ch[c];
if(t[q].len==t[p].len+1) t[np].f=q;
else
{
int nq=++tot;
t[nq].len=t[p].len+1;
t[nq].f=t[q].f,t[q].f=t[np].f=nq;
memcpy(t[nq].ch,t[q].ch,sizeof(t[q].ch));
for(;p&&t[p].ch[c]==q;p=t[p].f) t[p].ch[c]=nq;
}
}
t[np].cnt=flag;
}
int main()
{
int i,j;
long long answer=0;
// setIO("input");
scanf("%d%s%s",&n,str1+1,str2+1);
for(last=tot=i=1;i<=n;++i) extend(str1[i]-'a',str2[i]=='0'?1:0);
for(i=1;i<=tot;++i) ++c[t[i].len];
for(i=1;i<=tot;++i) c[i]+=c[i-1];
for(i=1;i<=tot;++i) rk[c[t[i].len]--]=i;
for(i=tot;i>=1;--i)
{
int p,ff;
p=rk[i],ff=t[p].f;
answer=max(answer,(long long)t[p].len*t[p].cnt);
t[ff].cnt+=t[p].cnt;
}
printf("%lld\n",answer);
return 0;
}
CF873F Forbidden Indices 后缀自动机+水题的更多相关文章
- 【CF873F】Forbidden Indices 后缀自动机
[CF873F]Forbidden Indices 题意:给你一个串s,其中一些位置是危险的.定义一个子串的出现次数为:它的所有出现位置中,不是危险位置的个数.求s的所有子串中,长度*出现次数的最大值 ...
- Match & Catch CodeForces - 427D 后缀自动机水题
题意: 给出两个字符串a,b,求一个字符串,这个字符串是a和b的子串, 且只在a,b中出现一次,要求输出这个字符串的最小长度. 题解: 将a串放入后缀自动机中,然后记录一下每个节点对应的子串出现的次数 ...
- 牛客网 桂林电子科技大学第三届ACM程序设计竞赛 A.串串-后缀自动机模板题
链接:https://ac.nowcoder.com/acm/contest/558/A来源:牛客网 A.串串 小猫在研究字符串. 小猫在研究字串. 给定一个长度为N的字符串S,问所有它的子串Sl…r ...
- BZOJ 5084: hashit 后缀自动机(原理题)
比较考验对后缀自动机构建过程的理解. 之前看题解写的都是树链的并,但是想了想好像可以直接撤销,复杂度是线性的. 自己想出来的,感觉后缀自动机的题应该不太能难倒我~ 注意:一定要手画一下后缀自动机的构建 ...
- 【BZOJ5084】hashit(后缀自动机水过)
点此看题面 大致题意: 每次在字符串后面加入或删除一个字符,求本质不同的子串个数. 后缀自动机 先说明,此题后缀自动机的确能过. 但我的后缀自动机比较弱,遇上一个较强的\(Hack\)数据就被卡掉了. ...
- SPOJ 1811 Longest Common Substring (后缀自动机第一题,求两个串的最长公共子串)
题目大意: 给出两个长度小于等于25W的字符串,求它们的最长公共子串. 题目链接:http://www.spoj.com/problems/LCS/ 算法讨论: 二分+哈希, 后缀数组, 后缀自动机. ...
- spoj - Longest Common Substring(后缀自动机模板题)
Longest Common Substring 题意 求两个串的最长公共子串. 分析 第一个串建后缀自动机,第二个串在自动机上跑,对于自动机上的结点(状态)而言,它所代表的最大长度为根结点到当前结点 ...
- POJ - 2774 Long Long Message (后缀数组/后缀自动机模板题)
后缀数组: #include<cstdio> #include<algorithm> #include<cstring> #include<vector> ...
- poj2774(后缀数组水题)
http://poj.org/problem?id=2774 题意:给你两串字符,要你找出在这两串字符中都出现过的最长子串......... 思路:先用个分隔符将两个字符串连接起来,再用后缀数组求出h ...
随机推荐
- rtsp学习----海康RTSP客户端连接深入分析
转载于:http://blog.csdn.net/zhouyongku/article/details/41546789 海康相机RTSP连接代码分析 最近在做海康相机rtsp连接获取音视频的工作,现 ...
- # log对数Hash映射优化
log对数Hash映射优化 利用了一个数学技巧:$\forall k \in [0,35],2^{k} mod 37 互不相等,且恰好取遍整数1-36 $ 应用:将int范围内的\(2^k映射到k\) ...
- C++实用模板 | 党办大活动计分
#include <iostream> #include <cmath> #include <cstring> #include <string> #i ...
- Linux(16):Shell编程(3)
vim 编程环境配置: .vimrc 文件配置如下:(.vimrc文件放到家目录下:/root :然后退出 xshell 再登陆进来xshell) set nocompatible set histo ...
- 简化SpringMVC配置
映射器处理器和适配器是可以省略的 为什么可以省略?因为有默认配置 SpringMVC的默认配置
- JS基础_强制类型转换-Boolean
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- k8s自签TLS证书
自签TLS证书 TLS证书用于进行通信使用,k8s组件需要的证书有: 第一步:安装证书生成工具cfssl 在这之前需要先建立一个目录来存放安装的工具mkdir ssl,后面将安装的工具移动到各自的目录 ...
- isAssignable
import java.util.HashMap; import java.util.Map; public class MapTest { public static void main(Strin ...
- springboot-异步线程调用
启动类:添加@EnableAsync注解 @SpringBootApplication @EnableAsync public class Application{ public static voi ...
- 点击切换的JS
$(function(){ var tabnav = $("#tab-nav ul li"); tabnav.click(function(){ $(this).addClass( ...