BZOJ3172 后缀数组
题意:求出一篇文章中每个单词的出现次数
对样例的解释:
原文是这样的:
a
aa
aaa
注意每个单词后都会换行
所以a出现次数为6,aa为3 (aa中一次,aaa中两次),aaa为1
标准解法好像是AC自动机or后缀自动机,还有人用KMP暴力过的= =
用后缀数组做的。原来没刷过这种类型,顺便复习一下~
Reference:http://blog.sina.com.cn/s/blog_6e63f59e0101bpw5.html
以样例为例:

对于每个单词第一个字母对应的height,向上、向下数出height值大于等于单词长度的height的个数。
注意细节的处理= =
//在BT5下attack的时候把数值调大点(超过2000)就行了,具体可以参见网上破解无线网密码的文章,就是抓包之后的那步,注入数据包的命令
#include "iostream"
#include "cstring"
using namespace std;
#define maxn 1010000 int wa[maxn],wb[maxn],wv[maxn],wws[maxn];
int rank[maxn],height[maxn];
int r[maxn],sa[maxn],ans[maxn],st[maxn],ln[maxn];
int n,len,tl;
char ts[maxn]; int cmp(int *r,int a,int b,int l)
{
return r[a]==r[b]&&r[a+l]==r[b+l];
} void da(int *r,int *sa,int n,int m)
{
int i,j,p,*x=wa,*y=wb,*t;
for(i=; i<m; i++) wws[i]=;
for(i=; i<n; i++) wws[x[i]=r[i]]++;
for(i=; i<m; i++) wws[i]+=wws[i-];
for(i=n-; i>=; i--) sa[--wws[x[i]]]=i;
for(j=,p=; p<n; j*=,m=p)
{ for(p=,i=n-j; i<n; i++) y[p++]=i;
for(i=; i<n; i++) if(sa[i]>=j) y[p++]=sa[i]-j;
for(i=; i<n; i++) wv[i]=x[y[i]];
for(i=; i<m; i++) wws[i]=;
for(i=; i<n; i++) wws[wv[i]]++;
for(i=; i<m; i++) wws[i]+=wws[i-];
for(i=n-; i>=; i--) sa[--wws[wv[i]]]=y[i];
for(t=x,x=y,y=t,p=,x[sa[]]=,i=; i<n; i++)
x[sa[i]]=cmp(y,sa[i-],sa[i],j)?p-:p++;
}
return;
} void calheight(int *r,int *sa,int n)
{
int i,j,k=;
for(i=; i<=n; i++) rank[sa[i]]=i;
for(i=; i<n; height[rank[i++]]=k)
for(k?k--:,j=sa[rank[i]-]; r[i+k]==r[j+k]; k++);
return;
} int main()
{
ios::sync_with_stdio(false);
cin>>n;
len=;
for (int i=;i<=n;i++)
{
st[i]=len;
cin>>ts;
tl=strlen(ts);
ln[i]=tl;
for (int j=len;j<len+tl;j++)
{
char ch=ts[j-len];
int tn=ch;
r[j]=tn;
}
len+=tl;
r[len]=;
len++;
}
r[len]=; da(r,sa,len+,);
calheight(r,sa,len); //for (int i=0;i<=len+1;i++)
// cout<<height[i]<<" ";
//cout<<endl;
for (int i=;i<=n;i++)
{
int tmp=rank[st[i]];
int tl=tmp,tr=tmp+,tlen=ln[i];
while ((height[tl]>=tlen)&&(tl>=)) tl--;
while ((height[tr]>=tlen)&&(tr<=len)) tr++;
cout<<tr-tl<<endl;
} return ;
}
BZOJ3172 后缀数组的更多相关文章
- BZOJ 3172([Tjoi2013]单词-后缀数组第一题+RMQ)
3172: [Tjoi2013]单词 Time Limit: 10 Sec Memory Limit: 512 MB Submit: 268 Solved: 145 [ Submit][ St ...
- 后缀数组的倍增算法(Prefix Doubling)
后缀数组的倍增算法(Prefix Doubling) 文本内容除特殊注明外,均在知识共享署名-非商业性使用-相同方式共享 3.0协议下提供,附加条款亦可能应用. 最近在自学习BWT算法(Burrows ...
- BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]
4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...
- BZOJ 1692: [Usaco2007 Dec]队列变换 [后缀数组 贪心]
1692: [Usaco2007 Dec]队列变换 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1383 Solved: 582[Submit][St ...
- POJ3693 Maximum repetition substring [后缀数组 ST表]
Maximum repetition substring Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9458 Acc ...
- POJ1743 Musical Theme [后缀数组]
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 27539 Accepted: 9290 De ...
- 后缀数组(suffix array)详解
写在前面 在字符串处理当中,后缀树和后缀数组都是非常有力的工具. 其中后缀树大家了解得比较多,关于后缀数组则很少见于国内的资料. 其实后缀数组是后缀树的一个非常精巧的替代品,它比后缀树容易编程实现, ...
- 【UOJ #35】后缀排序 后缀数组模板
http://uoj.ac/problem/35 以前做后缀数组的题直接粘模板...现在重新写一下模板 注意用来基数排序的数组一定要开到N. #include<cstdio> #inclu ...
- 【BZOJ-2119】股市的预测 后缀数组
2119: 股市的预测 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 334 Solved: 154[Submit][Status][Discuss ...
随机推荐
- 3D跑酷遇到的问题
前言 项目名称:3D跑酷项目 写作目地:使用Asset Server进行多人协作开发过程中,记录遇到的问题 问题1:UIAtlas无法自动更新 描述:NGUI的Atlas图集(图片)无法自动更新 后果 ...
- phpmyadmin后台拿shell方法总结
方法一: CREATE TABLE `mysql`.`xiaoma` (`xiaoma1` TEXT NOT NULL ); INSERT INTO `mysql`.`xiaoma` (`xiaoma ...
- this关键字的使用
一,表示类中属性 1,没有使用this的情况 class Person{ // 定义Person类 private String name ; // 姓名 private int age ; // 年 ...
- mysql高可用方案总结性说明
MySQL的各种高可用方案,大多是基于以下几种基础来部署的(也可参考:Mysql优化系列(0)--总结性梳理 该文后面有提到)1)基于主从复制:2)基于Galera协议(PXC):3)基于NDB引 ...
- 18Spring_AOP编程(AspectJ)_AspectJ的各种通知总结
小结: 前置通知(权限控制). 后置通知 ---- 不怎么用 环绕通知(权限控制. 性能监控. 缓存技术 ) 异常通知 (发生异常后, 记录错误日志 ) 最终通知 (释放资源 ) 环绕通知 是取代任何 ...
- C# 改变无边框窗体的尺寸大小
以下代码为修改窗体尺寸的代码: const int HTLEFT = 10; ; ; ; ; ; const int HTBOTTOMLEFT = 0x10; ; protected override ...
- Flume的安装与配置
Flume的安装与配置 一. 资源下载 资源地址:http://flume.apache.org/download.html 程序地址:http://apache.fayea.com/fl ...
- [CareerCup] 1.8 String Rotation 字符串的旋转
1.8 Assume you have a method isSubstring which checks if one word is a substring of another. Given t ...
- 信息安全系统设计基础第四次实验报告20135324&&20135330
课程:信息安全系统设计基础 班级:1353 姓名:杨舒雯 张若嘉 学号:20135324,20135330 实验日期:2015.11.17 15:30-17:30 实验名称:外设驱动程序设计 实验目的 ...
- Git差异比对
一. 查看变更还未载入(changed but unstaged,当前没有add 的内容)的文件比对: 只需运行不带任何参数的'git diff'命令即可 二. 查看载入(stage,即已经add)而 ...