HDU5853 Jong Hyok and String (binary + suffix array)

Source: Internet
Author: User

TopicsSource

http://acm.hdu.edu.cn/showproblem.php?pid=5853

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 are 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 the numbers n, m and then there is 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 on one line.

Sample Input

1
2 2
Aba
Ab
A
Ab

Sample Output

Case #1:
1
2

Analysis

The topic probably says to a number of string pi, and then several queries, asking how many different substrings within the Pi are the same as the strange set for the given query string. The strange set of a string is a collection of two tuples (I,J) that represents the position of the string in pi and the last character in the Pi J.

The game was a long discussion with his teammates.

The first thought is that the strange set of the query string must be the same as the suffix of the query string (in fact, more than this =). The query string suffix of the strange set is not the same as the query string is the suffix in pi is matched, but in that position query string is not matched.

The teammate then converts the suffix into a prefix by turning the string upside down.

Next, I see σ|pi|<=100000, so we begin to think of the array above the suffix. Naturally, those pi should be reversed (at this point the prefix is considered), then stitched together, in the middle with special characters separated.

After each suffix is sorted, it is possible to find the matching position by two points for any pattern string. Then we begin to consider the various prefixes of the query string, to find out how many of them match the upper and lower bounds of their position, and then compare the number of matches to the query string, if it is possible to specify the prefix as equal.

But the complexity of the time is obviously not. Later I thought if the prefix x does not work, then the prefix x-1 must not, and then slowly came to this conclusion--

    • For each query string, two times to find the upper bound upp and lower bound low (upp<=low). ), then the result is | query string |-max (LCP (Upp,upp-1), LCP (low,low+1))!

We verify the time complexity, is the total length of all query string *σ|pi|*logσ|pi|, all query string length clarification said about 200W, so probably can be a try. So wrote, but wa= = two points changed, and then what what. There was no end to the game.

In fact, the first logic is a loophole. "The same as the strange set of the query string must be the suffix of the query string (in fact more than this =)", there is a situation!

For example, this data:

1 1
Bbbaa
BBA
The result should be 3, because:
    • Strange Set ("bba") = {(1,4)}
    • These two suffixes of the BBA satisfy:Strange Set ("bba") = {(1,4)},Strange Set ("ba") = {(1,4)}
    • Also there is this satisfaction:Strange Set ("Bbba") = {(1,4)}
That is, there is a possible satisfaction that contains the entire string. Then I thought about it, drew the picture, and came to the conclusion:
    • The number of such cases is the length of the LCP in the upper and lower bounds minus the query string!
Also note that the upper bound = nether, and special characters should be different here. Feel the problem is very difficult to describe = = That's it. Finally, I finally AC. code
#include <cstdio> #include <cstring> #include <cmath> #include <algorithm>using namespace std;# Define INF (1<<30) #define MAXN 222222int wa[maxn],wb[maxn],wv[maxn],ws[maxn];int cmp (int *r,int a,int b,int l) {R Eturn R[a]==r[b] && r[a+l]==r[b+l];}    int sa[maxn],rnk[maxn],height[maxn];void SA (int *r,int n,int m) {int *x=wa,*y=wb;    for (int i=0; i<m; ++i) ws[i]=0;    for (int i=0; i<n; ++i) ++ws[x[i]=r[i];    for (int i=1; i<m; ++i) ws[i]+=ws[i-1];    for (int i=n-1; i>=0;-I.) sa[--ws[x[i]]]=i;    int p=1;        for (int j=1; p<n; j<<=1,m=p) {p=0;        for (int i=n-j; i<n; ++i) y[p++]=i;        for (int i=0; i<n; ++i) if (sa[i]>=j) y[p++]=sa[i]-j;        for (int i=0; i<n; ++i) wv[i]=x[y[i];        for (int i=0; i<m; ++i) ws[i]=0;        for (int i=0; i<n; ++i) ++ws[wv[i];        for (int i=1; i<m; ++i) ws[i]+=ws[i-1];        for (int i=n-1; i>=0;-I.) sa[--ws[wv[i]]]=y[i]; Swap (x, y); x[sa[0]]=0;     P=1;   for (int i=1; i<n; ++i) x[sa[i]]=cmp (y,sa[i-1],sa[i],j)? p-1:p++;    } for (int i=1; i<n; ++i) rnk[sa[i]]=i;    int k=0;        for (int i=0; i<n-1; height[rnk[i++]]=k) {if (k)--k;    for (int j=sa[rnk[i]-1]; r[i+k]==r[j+k]; ++k);    }}int st[18][maxn];void St (int *a,int N) {for (int i=1; i<=n; ++i) st[0][i]=a[i];            for (int i=1, i<18; ++i) {for (Int. j=1; j<=n; ++j) {if (j+ (1<<i) >n) break;        St[i][j]=min (st[i-1][j],st[i-1][j+ (1<<i-1));    }}} int rmq (int a,int b) {if (a>b) swap (A, b);    int k= (int) (LOG2 (b-a+1) +1e-6); return min (st[k][a],st[k][b-(1<<k) +1]);}    Char str[maxn];int an,a[maxn],b[maxn],bn;int len[maxn];int cmp (int k) {int i;        for (i=0; I+k<an && i<bn; ++i) {if (A[i+k]>b[i]) return 1;    else if (a[i+k]<b[i]) return-1;    } if (i!=bn) return-1; return 0;}    int main () {int t,n,m;    scanf ("%d", &t); for (int cse=1; cse<=t; ++cse) {scanF ("%d%d", &n,&m);        an=0;            for (int i=0; i<n; ++i) {scanf ("%s", str);                for (int j=strlen (str)-1; j>=0;--j) {len[an]=j+1;            a[an++]=str[j]-' a ' +1;        } a[an++]=28+i;        } a[an++]=0;        SA (A,an,28+n);        ST (height,an-1);        printf ("Case #%d:\n", CSE);            while (m--) {scanf ("%s", str);            bn=0;            for (int j=strlen (str)-1; j>=0;--j) {b[bn++]=str[j]-' a ' +1;            } int l=1,r=an-1;            int upp=-1;                while (l<=r) {int mid=l+r>>1;                int tmp=cmp (Sa[mid]);                    if (tmp==0) {upp=mid;                R=mid-1;                }else if (tmp>0) r=mid-1;            else if (tmp<0) l=mid+1;                } if (upp==-1) {printf ("%d\n", 0);            Continue            } l=1,r=an-1;      int low=-1;      while (l<=r) {int mid=l+r>>1;                int tmp=cmp (Sa[mid]);                    if (tmp==0) {low=mid;                l=mid+1;                }else if (tmp>0) r=mid-1;            else if (tmp<0) l=mid+1;            } int tmp=0;            if (upp!=1) {Tmp=max (Tmp,height[upp]);            } if (low!=an-1) {Tmp=max (tmp,height[low+1]);            } if (Upp==low) printf ("%d\n", bn-tmp+len[sa[upp]]-bn);        else printf ("%d\n", Bn-tmp+rmq (Upp+1,low)-bn); }} return 0;}

HDU5853 Jong Hyok and String (binary + suffix array)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.