JZOJ.5315【NOIP2017模拟8.19】小串串
Description
Input
Output
Sample Input
4
aa
abcd
ccc
abcc
Sample Output
5
10
14
12
Data Constraint
字符串操作我们可以采用任何后缀的东西,包括后缀数组,后缀自动机,后缀树等等...
鉴于某蒟蒻只会后缀数组下面我们采用后缀数组的方法
本题要求出某字符子串在原串出现的次数,我们对原串求出height数组后,将height[i]值作为纵坐标,横坐标为i我们可以发现

矩形宽度即为某子串出现的次数,高度为该串的长度,每个单位高度的矩形就对答案贡献宽度的平方值,于是我们用单调栈来统计这矩形对答案的贡献,然后剩下的子串对答案都贡献1即可。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#define N 100005
using namespace std;
long long t,n,sa[N],rank[N],tp[N],tax[N],h[N],len,a[N],top,id[N],sum,m,ans,oo[N];
char qwq[N];
void rsort(){
for (int i=;i<=m;i++) tax[i]=;
for (int i=;i<=len;i++) tax[rank[tp[i]]]++;
for (int i=;i<=m;i++) tax[i]+=tax[i-];
for (int i=len;i>=;i--) sa[tax[rank[tp[i]]]--]=tp[i];
}
bool comp(long long *f,int x,int y,int w){
return ((f[x]==f[y])&&(f[x+w]==f[y+w]));
}
void SS(){
for (int i=;i<=len;i++) rank[i]=a[i],tp[i]=i;
m=;rsort();
for (int i,p=,w=;p<len;w+=w,m=p){
for (p=,i=len-w+;i<=len;i++) tp[++p]=i;
for (i=;i<=len;i++) if (sa[i]>w) tp[++p]=sa[i]-w;
rsort();swap(tp,rank);rank[sa[]]=;p=;
for (i=;i<=len;i++) rank[sa[i]]=comp(tp,sa[i],sa[i-],w)?p:++p;
}
int j=,k=;
for (int i=;i<=len;oo[rank[i++]]=k)
for (k=k?k-:k,j=sa[rank[i]-];a[i+k]==a[j+k];k++);
}
void work(){
long long tmp=;
for (long long i=;i<=len;i++){
while ((h[id[top]]>=h[i])&&(top>)){
long long x=i-id[top-];
long long y=h[id[top]]-max(h[id[top-]],h[i]);
ans+=x*x*y;
sum+=x*y;
top--;
}
id[++top]=i;
}
ans+=(n-sum);
printf("%lld\n",ans);
}
void clear(){
memset(sa,,sizeof(sa));
memset(rank,,sizeof(rank));
memset(h,,sizeof(h));
memset(id,,sizeof(id));
memset(a,,sizeof(a));
top=;
ans=;
sum=;
len=;
n=;
}
int main(){
for (scanf("%lld",&t);t;t--){
scanf("%s",qwq);
n=strlen(qwq);
for (int i=;i<n;i++)
a[i+]=(long long)qwq[i]-(long long)'a'+;
len=n;
n=n*(n+)/;
SS();
for (int i=;i<=len;i++)
h[i]=oo[i+];
work();
clear();
}
return ;
}
神奇的代码
第一次打单调栈打到崩溃QAQ
JZOJ.5315【NOIP2017模拟8.19】小串串的更多相关文章
- [jzoj 5343] [NOIP2017模拟9.3A组] 健美猫 解题报告 (差分)
题目链接: http://172.16.0.132/senior/#main/show/5343 题目: 题解: 记旋转i次之后的答案为$ans_i$,分别考虑每个元素对ans数组的贡献 若$s_i& ...
- JZOJ 5246. 【NOIP2017模拟8.8A组】Trip(trip)
5246. [NOIP2017模拟8.8A组]Trip(trip) (File IO): input:trip.in output:trip.out Time Limits: 1500 ms Memo ...
- JZOJ 5236. 【NOIP2017模拟8.7A组】利普希茨
5236. [NOIP2017模拟8.7A组]利普希茨 (File IO): input:lipschitz.in output:lipschitz.out Time Limits: 1000 ms ...
- JZOJ 【NOIP2017提高A组模拟9.14】捕老鼠
JZOJ [NOIP2017提高A组模拟9.14]捕老鼠 题目 Description 为了加快社会主义现代化,建设新农村,农夫约(Farmer Jo)决定给农庄里的仓库灭灭鼠.于是,猫被农夫约派去捕 ...
- JZOJ 5235. 【NOIP2017模拟8.7A组】好的排列
5235. [NOIP2017模拟8.7A组]好的排列 (File IO): input:permutation.in output:permutation.out Time Limits: 1000 ...
- JZOJ 5230. 【NOIP2017模拟A组模拟8.5】队伍统计
5230. [NOIP2017模拟A组模拟8.5]队伍统计 (File IO): input:count.in output:count.out Time Limits: 1500 ms Memory ...
- 模拟XShell的小项目
不知道大家有没有用过XShell这款工具,这款工具通过windows可以远程操作处于开机状态的linux操作系统,也就是说把你的电脑和一台服务器连入网络,你通过输入服务器所在的IP地址建立一个会话就可 ...
- 常用Java API之Ramdom--用代码模拟猜数小游戏
常用Java API之Ramdom Ramdom类用来生成随机数字.使用起来也是三个步骤: 1.导包 import java.util.Random; 2.创建 Random r = new Rand ...
- JZOJ【NOIP2013模拟联考14】隐藏指令
JZOJ[NOIP2013模拟联考14]隐藏指令 题目 Description 在d维欧几里得空间中,指令是一个长度为2N的串.串的每一个元素为d个正交基的方向及反方向之一.例如,d = 1时(数轴) ...
随机推荐
- C#:xml操作(待补充)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.X ...
- Python装饰器(Decorator)简介
Python有许多出色的语言特性,装饰器(Decorator)便是其中一朵奇葩.先来看看一段代码: def deco1(f): print 'decorate 1' return f def deco ...
- firefox浏览器批处理插件imacros
http://www.360doc.com/content/14/1012/19/4360822_416372016.shtml javascript部分 var code = "CODE: ...
- 为什么在c语言中使用gets函数是危险的
If you have code like this: char s[10]; gets( s ); and you type in more than 10 characters when th ...
- MySQL慢查询查找和调优测试
MySQL慢查询查找和调优测试,接下来详细介绍,需要了解的朋友可以参考下.本文参考自:http://www.jbxue.com/db/4376.html 编辑 my.cnf或者my.ini文件,去除 ...
- Xilinx-7Series-FPGA高速收发器使用学习—TX发送端介绍
每一个收发器拥有一个独立的发送端,发送端有PMA(Physical Media Attachment,物理媒介适配层)和PCS(PhysicalCoding Sublayer,物理编码子层)组成,其中 ...
- swift 函数.和匿名函数
函数 注意: 没有定义返回类型的函数会返回特殊的值,叫 Void.它其实是一个空的元组(tuple),没有任何元素,可以写成(). 使用元组作为返回参数,返回多个参数 func count(strin ...
- Recusively change the owner of files (chown) on Mac OS X
I've just changed my OS X / Mac main user - I've created a new 'coder' user account that I want to u ...
- UITableView__cell 距tableview顶端有间距
UITableView__cell 距tableview顶端有间距 如何去掉这个间距呢?解决方法如下: //top 为cell距顶端的间距 (一般为负值) self.formTable.con ...
- visual studio 2013 触发挂起事件
在 VS2013 中调试 winddows phone 或者 win rt 程序的时候,需要手动触发 “挂起” 事件. 如果找不到这个按钮: 1.打开菜单栏中的 “自定义” 对话框: 2.选择调试位置 ...