标签:
|
Good Article Good sentenceTime Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2784 Accepted Submission(s): 785
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
Sample Output
Source
Recommend
|
思路来自爱酱http://blog.csdn.net/acm_cxlove/article/details/8013942
其实这种子串问题,比较明显是后缀数组,但是当时的数据让我没办法把所有的串拼接在一起。哎~~~~
将所有的串拼接在一起,中间用一个不同的字符分隔开。然后求一次后缀数组以及height数组。
然后对于A中的某一个后缀,统计一下有B中的LCA有多少,就OK了,说明有A的这个后缀有LCA个子串在B中出现过。
只需要从前往后以及从后往前统计一次height就OK了。注意我们这里统计的是A与B的LCA。如果连续的两个sa是A中,那我们需要求一次最小值,保证求的是和B串的LCA。
但是题目要求的是A中的不同的子串,所以还要去重,遍历一次,如果连续两个都是A串的,则更新一下
#include<stdio.h> #include<string.h> #include<algorithm> #include<iostream> #define min(a,b) (a>b?b:a) #define max(a,b) (a>b?a:b) using namespace std; #define INF 0x3f3f3f3f char str[303030]; int sa[303030],Rank[303030],rank2[303030],height[303030],c[303030],*x,*y,s[300030]; int n; void cmp(int n,int sz) { int i; memset(c,0,sizeof(c)); for(i=0;i<n;i++) c[x[y[i]]]++; for(i=1;i<sz;i++) c[i]+=c[i-1]; for(i=n-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i]; } void build_sa(int *s,int n,int sz) { x=Rank,y=rank2; int i,j; for(i=0;i<n;i++) x[i]=s[i],y[i]=i; cmp(n,sz); int len; for(len=1;len<n;len<<=1) { int yid=0; for(i=n-len;i<n;i++) { y[yid++]=i; } for(i=0;i<n;i++) if(sa[i]>=len) y[yid++]=sa[i]-len; cmp(n,sz); swap(x,y); x[sa[0]]=yid=0; for(i=1;i<n;i++) { if(y[sa[i-1]]==y[sa[i]]&&sa[i-1]+len<n&&sa[i]+len<n&&y[sa[i-1]+len]==y[sa[i]+len]) x[sa[i]]=yid; else x[sa[i]]=++yid; } sz=yid+1; if(sz>=n) break; } for(i=0;i<n;i++) Rank[i]=x[i]; } void getHeight(int *s,int n) { int k=0; for(int i=0;i<n;i++) { if(Rank[i]==0) continue; k=max(0,k-1); int j=sa[Rank[i]-1]; while(s[i+k]==s[j+k]) k++; height[Rank[i]]=k; } } int pos[303030]; int main() { int t,c=0; scanf("%d",&t); while(t--) { int n; scanf("%d",&n); scanf("%s",str); int i,j; int len=strlen(str); int num=27; for(i=0;i<len;i++) { s[i]=str[i]-'a'+1; } int m=len; s[m++]=num; for(i=1;i<=n;i++) { scanf("%s",str); int len=strlen(str); for(j=0;j<len;j++) { s[m++]=str[j]-'a'+1; } s[m++]=num+i; } s[m]=0; // printf("%d\n",m); build_sa(s,m+1,num+n+1); getHeight(s,m); memset(pos,0,sizeof(pos)); int temp=INF; for(i=1;i<=m;i++) { if(sa[i]<len) { if(height[i]<temp) temp=height[i]; if(pos[sa[i]]<temp) pos[sa[i]]=temp; } else temp=INF; } temp=INF; for(i=m;i>=1;i--) { if(sa[i-1]<len) { if(height[i]<temp) temp=height[i]; if(pos[sa[i-1]]<temp) pos[sa[i-1]]=temp; } else temp=INF; } for(i=1;i<=m;i++) { if(sa[i]<len&&sa[i-1]<len) { if(pos[sa[i-1]]<height[i]) { pos[sa[i-1]]=height[i]; } } } __int64 ans=(__int64)len*(len+1)/2;//没有转换64位wa了一次 // printf("======%I64d\n",ans); for(i=0;i<len;i++) { // printf("******%d\n",pos[i]); ans-=pos[i]; // printf("======%I64d\n",ans); } printf("Case %d: %I64d\n",++c,ans); } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
HDOJ 题目4416 Good Article Good sentence(后缀数组求a串子串在b串中不出现的种类数)
标签:
原文地址:http://blog.csdn.net/yu_ch_sh/article/details/48087229