pro:

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

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

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

sol:

考虑dp。

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

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

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

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

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

这里用SAM进行判重即可。

  1. #include<bits/stdc++.h>
  2. #define N 22000
  3. #define L 20000
  4. #define eps 1e-7
  5. #define inf 1e9+7
  6. #define db double
  7. #define ll long long
  8. #define ldb long double
  9. using namespace std;
  10. inline int read()
  11. {
  12. char ch=0;
  13. int x=0,flag=1;
  14. while(!isdigit(ch)){ch=getchar();if(ch=='-')flag=-1;}
  15. while(isdigit(ch)){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
  16. return x*flag;
  17. }
  18. const ll mo=1e9+7;
  19. int f[N],id[N],dp[N][8];
  20. int root=1,size=1,last=1;
  21. struct node{int pos,len,lnk,nxt[2];}s[N];
  22. void build(int k)
  23. {
  24. int cur=++size,p=last;last=cur;
  25. s[cur].len=s[p].len+1;s[cur].pos=s[cur].len;
  26. while(p&&!s[p].nxt[k])s[p].nxt[k]=cur,p=s[p].lnk;
  27. if(!p){s[cur].lnk=root;return;}
  28. int q=s[p].nxt[k];
  29. if(s[q].len==s[p].len+1){s[cur].lnk=q;return;}
  30. else
  31. {
  32. int clone=++size;
  33. s[clone]=s[q];s[clone].len=s[p].len+1;
  34. while(p&&s[p].nxt[k]==q)s[p].nxt[k]=clone,p=s[p].lnk;
  35. s[q].lnk=s[cur].lnk=clone;
  36. }
  37. }
  38. int main()
  39. {
  40. srand(time(0));
  41. int n=read(),ans=0;
  42. for(int i=1;i<=n;i++)f[i]=read(),build(f[i]);
  43. for(register int o=1;o<=n;o++)
  44. {
  45. id[o]=root;
  46. for(register int i=1;i<o;i++)
  47. {
  48. for(register int j=4;j>=1;j--)dp[i][j]=dp[i][j-1];dp[i][0]=0;
  49. for(register int j=1;j<=4;j++)if(o-i+1>=j)
  50. {
  51. if(j==4)
  52. {
  53. if(f[o-3]==0&&f[o-2]==0&&f[o-1]==1&&f[o]==1)continue;
  54. if(f[o-3]==0&&f[o-2]==1&&f[o-1]==0&&f[o]==1)continue;
  55. if(f[o-3]==1&&f[o-2]==1&&f[o-1]==1&&f[o]==0)continue;
  56. if(f[o-3]==1&&f[o-2]==1&&f[o-1]==1&&f[o]==1)continue;
  57. }
  58. dp[i][0]=(dp[i][0]+dp[i][j])%mo;
  59. }
  60. int x=id[i];
  61. if(x!=-1){if(s[x].nxt[f[o]])x=s[x].nxt[f[o]];else x=-1;}
  62. if(x==-1||o==s[x].pos)ans=(ans+dp[i][0])%mo;id[i]=x;
  63. }
  64. dp[o][0]=dp[o][1]=1;
  65. int x=id[o];
  66. if(x!=-1){if(s[x].nxt[f[o]])x=s[x].nxt[f[o]];else x=-1;}
  67. if(x==-1||o==s[x].pos)ans=(ans+dp[o][0])%mo;id[o]=x;
  68. printf("%d\n",ans);
  69. }
  70. return 0;
  71. }

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. P2519 [HAOI2011]problem a

    思路 神仙思路,就差一步就能想出来了... 看到第i个人给出的条件,发现有\(a_i\)个大于,\(b_i\)个小于并不好处理 考虑把条件转化成第i个人对应的排名处理,设第i个人的排名为\(a_i+1 ...

  2. PredNet --- Deep Predictive coding networks for video prediction and unsupervised learning --- 论文笔记

    PredNet --- Deep Predictive coding networks for video prediction and unsupervised learning   ICLR 20 ...

  3. 解决Maven管理项目update Maven时,jre自动变为1.5

    本文为博主原创,未经允许不得转载: 在搭建一个maven web项目时,项目已经按步骤搭建完好,之后项目上就报了一个错误. 在控制台看到错误提示如下:Dynamic Web Module 3.0 re ...

  4. JavaWeb 基础学习

    XMAPP是自己封装的一套 web 开发套件 —— 例如Tomcat等是用自己的,而不是使用系统中其他地方安装好了的.此外将提供的 xampp 工具解压到 D 盘根目录下.(注意 xampp 一定要解 ...

  5. C++ 空字符('\0')和空格符(' ')

    1.从字符串的长度:-->空字符的长度为0,空格符的长度为1. 2.虽然输出到屏幕是一样的,但是本质的ascii code 是不一样的,他们还是有区别的. #include<iostrea ...

  6. markdown一些网站

    1.https://stackedit.io/editor 2.https://github.com/bioinformatist/LncPipeReporter 3.

  7. Webpack+React项目入门——入门及配置Webpack

    一.入门Webpack 参考文章:<入门Webpack,看这篇就够了> 耐心看完这篇非常有帮助 二.React+Webpack环境配置 参考文章:<webpack+react项目初体 ...

  8. SQLServer2008 远程过程调用失败

    今天在连接数据库的时候,发现无法获取到服务器名称,打开SQLServer Configuration Manager,发现SQLServer服务中远程过程调用失败 我装的是VS2017,在网上百度了一 ...

  9. tarjan 缩点(模板)

    描述: 给定一个n个点m条边有向图,每个点有一个权值,求一条路径,使路径经过的点权值之和最大.你只需要求出这个权值和. 注:允许多次经过一条边或者一个点,但是,重复经过的点,权值只计算一次. 思路: ...

  10. js 捕获浏览器后退事件

    $(document).ready(function(e) {             var counter = 0;             if (window.history &&am ...