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 ...
随机推荐
- 【RAC搭建报错】You need disks from at least two different failure groups, excluding quorum disks and quorum failure groups, to create a Disk Group with normal redundancy
报错: You need disks from at least two different failure groups, excluding quorum disks and quorum fai ...
- 第四篇 HTTP请求返回状态码收集及解释
[转载]https://blog.csdn.net/wangsen2235068/article/details/8081274 当用户试图通过 HTTP 访问一台正在运行 Internet 信息服务 ...
- SSH结合EasyUI系统(一)———简单介绍
鉴于前文<不仅仅是吐槽>,决定将自己学过的和在学的东西整理一下放进园子:做一个好园友! 接下来将会持续更新的是近一段时间在学的java web中比较流行的框架SSH(Struts+Spri ...
- lua栈
既然Lua虚拟机模拟的是CPU的运作,那么Lua栈模拟的就是内存的角色.在Lua内部,参数的传递是通过Lua栈,同时Lua与C等外部进行交互的时候也是使用的栈.,先关注的是Lua栈的分配,管理和相关的 ...
- Datawhale MySQL 训练营 Task4 表联结
学习内容 MySQL别名 列别名,将查询或者筛选出来列用AS 命名,如果有空格则需要引号 '' SELECT xxx AS xxxx FROM WHERE GROUP BY HAVING 表别名, 把 ...
- 前端开发利器 livereload -- 从此告别浏览器F5键
各位从事前端开发的童鞋们,大家每天coding && coding,然后F5 && F5,今天推荐一个静态文件在浏览器中自动更新的扩展 livereload,不同手动刷 ...
- Ubuntu16.04安装vmware workstation14
1.获得vmware安装包:https://www.vmware.com/products/workstation-pro/workstation-pro-evaluation.html?ClickI ...
- XSS-DVWA
1.反射型 LOW: 没有过滤,直接键入PAYLOAD 查看源码 这里没有任何过滤,使用htmlspecialchars()过滤 结果不弹窗 MEDIUM: LOW等级的方法不奏效了 观察输出可能是过 ...
- stat命令详解
基础命令学习目录首页 原文链接:https://blog.csdn.net/yexiangcsdn/article/details/81012732 stat命令用于显示文件的状态信息.stat命令的 ...
- 第十章I/O
系统级i/o 开始进程时的三个标准: 标准输入(描述符0):STDIN_FILENO 标准输出(描述符1):STDOUT_FILENO 标准错误(描述符2):STDERR_FILENO 文件位置: 从 ...