Jong Hyok and String
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 364 Accepted Submission(s): 83
Problem Description
Jong Hyok loves strings. One day he gives a problem to his friend you. He writes down n strings Pi in front of you, and asks m questions. For i-th question, there is a string Qi. We called strange set(s) = {(i, j) | s occurs in Pi and j is the position of its last character in the current occurence}. And for ith question, you must answer the number of different strings t which satisfies strange set(Qi) = strange set(t) and t is a substring of at least one of the given n strings.
Input
First line contains T, a number of test cases.
For each test cases, there two numbers n, m and then there are n strings Pi and m strings Qj.(i = 1…n, j = 1…m)
1 <= T <= 10
1 <= n <= 100000
1 <= m<= 500000
1 <=|Pi|<=100000
1 <=|Qi|<=100000
∑ni=1|Pi|≤100000
File size is less than 3.5 megabytes.
For each test cases, there two numbers n, m and then there are n strings Pi and m strings Qj.(i = 1…n, j = 1…m)
1 <= T <= 10
1 <= n <= 100000
1 <= m<= 500000
1 <=|Pi|<=100000
1 <=|Qi|<=100000
∑ni=1|Pi|≤100000
File size is less than 3.5 megabytes.
Output
For each test case, first line contains a line “Case #x:”, x is the number of the case.
For each question, you should print one integer in one line.
For each question, you should print one integer in one line.
Sample Input
1 2 2 aba ab a ab
Sample Output
Case #1: 1 2Hintstrange set(“a”) ={(1, 1), (1, 3), (2, 1)}. strange set(“ab”) ={(1, 2), (2, 2)}. strange set(“b”) ={(1, 2), (2, 2)}.
Author
金策工业综合大学(DPRK)
解题思路:后缀数组将所给字符串反转后中间加上一个特殊字符后连接求其后缀数组。因为后缀数组是按照后缀的字典序递增的。因此可以将所给字符串反转后到在后缀数组中二分查找一个区间l~r使得在这个区间内的后缀都以反转后的q为前缀。t的种类数就为在这个区间中的公共前缀到不满足的字符位置所取的值。对于公共前缀的长度可用rmq求出。当l==r时就是从该后缀字符起到特殊字符的位置减去不能将q做为为前缀的最大值。
/* ***********************************************
Author : ryc
Created Time : 2016-08-19 Friday
File Name : E:\acm\hdoj\5853.cpp
Language : c++
Copyright 2016 ryc All Rights Reserved
************************************************ */
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<list>
#include<vector>
#include<map>
#include<stack>
using namespace std;
typedef long long LL;
typedef pair<int,int>pii;
const int maxn=1100010;
char s[maxn],str[maxn];
int dp[maxn][25];
int sa[maxn],t1[maxn],t2[maxn],c[maxn];
int Rank[maxn],height[maxn],r[maxn];
void getHeight(int n){
int k = 0;
for(int i=1;i<=n;i++)Rank[sa[i]] = i;
for(int i=0;i<n;i++){
if(k)k--;
int j = sa[Rank[i]-1];
while(r[i+k]==r[j+k])k++;
height[Rank[i]] = k;
}height[n+1]=0;
}
bool cmp(int *r,int a,int b,int l){
return (r[a]==r[b] && r[a+l]==r[b+l]);
}
void build_sa(int m,int n){
int i,*x=t1,*y=t2,k,p;m=150;
for(int i=0;i<=n;++i){
r[i]=s[i];if(s[i]=='#')r[i]=m++;
}
for( i=0;i<m;i++)c[i] = 0;
for( i=0;i<n;i++)c[x[i] = r[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(k=1,p=0;p<n;m=p,k<<=1){
p = 0;
for(i=n-k;i<n;i++)y[p++] = i;
for(i=0;i<n;i++)if(sa[i]>=k)y[p++] = sa[i]-k;
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],k)?p-1:p++;
}
getHeight(n-1);
}
void init(int N){
int i,j;
for(j=1;j<=log2(N+1);++j){
for(i=1;i<=N-(1<<j)+1;++i){
dp[i][j]=min(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
}
}
}
LL query(int l,int r){
if(l>r)return 0;
int k=log2(r-l+1);
return min(dp[l][k],dp[r-(1<<k)+1][k]);
}
int judge(int n,int len){
for(int i=0;i<len;++i){
if(str[i]<r[n+i])return 0;
else if(str[i]>r[n+i])return -1;
}
return 1;
}
int main()
{
int t,n,m,test=1;cin>>t;
while(t--){
int len=0,cnt=150;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;++i){
scanf("%s",str);
int l=strlen(str);
for(int j=l-1;j>=0;--j){
s[len]=str[j];len++;
}
s[len]='#';len++;
}len--;s[len]=0;
build_sa(255,len+1);
for(int i=1;i<=len;++i){
dp[i][0]=height[i];
}init(len);
printf("Case #%d:\n",test++);
while(m--){
scanf("%s",str);
int l=strlen(str);
reverse(str,str+l);
int left=1,right=len,lower=-1,upper=-1;
while(left<=right){
int mid=(left+right)>>1;
int oper=judge(sa[mid],l);
if(oper==-1){
left=mid+1;
}
else if(oper==0){
right=mid-1;
}
else if(oper==1){
lower=mid;right=mid-1;
}
}
if(lower==-1){
printf("0\n");continue;
}
left=1;right=len;
while(left<=right){
int mid=(left+right)>>1;
int oper=judge(sa[mid],l);
if(oper==-1){
left=mid+1;
}
else if(oper==0){
right=mid-1;
}
else if(oper==1){
upper=mid;left=mid+1;
}
}
if(upper==-1){//未找到输出0
printf("0\n");continue;
}
if(lower==upper){
int k;
for(k=sa[lower];k<len;++k){
if(s[k]=='#')break;
}//当lower==upper时就为该后缀所在字符串的长度-与当前字符串产生公共前缀的最大值。
printf("%d\n",k-sa[lower]-max(height[lower],height[upper+1]));
}
else {//用rmq求出区间内公共前缀的长度-不包含q的字符串与当前字符串产生的公共前缀的最大值
printf("%d\n",query(lower+1,upper)-max(height[lower],height[upper+1]));
}
}
}
return 0;
}