pro:

维护一个01字符串,支持在结尾动态加字符。

每一个长度<=4的01串可以对应一个字母(有几个特例除外)

每次操作后询问,这个字符串的所有子串一共可以对应出多少种本质不同的字符串。

sol:

考虑dp。

dp[i][j]表示从i这个字符开始到当前倒数第j位这个01串,可以划分为多少不同的字符串。

每次只需要更新一下从1到i的每一个dp数组即可。

但时考虑本质不同这个问题。

显然每一种字母串对应唯一的01串。

因此我们只需要对相同的01子串只统计一次即可。

这里用SAM进行判重即可。

#include<bits/stdc++.h>
#define N 22000
#define L 20000
#define eps 1e-7
#define inf 1e9+7
#define db double
#define ll long long
#define ldb long double
using namespace std;
inline int read()
{
char ch=0;
int x=0,flag=1;
while(!isdigit(ch)){ch=getchar();if(ch=='-')flag=-1;}
while(isdigit(ch)){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
return x*flag;
}
const ll mo=1e9+7;
int f[N],id[N],dp[N][8];
int root=1,size=1,last=1;
struct node{int pos,len,lnk,nxt[2];}s[N];
void build(int k)
{
int cur=++size,p=last;last=cur;
s[cur].len=s[p].len+1;s[cur].pos=s[cur].len;
while(p&&!s[p].nxt[k])s[p].nxt[k]=cur,p=s[p].lnk;
if(!p){s[cur].lnk=root;return;}
int q=s[p].nxt[k];
if(s[q].len==s[p].len+1){s[cur].lnk=q;return;}
else
{
int clone=++size;
s[clone]=s[q];s[clone].len=s[p].len+1;
while(p&&s[p].nxt[k]==q)s[p].nxt[k]=clone,p=s[p].lnk;
s[q].lnk=s[cur].lnk=clone;
}
}
int main()
{
srand(time(0));
int n=read(),ans=0;
for(int i=1;i<=n;i++)f[i]=read(),build(f[i]);
for(register int o=1;o<=n;o++)
{
id[o]=root;
for(register int i=1;i<o;i++)
{
for(register int j=4;j>=1;j--)dp[i][j]=dp[i][j-1];dp[i][0]=0;
for(register int j=1;j<=4;j++)if(o-i+1>=j)
{
if(j==4)
{
if(f[o-3]==0&&f[o-2]==0&&f[o-1]==1&&f[o]==1)continue;
if(f[o-3]==0&&f[o-2]==1&&f[o-1]==0&&f[o]==1)continue;
if(f[o-3]==1&&f[o-2]==1&&f[o-1]==1&&f[o]==0)continue;
if(f[o-3]==1&&f[o-2]==1&&f[o-1]==1&&f[o]==1)continue;
}
dp[i][0]=(dp[i][0]+dp[i][j])%mo;
}
int x=id[i];
if(x!=-1){if(s[x].nxt[f[o]])x=s[x].nxt[f[o]];else x=-1;}
if(x==-1||o==s[x].pos)ans=(ans+dp[i][0])%mo;id[i]=x;
}
dp[o][0]=dp[o][1]=1;
int x=id[o];
if(x!=-1){if(s[x].nxt[f[o]])x=s[x].nxt[f[o]];else x=-1;}
if(x==-1||o==s[x].pos)ans=(ans+dp[o][0])%mo;id[o]=x;
printf("%d\n",ans);
}
return 0;
}

CF1129C Morse Code的更多相关文章

  1. morse code

    morse code,摩斯电码,是一种时通时断的信号代码,通过不同的排列顺序来表达不同的英文字母.数字和标点符号. 摩斯电码,是一种早期的数字化通信形式,但是它不同于现代只使用0和1两种状态的二进制代 ...

  2. 摩尔斯电码(Morse Code)Csharp实现

    摩尔斯电码,在早期的"生产斗争"生活中,扮演了很重要的角色,作为一种信息编码标准,摩尔斯电码拥有其他编码方案无法超越的长久生命.摩尔斯电码在海事通讯中被作为国际标准一直使用到199 ...

  3. Leetcode 804. Unique Morse Code Words 莫尔斯电码重复问题

    参考:https://blog.csdn.net/yuweiming70/article/details/79684433 题目描述: International Morse Code defines ...

  4. [Swift]LeetCode804. 唯一摩尔斯密码词 | Unique Morse Code Words

    International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...

  5. [LeetCode] Unique Morse Code Words 独特的摩斯码单词

    International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...

  6. (string 数组) leetcode 804. Unique Morse Code Words

    International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...

  7. 804. Unique Morse Code Words

    Description International Morse Code defines a standard encoding where each letter is mapped to a se ...

  8. Unique Morse Code Words

    Algorithm [leetcode]Unique Morse Code Words https://leetcode.com/problems/unique-morse-code-words/ 1 ...

  9. LeetCode - 804. Unique Morse Code Words

    International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...

随机推荐

  1. noip模拟【service】

    service [问题描述] 一家公司为它在各地的用户提供服务,公司有三名负责这项工作的员工,分别编号为1,2,3,服务的地点有n个,分别编号为1,2,3,...n,把从编号为p的服务地点直接到达编号 ...

  2. 解决Win10 Git图标不显示问题

    升级系统到win10 1803版本以后发现TortoiseGit的忽略图标不显示了 开始以为是版本问题,将TortoiseGit版本进行了升级还是不行 网上查找以后发现 Windows Explore ...

  3. 【Dalston】【第四章】容错保护(Hystrix)

    我们在实践微服务架构时,通常会将业务拆分成一个个微服务,微服务之间通过网络进行通信,进行互相调用,造成了微服务之间存在依赖关系.我们知道由于网络原因或者自身的原因,服务并不能保证服务的100%可用,如 ...

  4. P4246 [SHOI2008]堵塞的交通

    思路 同LOJ121 动态图连通性的板子 好像有很神的线段树做法,不会,先码住 代码 #include <cstdio> #include <algorithm> #inclu ...

  5. (转)Applications of Reinforcement Learning in Real World

    Applications of Reinforcement Learning in Real World 2018-08-05 18:58:04 This blog is copied from: h ...

  6. (转)Understanding, generalisation, and transfer learning in deep neural networks

    Understanding, generalisation, and transfer learning in deep neural networks FEBRUARY 27, 2017   Thi ...

  7. Keepalived笔记

    Keepalived高可用服务对之间的故障切换转移,是通过 VRRP (Virtual Router Redundancy Protocol ,虚拟路由器冗余协议)来实现的. VRRP的出现是为了解决 ...

  8. kubernetes 实战4_命令_Configure Pods and Containers

    Configure Service Accounts for Pods A service account provides an identity for processes that run in ...

  9. transient关键字详解

    作用 1,一旦变量被transient修饰,变量将不再是对象持久化的一部分,该变量内容在序列化后无法获得访问. 2,transient关键字只能修饰变量,而不能修饰方法和类.注意,本地变量是不能被tr ...

  10. 使用pymongo连接mongodb时报错:pymongo.errors.OperationFailure: not authorized

    连接本机或局域网部署的mongodb时可以用以下方法: from urllib import parse from pymongo import MongoClient host = '*.*.*.* ...