Hdu 3948 The Number of Palindromes 后缀数组+Manacher

The Number of Palindromes

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 2692    Accepted Submission(s): 932


Problem Description
Now, you are given a string S. We want to know how many distinct substring of S which is palindrome.
 

Input
The first line of the input contains a single integer T(T<=20), which indicates number of test cases.
Each test case consists of a string S, whose length is less than 100000 and only contains lowercase letters.
 

Output
For every test case, you should output "Case #k:" first in a single line, where k indicates the case number and starts at 1. Then output the number of distinct substring of S which is palindrome.
 

Sample Input
  
  
3 aaaa abab abcd
 

Sample Output
  
  
Case #1: 4 Case #2: 4 Case #3: 4
 

Source


求不同的回文子串的个数。


先用manacher求出以某位为回文中心的回文子串个数,再用后缀数组去重。

对于每个sa[i],维护以sa[i]为回文中心的串,最多多算了多少个。

如果当前的sa[i]对答案没有贡献,多出来的部分最多是height[i]。否则,将多出来的部分置为当前位置的sa[i]的回文半径。


#include <cstdio>
#include <string.h>
#include <string> 
#include <map>
#include <queue>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
const int maxn=200005,inf=0x3f3f3f3f;  
const ll llinf=0x3f3f3f3f3f3f3f3f;   
const ld pi=acos(-1.0L);
int wa[maxn],wb[maxn],wv[maxn],ws[maxn],sa[maxn],ranki[maxn],height[maxn];
int a[maxn],rl[maxn];
char s[maxn],t[maxn];

int cmp(int *r,int a,int b,int l) {
	return r[a]==r[b]&&r[a+l]==r[b+l];
}

void build(int *r,int *sa,int n,int m) {
	int i,j,k,p,*x=wa,*y=wb,*t;
	for (i=0;i<m;i++) ws[i]=0;
	for (i=0;i<n;i++) ws[x[i]=r[i]]++;
	for (i=0;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;j*=2,m=p) {
		for (p=0,i=n-j;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];
		t=x;x=y;y=t;
		p=1;x[sa[0]]=0;
		for (i=1;i<n;i++) 
		    x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
	}
	for (i=1;i<n;i++) ranki[sa[i]]=i;
	k=0; 
	for (i=0;i<n-1;height[ranki[i++]]=k) {
		if (k) k--;
		for (j=sa[ranki[i]-1];r[i+k]==r[j+k];k++);
	}
}

void manacher(int len) {
	int i,maxright,pos;
	maxright=pos=0;
	for (i=0;i<len;i++) {
		if (i<=maxright) {
			rl[i]=min(rl[2*pos-i],maxright-i);
		} else rl[i]=1;
		while (i-rl[i]>=0&&i+rl[i]<len&&s[i-rl[i]]==s[i+rl[i]])
		    rl[i]++;
		if (i+rl[i]-1>maxright) {
			maxright=i+rl[i]-1;
			pos=i;
		}
	}
}

int main() {
	int cas,cnt=0;
	scanf("%d",&cas);
	while (cas--) {
		cnt++;
		scanf("%s",t);
		int len=strlen(t),i;
		for (i=0;i<len;i++) {
			s[i*2]='#';
			s[i*2+1]=t[i];
			a[i*2+1]=t[i]-'a'+2;
			a[i*2]=1;
		}
		s[len*2]='#';s[len*2+1]='@';a[len*2]=1;a[len*2+1]=0;
		manacher(len*2+2);
		build(a,sa,len*2+2,30);
		int sum=0,MX=0;
		for (i=1;i<=len*2+1;i++) {
			MX=min(MX,height[i]);
			if (rl[sa[i]]<=MX) continue;
			sum+=(rl[sa[i]]-MX)/2;
			MX=rl[sa[i]];
		}
		printf("Case #%d: %d\n",cnt,sum);
	}
	return 0;
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值