HDU-4622 Reincarnation 后缀数组 | Hash,维护和,扫描
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4622
题意:给一个字符串,询问某字串的不同字串的个数。
可以用后缀数组来解决,复杂度O(n)。先求出倍增算法求出sa数组,然后DP求出Height数组,对于每个询问,重构sa数组,这里重构可以利用整个串的sa数组来得到,但是这里截取的字串,可能顺序会变化,看一个例子:cacbe
排序为:acbe,be,cacbe,cbe,e 得到字串[0,2]的排序是:acbe(1),cacbe(0),cbe(2) 但cac的实际排序是:ac(1),c(2),cac(0) 后缀0和2的顺序反了,因此我们要保留的是在子串里面长度尽量长的串。。。
Hash做法就是先用Hash把所有的字串取出来,可以用BKDRHash,基本认为不产生冲突,产生冲突的概率很小,然后维护一个矩阵ans[i][j],表示字串[j,i]的不同字串的个数。处理所有串,对于相同的串[l1,r1],[l2,r2]...那么询问[L,R],l1<L<=l2,r1<=R<=r2,则ans[r2][r1+1]++,ans[r2][l2+1]++。最后遍历两遍矩阵,类似扫描线,求出答案。。
后缀数组:
//STATUS:C++_AC_1218MS_248KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
//typedef __int64 LL;
//typedef unsigned __int64 ULL;
//const
const int N=;
const int INF=0x3f3f3f3f;
const int MOD=,STA=;
//const LL LNF=1LL<<60;
const double EPS=1e-;
const double OO=1e15;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End
char s[N];
int num[N];
int sa[N],t1[N],t2[N],c[N],rank[N],height[N];
int T,n,m; void build_sa(int s[],int n,int m)
{
int i,k,p,*x=t1,*y=t2;
//第一轮基数排序
for(i=;i<m;i++)c[i]=;
for(i=;i<n;i++)c[x[i]=s[i]]++;
for(i=;i<m;i++)c[i]+=c[i-];
for(i=n-;i>=;i--)sa[--c[x[i]]]=i;
for(k=;k<=n;k<<=){
p=;
//直接利用sa数组排序第二关键字
for(i=n-k;i<n;i++)y[p++]=i;
for(i=;i<n;i++)if(sa[i]>=k)y[p++]=sa[i]-k;
//基数排序第一关键字
for(i=;i<m;i++)c[i]=;
for(i=;i<n;i++)c[x[y[i]]]++;
for(i=;i<m;i++)c[i]+=c[i-];
for(i=n-;i>=;i--)sa[--c[x[y[i]]]]=y[i];
//根据sa和x数组计算新的x数组
swap(x,y);
p=;x[sa[]]=;
for(i=;i<n;i++)
x[sa[i]]=y[sa[i-]]==y[sa[i]] && y[sa[i-]+k]==y[sa[i]+k]?p-:p++;
if(p>=n)break; //已经排好序,直接退出
m=p; //下次基数排序的最大值
}
} void getHeight(int s[],int n)
{
int i,j,k=;
for(i=;i<=n;i++)rank[sa[i]]=i;
for(i=;i<n;i++){
if(k)k--;
j=sa[rank[i]-];
while(s[i+k]==s[j+k])k++;
height[rank[i]]=k;
}
} int main()
{
// freopen("in.txt","r",stdin);
int i,j,a,b,l,up,low,ans,mlen,t;
scanf("%d",&T);
while(T--)
{
scanf("%s",s);
n=strlen(s);
for(i=;i<n;i++)
num[i]=s[i]-'a'+;
num[n]=;
build_sa(num,n+,);
getHeight(num,n); scanf("%d",&m);
while(m--){
scanf("%d%d",&a,&b);
a--,b--;
l=ans=low=mlen=,up=b-a+;
for(i=;i<=n && l<up;i++){
if(sa[i]<a || sa[i]>b){
low=Min(low,height[i+]);
continue;
}
t=b-sa[i]+;
ans+=t-Min(low,t,mlen);
if(t>=mlen || (t<mlen && low<t) || low==){
mlen=t;
low=height[i+];
}
else low=Min(low,height[i+]);
l++;
}
printf("%d\n",ans);
}
}
return ;
}
Hash,维护和,扫描:
//STATUS:C++_AC_1343MS_29980KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,102400000")
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef __int64 LL;
typedef unsigned __int64 ULL;
//const
const int N=;
const int INF=0x3f3f3f3f;
//const int MOD=100000,STA=8000010;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e15;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End char s[N];
int w[N][N],ans[N][N];
int l[N<<];
int n,m; const int MOD=,STA=; //MOD为表长,STA为表大小 struct Hash{
int first[MOD],next[STA],size;
int f[STA],sta[STA]; //sta[]存放状态,f[]为对应状态的权值
void init(){
size=;
memset(first,-,sizeof(first));
}
int find_add(int st,int ans){ //查找,如果未查找到则添加
int i,u=st%MOD;
for(i=first[u];i!=-;i=next[i]){
if(sta[i]==st){
return f[i]; //已存在状态
}
}
sta[size]=st;
f[size]=ans;
next[size]=first[u];
first[u]=size++;
return f[i];
}
}hs; void BKDRHash()
{
int i,j,hash,seed=;
for(i=;i<n;i++){
hash=;
for(j=i;j<n;j++){
hash=hash*seed+s[j];
w[i][j]=hash&0x7fffffff;
}
}
} int main(){
// freopen("in.txt","r",stdin);
int i,j,T,d,p,a,b;
scanf("%d",&T);
while(T--){
scanf("%s",s);
n=strlen(s);
BKDRHash(); mem(ans,);
for(d=;d<n;d++){
hs.init();
for(i=;i<n-d;i++)
hs.find_add(w[i][i+d],i);
mem(l,-);
for(i=;i<n-d;i++){
p=hs.find_add(w[i][i+d],);
ans[i+d][l[p]+]++;
ans[i+d][i+]--;
l[p]=i;
}
}
for(i=;i<n;i++)
for(j=;j<n;j++)
ans[i][j]+=ans[i][j-];
for(j=;j<n;j++)
for(i=;i<n;i++)
ans[i][j]+=ans[i-][j]; scanf("%d",&m);
while(m--){
scanf("%d%d",&a,&b);
printf("%d\n",ans[b-][a-]);
}
}
return ;
}
HDU-4622 Reincarnation 后缀数组 | Hash,维护和,扫描的更多相关文章
- HDU 4622 Reincarnation 后缀自动机 // BKDRHash(最优hash)
Reincarnation Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) P ...
- HDU 4622 Reincarnation 后缀自动机
模板来源:http://blog.csdn.net/zkfzkfzkfzkfzkfzkfzk/article/details/9669747 解法参考:http://blog.csdn.net/dyx ...
- Hdu 4622 Reincarnation(后缀自动机)
/* 字符串长度较小, 可以离线或者直接与处理所有区间的答案 动态加入点的时候, 因为对于其他点的parent构造要么没有影响, 要么就是在两个节点之间塞入一个点, 对于minmax的贡献没有改变 所 ...
- hdu 4622 Reincarnation(后缀数组)
hdu 4622 Reincarnation 题意:还是比较容易理解,给出一个字符串,最长2000,q个询问,每次询问[l,r]区间内有多少个不同的字串. (为了与论文解释统一,这里解题思路里sa数组 ...
- HDU 4622 Reincarnation Hash解法详解
今天想学字符串hash是怎么弄的.就看到了这题模板题 http://acm.hdu.edu.cn/showproblem.php?pid=4622 刚开始当然不懂啦,然后就上网搜解法.很多都是什么后缀 ...
- HDU 4622 Reincarnation (查询一段字符串的不同子串个数,后缀自动机)
Reincarnation Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)To ...
- hdu 4622 Reincarnation trie树+树状数组/dp
题意:给你一个字符串和m个询问,问你l,r这个区间内出现过多少字串. 连接:http://acm.hdu.edu.cn/showproblem.php?pid=4622 网上也有用后缀数组搞得. 思路 ...
- Reincarnation HDU - 4622 (后缀自动机)
Reincarnation \[ Time Limit: 3000 ms\quad Memory Limit: 65536 kB \] 题意 给出一个字符串 \(S\),然后给出 \(m\) 次查询, ...
- HDU 6194【后缀数组】
题目链接[http://acm.hdu.edu.cn/showproblem.php?pid=6194] 题意: 给你一个长度不大于1e5的字符串,然后然你判断其子串严格出现k次的子串个数. 题解: ...
随机推荐
- HDU 1885 Key Task(三维BFS)
题目链接 题意 : 出口不止一个,一共有四种颜色不同的门由大写字母表示,而钥匙则是对应的小写字母,当你走到门前边的位置时,如果你已经走过相应的钥匙的位置这个门就可以走,只要获得一把钥匙就可以开所有同颜 ...
- PowerDesigner将name自动添加到Comment注释的方法 VB代码
Option Explicit ValidationMode = True InteractiveMode = im_Batch Dim mdl ' the current model ' get t ...
- *[topcoder]GUMIAndSongsDiv1
http://community.topcoder.com/stat?c=problem_statement&pm=12706&rd=15700 这题有意思.首先要观察到,如果选定一些 ...
- P99、面试题13:在o(1)时间删除链表结点
题目:给定单向链表的头指针和一个结点指针,定义一个函数在o(1)时间删除该结点.链表结点与函数的定义如下:struct ListNode{ int m_nValue; List ...
- android 电容屏(一):电容屏基本原理篇
平台信息: 内核:linux3.4.39系统:android4.4 平台:S5P4418(cortex a9) 作者:瘋耔(欢迎转载,请注明作者) 欢迎指正错误,共同学习.共同进步!! 关注博主新浪博 ...
- 关于捕获键盘信息的processDialogkey方法2--具体应用
自定义控件里的keydown方法无法捕获所有的按键消息的处理方法1(自定义控件里的keydown方法无法获取的键值如上下左右键等) 处理办法具体如下: 1.首先在自定义控件UserControl1中重 ...
- Glimpse
给自己程序配好Glimpse. Glimpse.Mvc 有问题 遇到稍微复杂点的内套多个PartialView,内存就爆了彪1.7g,不开Glimpse一点问题都没.另外Glimpse.Nlog也有问 ...
- [POJ 1365] Prime Land
Prime Land Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 3211 Accepted: 1473 Descri ...
- apache开源项目--TIKA
Tika是一个内容抽取的工具集合(a toolkit for text extracting).它集成了POI, Pdfbox 并且为文本抽取工作提供了一个统一的界面.其次,Tika也提供了便利的扩展 ...
- 剑指Offer:二进制中1的个数
题目:输入一个整数,输出该数二进制表示中1的个数. // 二进制中1的个数 #include <stdio.h> int wrong_count_1_bits(int n) // 错误解法 ...