LightOJ 1314 Names for Babies

刚开始求height数组的地方写错了,看来还是理解不够透彻啊

所有的子串减去重复的子串

//先在草稿纸上把思想想清楚再动手,不要只是有个大概的思想,不然容易出错
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define N 10005
int n;
char s[N];
int r[N],sa[N],height[N],rank[N],wa[N],wb[N],wv[N],ws[N];
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;
	for(i=0;i<m;i++) ws[i]=0;
	for(i=0;i<n;i++) ws[x[i]=r[i]]++;
	for(i=1;i<m;i++) ws[i]+=ws[i-1];
	for(i=n-1;i>=0;i--) sa[--ws[x[i]]]=i;
	for(j=1,p=1;p<n;m=p,j<<=1){
		for(i=n-j,p=0;i<n;i++) y[p++]=i;
		for(i=0;i<n;i++) if(sa[i]>=j) y[p++]=sa[i]-j;
		for(i=0;i<n;i++) wv[i]=x[y[i]];
		for(i=0;i<m;i++) ws[i]=0;
		for(i=0;i<n;i++) ws[wv[i]]++;
		for(i=1;i<m;i++) ws[i]+=ws[i-1];
		for(i=n-1;i>=0;i--) sa[--ws[wv[i]]]=y[i];
		swap(x,y);
		for(i=1,p=1,x[sa[0]]=0;i<n;i++) x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
	}
}
void calheight(int *r,int *sa,int n){
	int i,j,k=0;
	for(i=1;i<=n;i++) rank[sa[i]]=i;
	for(i=0;i<n;height[rank[i++]]=k)
		for(k?k--:0,j=sa[rank[i]-1];r[i+k]==r[j+k];k++);//各自和其前一位只差1
	//for(i=1;i<=n;i++) printf("%d ",height[i]);
}
int main(){
	#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
	#endif
	int T,p,q;
	int cas=1;
	scanf("%d",&T);
	while(T--){
		scanf("%s",s);
		int n=strlen(s);
		scanf("%d%d",&p,&q);
		int i;
		for(i=0;i<n;i++) r[i]=s[i];
		r[n]=0;
		da(r,sa,n+1,129);
		calheight(r,sa,n);
		int ans=(2*n-p-q+2)*(q-p+1)/2;//长为p-q的所有子串
		for(i=1;i<=n;i++) {
			if(height[i]>=p&&height[i]<=q) ans-=height[i]-p+1;
			else if(height[i]>q) ans-=q-p+1;
		}
		printf("Case %d: %d\n",cas++,ans);
	}
}
时间: 2024-10-23 23:13:55

LightOJ 1314 Names for Babies的相关文章

Light OJ 1314 Names for Babies

http://www.lightoj.com/volume_showproblem.php?problem=1314 题意:给定一个串和p,q,求长度在p到q之间的子串有几种 思路:后缀数组,对于每个位置的贡献是min(n-sa[i],q),然后要减去重复和没有的部分,就是max(height[i],p-1),当然,还要对0取个max 1 #include<cstdio> 2 #include<iostream> 3 #include<cmath> 4 #include

LightOJ1314---Names for Babies

1314 - Names for Babies PDF (English) Statistics Forum Time Limit: 4 second(s) Memory Limit: 32 MB Long time ago, there was a strange kingdom. Peoples of different religions, different cultures used to live there. But as they were different, their na

(最长公共子序列+推导)Love Calculator (lightOJ 1013)

http://www.lightoj.com/volume_showproblem.php?problem=1013 Yes, you are developing a 'Love calculator'. The software would be quite complex such that nobody could crack the exact behavior of the software. So, given two names your software will genera

LightOJ 1338

B - B Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1338 Description In this problem you are given two names, you have to find whether one name is hidden into another. The restrictions are:

LightOJ 1030 Discovering Gold【概率】

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1030 题意:基础概率题. 代码: #include <stdio.h> #include <string.h> #include <vector> #include <string> #include <algorithm> #include <iostream> #include <iterator>

LightOJ - 1370 Bi-shoe and Phi-shoe

题目链接:http://lightoj.com/login_main.php?url=volume_showproblem.php?problem=1370 题目大意:给N个数a[i], N <= 1e6,问使 Φ(x) >= a[i] 成立的最小x的所有x的和是多少. 解题思路:我们知道的是对于素数 m 来说,phi[m] = m - 1.另一方面,对于一个合数 m 来说, phi[m] < phi[x] , x > m && x 是素数. 因此,我们可以认为,每

Palindrome Names

Palindrome Names Kattis - names Anna and Bob are having a baby. They both enjoy the advantage of having palindrome names, meaning that their names are spelled the same way forwards and backwards. Wanting to be good parents, they decide to give their

lightoj 1057 - Collecting Gold(状压dp)

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1057 题解:看似有点下记忆话搜索但是由于他是能走8个方向的也就是说两点的距离其实就是最大的x轴或y轴的差.然后只有15个藏金点状压一下加dfs就行了. #include <iostream> #include <cstring> #include <cstdio> #include <cmath> #define inf 0X3f3f3f

Lightoj 1088 - Points in Segments 【二分】

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1088 题意: 有一维的n个点和q条线段.询问每条线段上的点有多少个: 思路:寻找这些点中对于每条线段的上下界即可. 代码: #include <stdio.h> #include <ctime> #include <math.h> #include <limits.h> #include <complex> #include