【BZOJ4166】月宫的符卡序列

题解:题倒不难,就是有点恶心。

首先学习回文串的时候一定学到了这样一个结论:一个长度为n的串的本质不同的回文子串数量不超过n个。

那么我们就可以试图将所有回文串的价值都计算出来,这就需要我们先计算出每个回文中心i的最长回文半径rl[i],那么那些半径在[1,rl[i]]中的,且以i为回文中心的回文串的价值都应该被更新。其实只需要更新最长的那个就行,其余的可以扫一遍回文树,逐层更新上去。但是回文树太大建不出来怎么办?我们可以用hash,直接通过hash值得到每个串在回文树上的父亲。这样就可以更新了。

但是hash要用到map啊,然后本人就被光荣的卡常了。于是采取了一点黑科技~

#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn=2000010;
vector<int> v[maxn];
const ll m1=9997957;
const ll m2=1000000007;
int n,tot,len,ml,pos,mx;
char str[maxn],ss[maxn];
int rl[maxn],ans[maxn],p[maxn];
int head[10000000],next[maxn];
ll h1[maxn],b1[maxn],h2[maxn],b2[maxn],val[maxn];
int find(ll t1,ll t2)
{
for(int i=head[t1];i;i=next[i]) if(val[i]==t2) return i;
return 0;
}
void add(ll t1,ll t2)
{
val[++tot]=t2,next[tot]=head[t1],head[t1]=tot;
}
void work()
{
n=tot=mx=0;
memset(ans,0,sizeof(ans));
memset(rl,0,sizeof(rl));
memset(head,0,sizeof(head));
scanf("%s",ss),len=strlen(ss);
int i,j,k,t;
ll t1,t2;
str[n++]='*';
for(i=0;i<len;i++) str[n++]=ss[i],str[n++]='*';
for(ml=-1,i=0;i<n;i++)
{
if(i<ml) rl[i]=min(ml-i+1,rl[2*pos-i]);
else rl[i]=1;
for(;rl[i]<=i&&i+rl[i]<n&&str[i+rl[i]]==str[i-rl[i]];rl[i]++);
if(i+rl[i]-1>ml) pos=i,ml=i+rl[i]-1;
}
for(b1[0]=b2[0]=1,i=1;i<=n;i++) b1[i]=b1[i-1]*233%m1,b2[i]=b2[i-1]*2333%m2;
for(i=0;i<n;i++)
{
if(i) h1[i]=h1[i-1]*233%m1,h2[i]=h2[i-1]*2333%m2;
h1[i]=(h1[i]+str[i])%m1,h2[i]=(h2[i]+str[i])%m2;
}
for(i=0;i<n;i++)
{
if(rl[i]==1) continue;
t1=(h1[i+rl[i]-1]-h1[i-1]*b1[rl[i]]%m1+m1)%m1;
t2=(h2[i+rl[i]-1]-h2[i-1]*b2[rl[i]]%m2+m2)%m2;
t=find(t1,t2);
if(t)
{
ans[t]^=(i-1>>1);
continue;
}
add(t1,t2),ans[tot]=(i-1>>1),p[tot]=i,v[rl[i]-1].push_back(tot);
}
for(i=len;i;i--)
{
for(j=0;j<v[i].size();j++)
{
k=v[i][j],mx=max(mx,ans[k]);
if(i==1) continue;
t1=(h1[p[k]+i-1]-h1[p[k]-1]*b1[i]%m1+m1)%m1;
t2=(h2[p[k]+i-1]-h2[p[k]-1]*b2[i]%m2+m2)%m2;
t=find(t1,t2);
if(t)
{
ans[t]^=ans[k];
continue;
}
add(t1,t2),ans[tot]=ans[k],p[tot]=p[k],v[i-1].push_back(tot);
}
v[i].clear();
}
printf("%d\n",mx);
}
int main()
{
int T;
scanf("%d",&T);
while(T--) work();
return 0;
}

