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 ...
随机推荐
- [VB.NET][C#]调用API获取或设置键盘按键状态
前言 通过 C# 或 VB.NET,你只需编写少量的代码即可实现一个按键精灵. 第一节 接口 调用系统 API 实现获取或设置指定的按键状态. 获取按键状态 调用 GetAsyncKeyState() ...
- js简单时间比较的方法(转)
//时间比较(yyyy-MM-dd) function compareDate(startDate, endDate) { var arrStart = startDate.split(" ...
- STM8S——watchdog(IWDG)
IWDG工作原理: 1.当键值寄存器(IWDG_KR)中写入数值0xCC后,独立看门狗就会被启动,计数器开始从它的复位值0xFF开始递减计数,当计数减到0x00时就会产生一个复位信号. 2.使用IWD ...
- python自编程序实现——robert算子、sobel算子、Laplace算子进行图像边缘提取
实现思路: 1,将传进来的图片矩阵用算子进行卷积求和(卷积和取绝对值) 2,用新的矩阵(与原图一样大小)去接收每次的卷积和的值 3,卷积图片所有的像素点后,把新的矩阵数据类型转化为uint8 注意: ...
- Shader食谱 Chapter3--Toonshader卡通效果
Shader食谱 Chapter3--Toonshader卡通效果 unity shader toon 卡通Shader Shader食谱 Chapter3--Toonshader卡通效果 Over ...
- Python输出格式全总结
输入输出 有几种方法可以显示程序的输出:数据可以以人类可读的形式打印出来,或者写入文件以供将来使用.本章将讨论一些可能性. 更漂亮的输出格式 到目前为止,我们遇到了两种写入值的方法:表达式语句 和 p ...
- NO---20 文件上传
文件上传是我们会经常用到的一个业务,其实在h5中新增了FormData的对象,用它来提交表单,并且可以提交二进制文件,所以今天就写写文件上传,希望可以对大家有帮助 FormData 上传文件实例 首先 ...
- AssociatedObject关联对象原理实现
介绍 关联对象(AssociatedObject)是Objective-C 2.0运行时的一个特性,允许开发者对已经存在的类在扩展中添加自定义的属性.在实际生产过程中,比较常用的方式是给分类(Cate ...
- Keycloak服务器安装和配置
安装地址:https://www.keycloak.org/archive/downloads-4.4.0.html 参考文档:https://www.keycloak.org/docs/latest ...
- sqli-labs学习笔记 DAY6
DAY 6 sqli-labs lesson 30 与上一题一样,需要用到HPP 查看源代码,参数两边加上了双引号,直接使用lesson 26a与lesson 27a的脚本即可 sqli-labs l ...