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 ...
随机推荐
- Linux 和 Windows 之间共享文件之 samba
导语 如果对windows有过实际操作技巧的人都会明白,在windows下的文件共享加上网络驱动器映射是多么方便的体验,甚至比ftp更加的简单,就像本地多了一块可与他人交流的硬盘一样. 问题 由于性能 ...
- Centos6.8配置JDK
下载jdk-版本号.tar.gz 解压到/usr/local/ vi /etc/profile 最后一行添加如下内容: export JAVA_HOME=/usr/local/jdk-版本号expor ...
- python爬虫beautifulsoup4系列2
前言 本篇详细介绍beautifulsoup4的功能,从最基础的开始讲起,让小伙伴们都能入门 一.读取HTML页面 1.先写一个简单的html页面,把以下内容copy出来,保存为html格式文件 &l ...
- int str input的运用
- PHP的学习路线规划
第一阶段:WEB的快速入门 前期入门学习我们需要学一些HTML+CSS+JS前端的一些技术,这个阶段不需要太深入的学习,学习到可以制作出一个像样点的静态页面就可以了.因为大家是学习PHP,对于新人来说 ...
- 学习笔记 | Set
目录 Set Set 前言 不会数据结构选手 当几乎没写过什么数据结构的菜鸡遇上了毒瘤的splay和treap 时间正一点一点地被续走TAT 听说set有时候可以替代treap和splay 那么菜鸡L ...
- access和MySQL mssql
Access.MSSQL.MYSQL数据库之间有什么区别? Access数据库.MSSQL数据库.MYSQL数据库之间有什么区别? 不少企业和个人站长在网站制作时,会对数据库的概 ...
- Vim YouCompleteMe 安装配置
YouCompleteMe是很强大的vim插件,可以提供强大的补齐功能,曾经多次尝试安装,都没有配置成功,最近在一个契机下,看到有同事的配置,自己在边尝试和边咨询后,终于也搞定了,遂记录下. 官网有最 ...
- mac 上面安装 tree 命令
相信很多使用过Linux的用户都用过tree命令,它可以像windows的文件管理器一样清楚明了的显示目录结构. 但是mac下默认是没有 tree命令的. 1.我们可以使用find命令模拟出tree命 ...
- 作业MathExam
MathExam233 一.预估与实际 PSP2.1 Personal Software Process Stages 预估耗时(分钟) 实际耗时(分钟) Planning 计划 600 650 • ...