CF1129C Morse Code
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的更多相关文章
- morse code
morse code,摩斯电码,是一种时通时断的信号代码,通过不同的排列顺序来表达不同的英文字母.数字和标点符号. 摩斯电码,是一种早期的数字化通信形式,但是它不同于现代只使用0和1两种状态的二进制代 ...
- 摩尔斯电码(Morse Code)Csharp实现
摩尔斯电码,在早期的"生产斗争"生活中,扮演了很重要的角色,作为一种信息编码标准,摩尔斯电码拥有其他编码方案无法超越的长久生命.摩尔斯电码在海事通讯中被作为国际标准一直使用到199 ...
- Leetcode 804. Unique Morse Code Words 莫尔斯电码重复问题
参考:https://blog.csdn.net/yuweiming70/article/details/79684433 题目描述: International Morse Code defines ...
- [Swift]LeetCode804. 唯一摩尔斯密码词 | Unique Morse Code Words
International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...
- [LeetCode] Unique Morse Code Words 独特的摩斯码单词
International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...
- (string 数组) leetcode 804. Unique Morse Code Words
International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...
- 804. Unique Morse Code Words
Description International Morse Code defines a standard encoding where each letter is mapped to a se ...
- Unique Morse Code Words
Algorithm [leetcode]Unique Morse Code Words https://leetcode.com/problems/unique-morse-code-words/ 1 ...
- LeetCode - 804. Unique Morse Code Words
International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...
随机推荐
- 树形dp|无根树转有根树|2015年蓝桥杯生命之树
2015年蓝桥杯第十题--生命之树(无根树dfs) ①暴力解法:枚举子集(选点) + dfs判断连通性(题目要求连通)满足上面两个条件下找出最大值权值和 ②dfs无根树转有根树,递归找最优 先学习无根 ...
- try finally 执行顺序问题
有return的情况下try catch finally的执行顺序 在遇到Exception 并且没有catch的情况下finally语句块没有执行 System.exit(0),终止了 Java 虚 ...
- SSM项目 单元测试中 注入bean 空指针异常
##特别 由于准备春招,所以希望各位看客方便的话,能去github上面帮我Star一下项目https://github.com/Draymonders/Campus-Shop java.lang.Nu ...
- sql 指定数据库中的信息操作
查是否有该表名 SELECT * FROM sys.objects WHERE name='表名'查表字段的信息select * from syscolumns where id=Object_Id( ...
- WEB安全学习二、注入工具 sqlmap的使用
使用的是Kali Linux 系统,系统中默认的sqlmap 是安装好了的,电脑上没有安装sqlmap,自己百度 ,需要python的环境 使用 命令 sqlmap -h 可以查看 sqlm ...
- swagger实战踩坑1
1.swagger error 有详细的错误信息 自己根据详细信息找问题 2.swagger error 无详细的错误信息 Controller是否重名=>重名,指定不同的 [Route(& ...
- 如何判断一个js对象是否是Array
经常遇到一个问题,判断某个对象是否为数组类型,在Js中检测对象类型的常见方式有以下: typeof操作符 对于Function.String.Number.Undefined等几种类型的对象来说,几乎 ...
- R工具包一网打尽
这里有很多非常不错的R包和工具. 该想法来自于awesome-machine-learning. 这里是包的导航清单,看起来更方便 >>>导航清单 通过这些翻译了解这些工具包,以后干 ...
- 微信小游戏开发之JS面向对象
//游戏开发之面向对象 //在js的开发模式中有两种模式:函数式+面向对象 //1.es5 // 拓展一:函数的申明和表达式之间的区别 // 函数的申明: // function funA(){ // ...
- 解决github网站打开慢的问题
一.前言 作为一名合格的程序员,github打开速度太慢怎么能容忍.但是可以通过修改hosts文件信息来解决这个问题.现在chrome访问github速度杠杠的! 二.macOS解决方法 打开host ...