hdu 5769 Substring 后缀数组 + KMP
http://acm.hdu.edu.cn/showproblem.php?pid=5769
题意:在S串中找出X串出现的不同子串的数目? 其中1 <= |S| < $10^5$
官方题解: 处理出后缀数组中的sa[]数组和height[]数组。在不考虑包含字符X的情况下,不同子串的个数为
如果要求字符X,只需要记录距离sa[i]最近的字符X的位置(用nxt[sa[i]]表示)即可,个数
理解:后缀数组height[i]就是sa[i]与sa[i-1]的LCP,在后缀数组中求解全部的不同子串(之前只写过SAM处理所有不同子串..)还是比较好理解的,在一定要含有子串x时,需要先kmp求出所有匹配的位置,在处理到第i个后缀时,取max即可表示一定含有X,并且是不同的子串;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<bits/stdc++.h>
using namespace std;
#define rep0(i,l,r) for(int i = (l);i < (r);i++)
#define rep1(i,l,r) for(int i = (l);i <= (r);i++)
#define rep_0(i,r,l) for(int i = (r);i > (l);i--)
#define rep_1(i,r,l) for(int i = (r);i >= (l);i--)
#define MS0(a) memset(a,0,sizeof(a))
#define MS1(a) memset(a,-1,sizeof(a))
#define MSi(a) memset(a,0x3f,sizeof(a))
#define inf 0x3f3f3f3f
#define pb push_back
#define MK make_pair
#define A first
#define B second
#define eps 1e-8
#define zero(x) (((x)>0?(x):-(x))<eps)
#define bitnum(a) __builtin_popcount(a)
#define lowbit(x) (x&(-x))
#define clear0 (0xFFFFFFFE)
#define mod 1000000007
#define K(x) ((x)*(x))
typedef pair<int,int> PII;
typedef long long ll;
typedef unsigned long long ull;
template<typename T>
void read1(T &m)
{
T x = ,f = ;char ch = getchar();
while(ch <'' || ch >''){ if(ch == '-') f = -;ch=getchar(); }
while(ch >= '' && ch <= ''){ x = x* + ch - '';ch = getchar(); }
m = x*f;
}
template<typename T>
void read2(T &a,T &b){read1(a);read1(b);}
template<typename T>
void read3(T &a,T &b,T &c){read1(a);read1(b);read1(c);}
template<typename T>
void out(T a)
{
if(a>) out(a/);
putchar(a%+'');
}
inline ll gcd(ll a,ll b){ return b == ? a: gcd(b,a%b); }
template<class T1, class T2> inline void gmax(T1& a, T2 b){ if(a < b) a = b;}
template<class T1, class T2> inline void gmin(T1& a, T2 b){ if(a > b) a = b;}
const int maxn = ;
int sa[maxn], t[maxn],t2[maxn],c[maxn],w[maxn];
int cmp(int *r,int a,int b,int l)
{
return r[a] == r[b] && r[a+l] == r[b+l];
}
void build_sa(char *r,int n,int m)
{
int i, j, p, *x = t, *y = t2;
for(i = ; i < m;i++) c[i] = ;
for(i = ; i < n;i++) c[x[i] = r[i]]++;
for(i = ; i < m;i++) c[i] += c[i-];
for(i = n-; i >= ; i--) sa[--c[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++) w[i] = x[y[i]];
for(i = ; i < m; i++) c[i] = ;
for(i = ; i < n; i++) c[w[i]]++;
for(i = ; i < m; i++) c[i] += c[i-];
for(i = n-; i >= ; i--) sa[--c[w[i]]] = y[i];
swap(x,y);
for(p = , x[sa[]] = , i = ; i < n; i++)
x[sa[i]] = cmp(y, sa[i-], sa[i], j)? p-: p++;
if(p >= n) break;
m = p;
}
}
char S[maxn], X[maxn];
int height[maxn], rk[maxn];
void getHeight(int n)
{
for(int i = ; i<= n; i++) rk[sa[i]] = i;
for(int i = , j, k = ; i < n; height[rk[i++]] = k)
for(k? k--:, j = sa[rk[i] - ]; S[i+k] == S[j+k]; k++);
} int f[maxn];
void getfail(char *p)
{
f[] = f[] = ;
int n = strlen(p);
for(int i = ;i < n;i++){
int j = f[i];
if(j && p[i] != p[j]) j = f[j];
f[i+] = (p[i] == p[j] ?j+:);// i+1会递推到第n位
}
}
vector<int> vec;
void Find(char *T, char *p)
{
vec.clear();
ll j = ,n = strlen(T),m = strlen(p);
for(int i = ;i < n;i++){
while(j && T[i] != p[j]) j = f[j];
if(T[i] == p[j]) j++;
if(j == m){
vec.pb(i);
j = ;
i -= m-;
}
}
sort(vec.begin(), vec.end());
} int main()
{
//freopen("data.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T, kase = ;
scanf("%d",&T);
while(T--){
scanf("%s%s", X, S);
int len = strlen(S), m = strlen(X);
S[len] = '#';S[len+] = ;
build_sa(S,len+,'z'+);
getHeight(len); getfail(X);
Find(S,X);
vec.pb(len);
ll ans = ;
rep1(i,,len){
if(sa[i]+m- > len) continue;
int nxt = lower_bound(vec.begin(), vec.end(), sa[i]+m-) - vec.begin();
ans += len - max(vec[nxt],sa[i] + height[i]);
}
printf("Case #%d: %lld\n", kase++, ans);
}
return ;
}
hdu 5769 Substring 后缀数组 + KMP的更多相关文章
- HDU 5769 Substring 后缀数组
Substring Problem Description ?? is practicing his program skill, and now he is given a string, he h ...
- HDU 5679 Substring 后缀数组判重
题意:求母串中有多少不同的包含x字符的子串 分析:(首先奉上FZU官方题解) 上面那个题就是SPOJ694 ,其实这两个题一样,原理每次从小到大扫后缀sa数组,加上新的当前后缀的若干前缀,再减去重复的 ...
- hdu5442(2015长春赛区网络赛1006)后缀数组+KMP /最小表示法?
题意:给定一个由小写字母组成的长度为 n 的字符串,首尾相连,可以从任意一个字符开始,顺时针或逆时针取这个串(长度为 n),求一个字典序最大的字符串的开始字符位置和顺时针或逆时针.如果有多个字典序最大 ...
- HDU 5769 Substring(后缀数组)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5769 [题目大意] 在一个串中求出包含字母的子串个数, 只要存在一个字符不相等的子串即可视为不同的 ...
- hdu 1403 Longest Common Substring 后缀数组 模板题
题目链接 题意 问两个字符串的最长公共子串. 思路 加一个特殊字符然后拼接起来,求得后缀数组与\(height\)数组.扫描一遍即得答案,注意判断起始点是否分别在两个串内. Code #include ...
- POJ3080 POJ3450Corporate Identity(广义后缀自动机||后缀数组||KMP)
Beside other services, ACM helps companies to clearly state their “corporate identity”, which includ ...
- POJ3693 Maximum repetition substring [后缀数组 ST表]
Maximum repetition substring Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9458 Acc ...
- UVA 11475 后缀数组/KMP
题目链接: 题意:给定一个只含字母的字符串,求在字符串末尾添加尽量少的字符使得字符串为回文串. 思路:因为只能从末尾添加字符,所以其实求的是最长的后缀回文串.那么添加的字符为除了这个原串的最长后缀回文 ...
- POJ 3450 后缀数组/KMP
题目链接:http://poj.org/problem?id=3450 题意:给定n个字符串,求n个字符串的最长公共子串,无解输出IDENTITY LOST,否则最长的公共子串.有多组解时输出字典序最 ...
随机推荐
- NSDate简单介绍
NSDate简单介绍 一:NSDate是一个日期\时间方面的类,主要用来创建\获取时间 1.NSDate对象的创建: date 创建一个当前系统日期和时间的对象 dateWithTimeInterva ...
- 使用Nginx SSI功能辅助HTML页面设计
SSI,Server Side Include,支持html静态文件内以 <!--#include file="/layout/header.html"--> 的方式 ...
- PHP trim()函数的一些用法
string trim ( string $str [, string $charlist ] ) - 去除字符串首尾处的空白字符(或者其他字符) trim()函数当第二个参数为空时,默认去掉空格.制 ...
- 剑指Offer16 判断子树
/************************************************************************* > File Name: 17_Mirror ...
- 1822: [JSOI2010]Frozen Nova 冷冻波 二分最大流
1822: [JSOI2010]Frozen Nova 冷冻波 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 585 Solved: 175[Subm ...
- 转: 学习开源项目的若干建议(infoq)
转: http://www.infoq.com/cn/news/2014/04/learn-open-source 学习开源项目的若干建议 作者 崔康 发布于 2014年4月11日 | 注意:GTLC ...
- [wordpress]根据自定义字段排序并根据自定义字段查询
Wordpress中,根据根据自定义字段排序和查询是通过WP_Query()方法 如根据 一个自定义的sort的数字字段从小到大进行排序 $args = array( 'post_type' => ...
- MongoDB - The mongo Shell, Data Types in the mongo Shell
MongoDB BSON provides support for additional data types than JSON. Drivers provide native support fo ...
- Android高级音频应用
说到音频应用,首先想到的就是音乐播放器.有些播放器可以播放流媒体,有些可以播放本地音乐文件.随着Android平台的演变,需要更多高级的音频API.好在谷歌新增了这方面的API,支持低延迟的音频流媒体 ...
- 解决ashx文件下的Session“未将对象引用设置到对象的实例”
using System; using System.Collections.Generic; using System.Linq; using System.Web; using PPT_DAL; ...