【BZOJ4166】月宫的符卡序列 Manacher+hash的更多相关文章

  1. BZOJ 4166: 月宫的符卡序列

    如果使用回文树,节点 \(u\) 的回文串和 \(fail_u\) 的回文串中心不一样,因为回文树的 \(fail\) 指针指向的是最长回文后缀,没法快速解决异或和 考虑魔改回文树,用马拉车来解决,扩 ...

  2. 小妖精的完美游戏教室——东方PROJECT,同人,符卡系统

    //================================================================//// Copyright (C) 东方同人社// All Rig ...

  3. (转)AS3正则:元子符,元序列,标志,数量表达符

    (转)AS3正则:元子符,元序列,标志,数量表达符: AS3正则:元子符,元序列,标志,数量表达符 七月 4th, 2010 归类于 AS3前端技术 作者Linkjun 进行评论 as3正则:元子符, ...

  4. LOJ#6387 「THUPC2018」绿绿与串串 / String (Manacher || hash+二分)

    题目描述 绿绿和 Yazid 是好朋友.他们在一起做串串游戏. 我们定义翻转的操作:把一个串以最后一个字符作对称轴进行翻转复制.形式化地描述就是,如果他翻转的串为 RRR,那么他会将前 ∣R∣−1个字 ...

  5. [BZOJ4755][JSOI2016]扭动的回文串(manacher+Hash)

    前两种情况显然直接manacher,对于第三种,枚举回文中心,二分回文半径,哈希判断即可. #include<cstdio> #include<algorithm> #defi ...

  6. manacher(无讲解)

    BZOJ3325: [Scoi2013]密码 https://lydsy.com/JudgeOnline/problem.php?id=3325 分析: 根据前i个字符和一些不等和相等条件就可以确定每 ...

  7. 省选前的th题

    沙茶博主终于整完了知识点并学完了早该在NOIP之前学的知识们 于是终于开始见题了,之前那个奇怪的题单的结果就是这个了 题目按沙茶博主的做题顺序排序 个人感觉(暂时)意义不大的已被自动忽略 洛谷 491 ...

  8. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  9. 毒瘤养成记1: 如何卡hash

    各位毒瘤大家好, 最近模拟赛考了一道trie+主席树好题, 但大家都用hash水过了这道题(包括我), 为了测试一下新搭建的HEAT OJ的hack功能, 我将继续扮演毒瘤的角色, 用毒瘤的艺术形象努 ...

随机推荐

  1. LeetCode OJ-- Insertion Sort List **

    https://oj.leetcode.com/problems/insertion-sort-list/ 链表实现插入排序 首先插入排序是指: a b c d e g m 对b也就是第二个位置选做元 ...

  2. HDU 1033 Edge[地图型模拟/给你一串字符串,A代表以此点为参照顺时针90°,V代表逆时针90°]

    Edge Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  3. 聚类kmeans算法在yolov3中的应用

    yolov3 kmeans yolov3在做boundingbox预测的时候,用到了anchor boxes.这个anchors的含义即最有可能的object的width,height.事先通过聚类得 ...

  4. Kali Linux 2017中Scapy运行bug解决

    Kali Linux 2017中Scapy运行bug解决   Scapy是一款强大的网络数据包构建工具.在Kali Linux 2017中,当在scapy的命令行中,运行res.graph()生成图形 ...

  5. Unique Binary Search Trees II - LeetCode

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

  6. Maven在[INFO] Generating project in Interactive mode卡住的问题解决

    我的环境: Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-11T00:41:47+08:00) Maven ...

  7. Storyboards Tutorial 03

    这一节主要介绍segues,static table view cells 和 Add Player screen 以及 a game picker screen. Introducing Segue ...

  8. sencha toucha获取 constructor中的数据

    config:{ tmp:null }, constructor : function(conf) { this.config.tmp=conf; } 添加配置属性,然后直接用 this.config ...

  9. angular - 如何支持less和sass(scss)

    更新时间: (2018-7-26) - 使用angular6.x最新版本 新建项目时,我们指定类型: 示例:ng new projectname -style=sass(scss) 实例:ng new ...

  10. 每天5道面试题(二)java基础

    说出Servlet的生命周期,并说出Servlet和CGI的差别 Servlet被server实例化后,容器执行其init方法,请求到达时执行其service方法,service方法自己主动派遣执行与 ...