hdu 4691后缀数组+RMQ----子字符串的最长公共前缀


Description

Front compression is a type of delta encoding compression algorithm whereby common prefixes and their lengths are recorded so that they need not be duplicated. For example: 

The size of the input is 43 bytes, while the size of the compressed output is  40. Here, every space and newline is also counted as 1 byte. 
Given the input, each line of which is a substring of a long string, what are sizes of it and corresponding compressed output?

Input

There are multiple test cases. Process to the End of File. 
The first line of each test case is a long string S made up of lowercase letters, whose length doesn't exceed 100,000. The second line contains a integer 1 ≤ N ≤ 100,000, which is the number of lines in the input. Each of the following N lines contains two integers 0 ≤ A < B ≤ length(S), indicating that that line of the input is substring [A, B) of S.

Output

For each test case, output the sizes of the input and corresponding compressed output.

Sample Input

frcode
2
0 6
0 6
unitedstatesofamerica
3
0 6
0 12
0 21
myxophytamyxopodnabnabbednabbingnabit
6
0 9
9 16
16 19
19 25
25 32
32 37

Sample Output

14 12
42 31
43 40


当需要寻找公共长度的字符串全部为str字符串的子串时,计算strheight数组,for(int i=0;i<=len-1;i++)

            r[i]=str[i]-'a'+1;

        r[len]=0;   n=len;

然后用rmqheight[1~n]数组放入dp[][](此时为min)

对于两个字符串a,b,它们的首字母在str中的位置为(0~len-1xy,则它们的排名为rank[x]rank[y],(根据大小)计算[rank[x]+1,rank[y]]或者[rank[y],rank[x]+1]区间的height的最小值,即为以xy为首字母到最后,公共前缀的长度,由于ab不是完整的字符串,所以答案为min(lcp(x,y),min(len(a),len(b)));


#include
#include
#include
#include
#define ll long long
#include
using namespace std;
const int MAX=100020;
int wa[MAX],wb[MAX],wsf[MAX],wv[MAX],sa[MAX];
int rankx[MAX],height[MAX],r[MAX];
int dp[MAX][20];
int n;
int cmp(int *r,int a,int b,int k)
{
    return (r[a]==r[b])&&(r[a+k]==r[b+k]);
}
void da(int *r,int *sa,int n,int m)//此处N比输入的N要多1,为人工添加的一个字符,用于避免cmp时越界
{
    int i,j,p,*x=wa,*y=wb,*t;
    for(i=0;i=0;i--) sa[--wsf[x[i]]]=i;
    for(j=1,p=1;p=j) y[p++]=sa[i]-j;
        for(i=0;i=0;i--) sa[--wsf[wv[i]]]=y[i];
        for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1;i>str)
    {
        int len=str.length();
        for(int i=0;i<=len-1;i++)
            r[i]=str[i]-'a'+1;
        r[len]=0;
        n=len;
        da(r,sa,n+1,30);
        calheight(r,sa,n);
        RMQ_ST();
        ll len1=0,len2=0;
        int t;
        cin>>t;
        int a,b;
        cin>>a>>b;
        len1+=b-a+1;
        len2+=3+b-a;
        t--;
        int u,v;
        while(t--)
        {
            scanf("%d%d",&u,&v);
            len1+=v-u+1;
            int temp=min(lcp(a,u),min(v-u,b-a));
            len2+=v-u-temp+2+change(temp);
            a=u,b=v;
        }
        cout<



你可能感兴趣的:(字符串类问题)