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 ...
随机推荐
- OAuth2.0原理与实现
弄懂了原理流程,才可以搭建出来.更重要的是,可以根据原理流程自定义搭建,甚至可以完全自己实现一套,最后运行效果和原理和这个对得上就成功了,不要总期待标准答案! 首先参考两篇博客: 阮一峰的博客以及张开 ...
- LuoguP3792 由乃与大母神原型和偶像崇拜
题目地址 题目链接 题解 由乃题还是毒瘤啊orz 显然的一个结论是,如果保证不重复,维护区间min,max然后判断max-min+1==r-l+1是否成立即可 但是有重复 于是就要orz题解区的各位大 ...
- JZ2440之GPIO篇
买来开发板已经有一段时间了,刚接触时兴奋至极,后来跟着视频看下去发现似乎自己并没有学到太多东西,于是发现自己可能欠缺的太多以致从课程中无法提取出重要的东西来,所以并没有得到太多的营养成分.因此我个人认 ...
- HDU 1512 Monkey King(左偏树模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=1512 题意: 有n只猴子,每只猴子一开始有个力量值,并且互相不认识,现有每次有两只猴子要决斗,如果认识,就不打了 ...
- HDU 3400 Line belt (三分套三分)
http://acm.split.hdu.edu.cn/showproblem.php?pid=3400 题意: 有两条带子ab和cd,在ab上的速度为p,在cd上的速度为q,在其它地方的速度为r.现 ...
- 生成器的使用demo
定义一个函数: def frange(start, stop, increment): x = start while x < stop: yield x x += increment 使用: ...
- 正则获取 某段 DIV 中 的内容
string html = "<div class='aa'><div class='left'>324324<div>dsfsdf</div> ...
- VS2010_DLL_共享数据段
1.问题:写了一个DLL,自己一直测试不成功(程序A设置了 数值之后,程序B 始终读不到 读出来的都是初始化时的数值...) 具体过程: (1).DLL当初没想要用 共享数据段,测试使用 一直都是OK ...
- MVC路由 路由的三种扩展 替换MVC内置的Handler
Global.asax 是 程序入口文件 路由配置 为什么localhost:8088/Home/Index/1 能返问到我们写的 会去掉前缀跟端口号 变成Home/Index/1 用这个跟路由 ...
- 求1000以内的质数c语言
之前在做求1000以内的质数的时候,我们一般能想到的就是从3~(根号)no,逐一和no除,如果存在某个i使得 i|no成立的话,说明no不是质数(“i|no”是i整除除no的意思): 在<明解 ...