CF271D_Good Substrings
给一个原串,以及那些字符是坏的,现在问你可以从原串中取出多少个不同子串,使得其所含的坏字符的个数不超过一个定数。
这个题目网上有各种各样的解法。如hash,tire。
我说一下我的解法。
解法一:后缀自动机dp。f[][]保存到达某个状态,前面已经有的坏字符的个数的时候的字符串数量。这样按照拓扑序列一直递推下去就可以了。时间复杂度为O(N2)。
解法二:后缀自动机,预处理。对于字符串,每个位置保存它可以最前走到那个位置。然后,在自动机上增加信息,保存位置。然后直接按照距离判断就可以了。对于每个节点,直接缩小范围,更新答案。时间复杂度为O(N)。
召唤代码君:
#include <iostream>
#include <cstdio>
#include <cstring>
#define maxn 3555
using namespace std; char real[26],s[maxn];
int next[maxn][26],pre[maxn],step[maxn];
int N,last,n,L;
int p,q,np,nq,ans=0;
int f[maxn],tag[maxn],sum[maxn]; void insert(int x,int ggg)
{
p=last,np=++N,step[np]=step[p]+1,last=np,tag[np]=ggg;
for (; p!=-1 && next[p][x]==0; p=pre[p]) next[p][x]=np;
if (p==-1) return;
q=next[p][x];
if (step[q]==step[p]+1) { pre[np]=q; return ; }
nq=++N,step[nq]=step[p]+1,pre[nq]=pre[q],tag[nq]=ggg;
for (int i=0; i<26; i++) next[nq][i]=next[q][i];
pre[np]=pre[q]=nq;
for (; p!=-1 && next[p][x]==q; p=pre[p]) next[p][x]=nq;
} int main()
{
scanf("%s",s+1);
scanf("%s",real);
scanf("%d",&n);
pre[0]=-1;
L=strlen(s+1);
for (int i=1; s[i]; i++) insert(s[i]-'a',i);
sum[0]=0;
for (int i=1; i<=L; i++)
{
sum[i]=sum[i-1];
if (real[s[i]-'a']=='0') sum[i]++;
} f[L+1]=L+1;
for (int i=L; i>=1; i--)
{
int k=min(f[i+1],i+1);
for ( ;k>1 && sum[i]-sum[k-2]<=n; k--) ;
f[i]=k;
} for (int i=1; i<=N; i++)
{
int l=tag[i]-step[i]+1,r=tag[i]-step[pre[i]];
if (f[tag[i]]>l) l=f[tag[i]];
if (l<=r) ans+=r-l+1;
}
printf("%d\n",ans);
return 0;
}
CF271D_Good Substrings的更多相关文章
- [LeetCode] Unique Substrings in Wraparound String 封装字符串中的独特子字符串
Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...
- Leetcode: Unique Substrings in Wraparound String
Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...
- CSU-1632 Repeated Substrings (后缀数组)
Description String analysis often arises in applications from biology and chemistry, such as the stu ...
- CF451D Count Good Substrings (DP)
Codeforces Round #258 (Div. 2) Count Good Substrings D. Count Good Substrings time limit per test 2 ...
- LA4671 K-neighbor substrings(FFT + 字符串Hash)
题目 Source http://acm.hust.edu.cn/vjudge/problem/19225 Description The Hamming distance between two s ...
- 后缀数组---New Distinct Substrings
Description Given a string, we need to find the total number of its distinct substrings. Input T- nu ...
- Codeforces Round #258 D Count Good Substrings --计数
题意:由a和b构成的字符串,如果压缩后变成回文串就是Good字符串.问一个字符串有几个长度为偶数和奇数的Good字串. 分析:可知,因为只有a,b两个字母,所以压缩后肯定为..ababab..这种形式 ...
- SPOJ 694. Distinct Substrings (后缀数组不相同的子串的个数)转
694. Distinct Substrings Problem code: DISUBSTR Given a string, we need to find the total number o ...
- Codeforces Round #306 (Div. 2) A. Two Substrings 水题
A. Two Substrings Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/550/pro ...
随机推荐
- SpringBoot日记——Web开发篇
准备开始实战啦!~~~~ 我们先来看,SpringBoot的web是如何做web开发的呢?通常的步骤如下: 1.创建springboot应用,指定模块: 2.配置部分参数配置: 3.编写业务代码: 为 ...
- 设置JFrame背景图片
这里我就放上改写的代码吧,不做多的解释,推荐一个好的博文 https://blog.csdn.net/jdsjlzx/article/details/16831815 public void ini_ ...
- 阿里路由框架ARouter的使用步骤
ARouter的使用步骤(以宿主APP modulebase和moduleuser 三大模块组成的工程为例) 第一步 因为路由跳转是子模块都需要用到的,所以我们在module_base模块中引入 co ...
- LintCode——颜色分类
颜色分类:给定一个包含红,白,蓝且长度为 n 的数组,将数组元素进行分类使相同颜色的元素相邻,并按照红.白.蓝的顺序进行排序. 我们可以使用整数 0,1 和 2 分别代表红,白,蓝. 注意事项: 不能 ...
- Azure-如何排查应用程序网关返回 HTTP Code 502 或客户端得到应用程序网关响应慢的问题(二)
问题描述 经过如何排查应用程序网关返回 HTTP Code 502 或客户端得到应用程序网关响应慢的问题(一)中的排查步骤,可以判断出是由于 Web 服务器自身问题导致的响应异常. 那么可以在 IIS ...
- 持续更新 | 想不到的key
前言 开坑写一些我认为比较巧妙的东西想不到的东西 正文 判断回文串的时候 考虑一下枚举中心位往两边扩展 最大子矩阵与单调栈 遇难则反系列 算合法可以转换成算不合法.同理,不合法转换成合法计算有时也会简 ...
- Homebrew1.5之后安装PHP和扩展
Homebrew 1.5 宣布放弃 homebrew/php, 转而使用homebrew/core维护, 详见https://brew.sh/2018/01/19/homebrew-1.5.0/ 于是 ...
- Node.js开发入门—套接字(socket)编程
Node.js的net模块提供了socket编程接口,方便我们利用较为底层的套接字接口来实现应用协议.这次我们看一个简单的回显服务器示例,包括服务端和客户端的代码. 代码 分服务器和客户端两部分来说吧 ...
- css3 transform属性多值的顺序问题
对于transform属性的多值的顺序问题,我自己就被困扰过.后来知道了跟顺序有关,但是不知道为什么.我想应该很多人跟我以前一样,知其然不知其所以然.如果不知道的,也许这篇文章会对大家有所帮助. 先来 ...
- 必应词典手机版(IOS版)与有道词典(IOS版)之问卷分析
我们制定了一个调查问卷: 1.年龄分布: 2.地域分布: 3.是否用过必应词典? 对于必应词典还是没用过的人数更多. 4.是否用过有道词典? 有道词典的使用率更高一点. 5.对于必应的基本功能给几分? ...