题目链接: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,维护和,扫描的更多相关文章

  1. HDU 4622 Reincarnation 后缀自动机 // BKDRHash(最优hash)

    Reincarnation Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) P ...

  2. HDU 4622 Reincarnation 后缀自动机

    模板来源:http://blog.csdn.net/zkfzkfzkfzkfzkfzkfzk/article/details/9669747 解法参考:http://blog.csdn.net/dyx ...

  3. Hdu 4622 Reincarnation(后缀自动机)

    /* 字符串长度较小, 可以离线或者直接与处理所有区间的答案 动态加入点的时候, 因为对于其他点的parent构造要么没有影响, 要么就是在两个节点之间塞入一个点, 对于minmax的贡献没有改变 所 ...

  4. hdu 4622 Reincarnation(后缀数组)

    hdu 4622 Reincarnation 题意:还是比较容易理解,给出一个字符串,最长2000,q个询问,每次询问[l,r]区间内有多少个不同的字串. (为了与论文解释统一,这里解题思路里sa数组 ...

  5. HDU 4622 Reincarnation Hash解法详解

    今天想学字符串hash是怎么弄的.就看到了这题模板题 http://acm.hdu.edu.cn/showproblem.php?pid=4622 刚开始当然不懂啦,然后就上网搜解法.很多都是什么后缀 ...

  6. HDU 4622 Reincarnation (查询一段字符串的不同子串个数,后缀自动机)

    Reincarnation Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)To ...

  7. hdu 4622 Reincarnation trie树+树状数组/dp

    题意:给你一个字符串和m个询问,问你l,r这个区间内出现过多少字串. 连接:http://acm.hdu.edu.cn/showproblem.php?pid=4622 网上也有用后缀数组搞得. 思路 ...

  8. Reincarnation HDU - 4622 (后缀自动机)

    Reincarnation \[ Time Limit: 3000 ms\quad Memory Limit: 65536 kB \] 题意 给出一个字符串 \(S\),然后给出 \(m\) 次查询, ...

  9. HDU 6194【后缀数组】

    题目链接[http://acm.hdu.edu.cn/showproblem.php?pid=6194] 题意: 给你一个长度不大于1e5的字符串,然后然你判断其子串严格出现k次的子串个数. 题解: ...

随机推荐

  1. sqlmap动态sql优化,避免传参失误批量修改和删除操作!

    分析以下的sqlmap存在问题: <delete id="deletePartspic" parameterClass="TblSpPartspic"&g ...

  2. Flask, Tornado, GEvent, 以及它们的结合的性能比较

    Flask, Tornado, GEvent, 以及它们的结合的性能比较 英文: http://blog.wensheng.com/2011/10/performance-of-flask-torna ...

  3. CSU1321+SPFA

    简单题 /* 简单的bfs */ #include<algorithm> #include<iostream> #include<string.h> #includ ...

  4. hdu 4652 Dice 概率DP

    思路: dp[i]表示当前在已经投掷出i个不相同/相同这个状态时期望还需要投掷多少次 对于第一种情况有: dp[0] = 1+dp[1] dp[1] = 1+((m-1)*dp[1]+dp[2])/m ...

  5. linux mysql为root用户初始化密码和改变root密码

    初始化密码: 由于安装MySQL完后,MySQL会自动提供一个不带密码的root用户,为了安全起见给root设置密码: #mysqladmin -u root password 123 (123为密码 ...

  6. net.sf.json.JSONException: Object is null

    出现这个错误的原因是net.sf.json.JSONArray或JSONObject转换时,对象内包含另一个对象,而该被包含的对象为NULL,所以抛出异常. 补充: 最可恨的是,明明转换的时候已经成功 ...

  7. Oracle导入(imp )与导出(exp )

    导出exp username/password@orcl file=db.dmp 导入imp username/password@orcl file=h:\db.dmp  full=y 备注:在导入之 ...

  8. SaaS系列介绍之一: SaaS的前身ASP介绍

    1. 引言 未来将越来越不可预测,这是新经济最具挑战性的方面之一.商务和技术上的瞬息万变会产生变化,这既可以看作要防范的威胁,也可以看作应该欢迎的机遇.                         ...

  9. laravel Authentication and Security

    Creating the user modelFirst of all, we need to define the model that is going to be used to represe ...

  10. python的常用概念

    常用的概念 主体字符串 主体列表 内置函数和方法的区别 映射表 引用 迭代器: 1. 字典:单步遍历迭代器 2. 文件:逐行读取的迭代器