当前位置: 首页 > news >正文

[SDOI2016]生成魔咒

嘟嘟嘟

哈,我竟然一眼秒了,从开始看题到A用了不到15分钟。

SAM每一次求不同子串个数是\(O(n)\)的,所以\(O(n ^ 2)\)自然过不了。
这时我想了想SAM每一次是怎么求不同子串个数的,就是遍历parent树,每一次加上\(len[now] - len[link[now]]\)
因此我们添加字符的时候,加上新的节点产生的贡献,如果遇到第三种情况,就把原来节点的贡献减去,再加上改过的节点的贡献就好啦~

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
#include<map>
using namespace std;
#define enter puts("") 
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 1e5 + 5;
inline ll read()
{
  ll ans = 0;
  char ch = getchar(), last = ' ';
  while(!isdigit(ch)) last = ch, ch = getchar();
  while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
  if(last == '-') ans = -ans;
  return ans;
}
inline void write(ll x)
{
  if(x < 0) x = -x, putchar('-');
  if(x >= 10) write(x / 10);
  putchar(x % 10 + '0');
}

int n;
ll ans = 0;
struct Sam
{
  int las, cnt;
  map<int, int> tra[maxn << 1];
  int len[maxn << 1], link[maxn << 1];
  In void init() {link[las = cnt = 0] = -1;}
  In void insert(int x)
  {
    int now = ++cnt, p = las;
    len[now] = len[las] + 1;
    while(~p && !tra[p].count(x)) tra[p][x] = now, p = link[p];
    if(p == -1) link[now] = 0, ans += len[now];
    else
      {
    int q = tra[p][x];
    if(len[q] == len[p] + 1) link[now] = q, ans += len[now] - len[link[now]];
    else
      {
        int clo = ++cnt;
        tra[clo] = tra[q];
        len[clo] = len[p] + 1;
        ans -= len[q] - len[link[q]];
        link[clo] = link[q], link[q] = link[now] = clo;
        ans += len[clo] - len[link[clo]];
        ans += len[q] - len[link[q]];
        ans += len[now] - len[link[now]];
        while(~p && tra[p][x] == q) tra[p][x] = clo, p = link[p];
      }
      }
    las = now;
  }
}S;


int main()
{
  n = read(); S.init();
  for(int i = 1; i <= n; ++i)
    {
      int x = read();
      S.insert(x);
      write(ans), enter;
    }
  return 0;
}

转载于:https://www.cnblogs.com/mrclr/p/10459986.html

相关文章:

  • 二、Docker基础操作
  • ugui中ContentSizeFilter使用注意点
  • 如何将监控画面嵌入微信公众号进行直播
  • 忘尘世
  • 基于高度的纹理混合shader
  • Ray Tracing in One Weekend ——Chapter 0: Overview
  • java23种设计模式之二: 单例设计模式(6种写法)
  • opengl proamming guide chapter 9——Tessellation Shaders
  • opengl proamming guide chapter 10——Geometry Shaders
  • 开学第一课
  • 第七章 更灵活的定位内存地址的方法 知识梳理
  • [转帖]nginx服务器安装及配置文件详解
  • Real-Time Rendering——Chapter 9 Global Illumination
  • Django模板语言的复用
  • ssh: connect to host gitlab.alpha.com port 22: Network is unreachable
  • 实现windows 窗体的自己画,网上摘抄的,学习了
  • 【159天】尚学堂高琪Java300集视频精华笔记(128)
  • C++类中的特殊成员函数
  • CentOS学习笔记 - 12. Nginx搭建Centos7.5远程repo
  • Consul Config 使用Git做版本控制的实现
  • Fabric架构演变之路
  • hadoop集群管理系统搭建规划说明
  • iBatis和MyBatis在使用ResultMap对应关系时的区别
  • idea + plantuml 画流程图
  • Iterator 和 for...of 循环
  • MySQL用户中的%到底包不包括localhost?
  • React-生命周期杂记
  • springboot_database项目介绍
  • 大数据与云计算学习:数据分析(二)
  • 个人博客开发系列:评论功能之GitHub账号OAuth授权
  • 规范化安全开发 KOA 手脚架
  • 基于组件的设计工作流与界面抽象
  • 聊聊directory traversal attack
  • 巧用 TypeScript (一)
  • 如何优雅地使用 Sublime Text
  • 通过npm或yarn自动生成vue组件
  • 消息队列系列二(IOT中消息队列的应用)
  • 机器人开始自主学习,是人类福祉,还是定时炸弹? ...
  • 积累各种好的链接
  • ​LeetCode解法汇总307. 区域和检索 - 数组可修改
  • # include “ “ 和 # include < >两者的区别
  • %3cscript放入php,跟bWAPP学WEB安全(PHP代码)--XSS跨站脚本攻击
  • (9)YOLO-Pose:使用对象关键点相似性损失增强多人姿态估计的增强版YOLO
  • (webRTC、RecordRTC):navigator.mediaDevices undefined
  • (附源码)springboot高校宿舍交电费系统 毕业设计031552
  • (附源码)springboot优课在线教学系统 毕业设计 081251
  • (附源码)springboot掌上博客系统 毕业设计063131
  • (六) ES6 新特性 —— 迭代器(iterator)
  • (免费领源码)python+django+mysql线上兼职平台系统83320-计算机毕业设计项目选题推荐
  • (十二)devops持续集成开发——jenkins的全局工具配置之sonar qube环境安装及配置
  • (一)插入排序
  • (一)基于IDEA的JAVA基础10
  • (转载)深入super,看Python如何解决钻石继承难题
  • ****** 二十三 ******、软设笔记【数据库】-数据操作-常用关系操作、关系运算
  • ***汇编语言 实验16 编写包含多个功能子程序的中断例程