首页 > 其他 > 详细

HDU 4416 后缀数组

时间:2014-04-11 17:15:59      阅读:539      评论:0      收藏:0      [点我收藏+]

Good Article Good sentence

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2033    Accepted Submission(s): 569


Problem Description
In middle school, teachers used to encourage us to pick up pretty sentences so that we could apply those sentences in our own articles. One of my classmates ZengXiao Xian, wanted to get sentences which are different from that of others, because he thought the distinct pretty sentences might benefit him a lot to get a high score in his article.
Assume that all of the sentences came from some articles. ZengXiao Xian intended to pick from Article A. The number of his classmates is n. The i-th classmate picked from Article Bi. Now ZengXiao Xian wants to know how many different sentences she could pick from Article A which don‘t belong to either of her classmates?Article. To simplify the problem, ZengXiao Xian wants to know how many different strings, which is the substring of string A, but is not substring of either of string Bi. Of course, you will help him, won‘t you?
 

Input
The first line contains an integer T, the number of test data.
For each test data
The first line contains an integer meaning the number of classmates.
The second line is the string A;The next n lines,the ith line input string Bi.
The length of the string A does not exceed 100,000 characters , The sum of total length of all strings Bi does not exceed 100,000, and assume all string consist only lowercase characters ‘a‘ to ‘z‘.
 

Output
For each case, print the case number and the number of substrings that ZengXiao Xian can find.
 

Sample Input
3 2 abab ab ba 1 aaa bbb 2 aaaa aa aaa
 

Sample Output
Case 1: 3 Case 2: 3 Case 3: 1


题意:一个字符串,给定若干病毒串,求不包含病毒串的字串个数。

把病毒串添加在字符串后面,中间用不同的字符隔开,然后求一个sa,height,然后从后向前扫,

去掉的包含两个部分,一个是字符串本身已经出现过的,另外一个是包含病毒串的,

代码:

/* ***********************************************
Author :rabbit
Created Time :2014/4/10 16:33:22
File Name :A.cpp
************************************************ */
#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <string>
#include <time.h>
#include <math.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-8
#define pi acos(-1.0)
typedef long long ll;
const int maxn=500200;
int t1[maxn],t2[maxn],c[maxn];
bool cmp(int *r,int a,int b,int l){
	return r[a]==r[b]&&r[a+l]==r[b+l];
}
void da(int str[],int sa[],int rank[],int height[],int n,int m){
	n++;
	int i,j,p,*x=t1,*y=t2;
	for(i=0;i<m;i++)c[i]=0;
	for(i=0;i<n;i++)c[x[i]=str[i]]++;
	for(i=1;i<m;i++)c[i]+=c[i-1];
	for(i=n-1;i>=0;i--)sa[--c[x[i]]]=i;
	for(j=1;j<=n;j<<=1){
		p=0;
		for(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<m;i++)c[i]=0;
		for(i=0;i<n;i++)c[x[y[i]]]++;
		for(i=1;i<m;i++)c[i]+=c[i-1];
		for(i=n-1;i>=0;i--)sa[--c[x[y[i]]]]=y[i];
		swap(x,y);
		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++;
		if(p>=n)break;
		m=p;
	}
	int k=0;n--;
	for(i=0;i<=n;i++)rank[sa[i]]=i;
	for(i=0;i<n;i++){
		if(k)k--;
		j=sa[rank[i]-1];
		while(str[i+k]==str[j+k])k++;
		height[rank[i]]=k;
	}
}
int rank[maxn],height[maxn],r[maxn],sa[maxn];
char str[maxn];
int main()
{
     //freopen("data.in","r",stdin);
     //freopen("data.out","w",stdout);
     int T;
	 scanf("%d",&T);
	 for(int t=1;t<=T;t++){
		 int len,len1,n;
		 scanf("%d",&n);
		 scanf("%s",str);
		 len=len1=strlen(str);
		 for(int i=0;i<len;i++)r[i]=str[i];
		 r[len++]=127;
		 while(n--){
			 scanf("%s",str);
			 int cnt=strlen(str);
			 for(int i=0;i<cnt;i++)
				 r[len++]=str[i];
			 r[len++]=‘#‘;
		 }
		 r[len]=0;
		 da(r,sa,rank,height,len,200);
		 ll ans=0;int lcp=0;
		 for(int i=1;i<=len;i++){
			 if(sa[i]>len1)lcp=height[i];
			 else{
				 ans+=len1-sa[i]-max(height[i],lcp);
				 lcp=min(lcp,height[i]);
			 }
		 }
		 printf("Case %d: %I64d\n",t,ans);  
	 }
     return 0;
}


HDU 4416 后缀数组,布布扣,bubuko.com

HDU 4416 后缀数组

原文:http://blog.csdn.net/xianxingwuguan1/article/details/23369141

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!