Cf 119d string Transformation (KMP, hash, enumeration, various optimizations)

Source: Internet
Author: User

Reprint please indicate the source, thank you http://blog.csdn.net/ACM_cxlove? Viewmode = Contents
By --- cxlove

Question: There are two strings, S, FF(S, Bytes,I, Bytes,J) Bytes = bytesS[ILifecycle + lifecycle 1...JAudio-extract 1] Audio + ExtractR(S[J...NExecutor-cores 1]) buffers + BuffersR(S[0...I]).

The original string S is transformed through a binary group (I, j) to get a new string. Now you need to find this binary group.

Http://codeforces.com/problemset/problem/119/D

Haha, cool, finally

The first time debugging on CF was so difficult, TLE + wa had a lot of clicks

We represent two strings as a and B, which are divided into three parts.

A (1, 2, 3) a' indicates the reverse order of

Then B (2, 3 ', 1') B 'indicates the reverse order of B.

In my practice, the first step is to enumerate I. First, we can see the longest public prefix of A and B.

Because the question requires the largest I, first obtain the longest public prefix length of A and B ', mx + 1

Then I can enumerate from MX.

Next, it is equivalent to enumerating J, but directly enumerating it will certainly

It can be found that the suffix of B is the prefix of a' except 1.

Then we use a' and B to perform a KMP to record the longest-range POs that can be matched for a location in B []

But not necessarily the farther J is, so during enumeration, starting from POS [], we can enumerate the possible J through the next [] array.

In this way, it must satisfy the 1, 3 parts.

The rest is to determine whether the remaining part of the two strings is matched. This part can be processed by O (1) Through hash.

But it will still be TLE, because we can know that like aaaaaaaaaaaBAaaaaaaaa strings are very slow to be transferred through the next array. Or is it equivalent to enumeration?

Here, I added an optimization. I ran KMP directly with string B and string a to record the longest matching length of DP [] for a position in string a and string a.

After enumerating I, enumerative J can be used for one pruning.

However, it has been wa for a long time. This is because when processing the DP [] array, I transferred the DP [] array while moving the matching forward. In fact, it is not correct. Even if they do not match, you can use the next [] array to obtain that the prefix of the suffix matches the prefix and can also be transferred.

The above ideas are purely immature and unclear.

Welcome to the discussion. Check the code.

#include<iostream>  #include<cstdio>  #include<map>  #include<cstring>  #include<cmath>  #include<vector>  #include<algorithm>  #include<set>  #include<string>  #include<queue>  #define inf 1600005  #define M 40  #define N 1000005#define maxn 300005  #define eps 1e-12#define zero(a) fabs(a)<eps  #define Min(a,b) ((a)<(b)?(a):(b))  #define Max(a,b) ((a)>(b)?(a):(b))  #define pb(a) push_back(a)  #define mp(a,b) make_pair(a,b)  #define mem(a,b) memset(a,b,sizeof(a))  #define LL long long  #define MOD 1000000007#define lson step<<1#define rson step<<1|1#define sqr(a) ((a)*(a))  #define Key_value ch[ch[root][1]][0]  #define test puts("OK");  #define pi acos(-1.0)#define lowbit(x) ((-(x))&(x))#define HASH1 1331#define HASH2 10001#pragma comment(linker, "/STACK:1024000000,1024000000")  using namespace std;char a[N],a_2[N],b[N];int la,lb;int next[N];int pos[N];int dp[N];LL fac1[N]={1},fac2[N]={1};LL h11[N]={0},h21[N]={0};LL h12[N]={0},h22[N]={0};void get_next(char *s,int len){    next[0]=-1;    int i=0,j=-1;    while(i<len){        if(j==-1||s[i]==s[j]){            i++;j++;            next[i]=j;        }        else j=next[j];    }}void match(char *pat,int lp,char *str,int ls,int flag){    int i=0,j=0;    while(i<lp&&j<ls){        if(i==-1||pat[i]==str[j]){            if(!flag) pos[j]=i;            if(flag&&i!=-1){                dp[j-i]=max(dp[j-i],i+1);            }            i++;j++;        }        else{            i=next[i];            dp[j-i]=max(dp[j-i],i+1);        }        if(i==lp) i=next[i];    }}LL get(int l,int r,LL *hash,LL *fac){    l++;r++;    return hash[r]-hash[l-1]*fac[r-l+1];}int main(){    //freopen("input.txt","r",stdin);    for(int i=1;i<N;i++){        fac1[i]=fac1[i-1]*HASH1;        fac2[i]=fac2[i-1]*HASH2;    }    while(gets(a)!=NULL&&gets(b)!=NULL){        la=strlen(a);lb=strlen(b);        if(la!=lb){            //if(a[0]==a[1]&&a[2]==a[3]&&a[2]==' ') cout<<3333333333<<endl;            printf("-1 -1\n");            continue;        }        for(int i=0;i<la;i++)            a_2[i]=a[la-i-1];        for(int i=1;i<=la;i++){            h11[i]=h11[i-1]*HASH1+a[i-1];            h12[i]=h12[i-1]*HASH2+a[i-1];        }        for(int i=1;i<=lb;i++){            h21[i]=h21[i-1]*HASH1+b[i-1];            h22[i]=h22[i-1]*HASH2+b[i-1];        }        get_next(a_2,la);        match(a_2,la,b,lb,0);        mem(dp,-1);        get_next(b,lb);        match(b,lb,a,la,1);        int l=-1,r=-1,mx=-1;        for(int i=0;i<la;i++)            if(a[i]==b[lb-i-1]){                mx=i;            }            else break;        for(int i=min(la-2,mx);i>=0;i--){            int p=pos[lb-i-2];            while(p>=0){                int j=la-p-1;                if(j-i-1>dp[i+1]&&j-i-1!=0) break;                if(i+1==j){                    l=i,r=j;                }                else{                    LL t1=get(i+1,j-1,h11,fac1),t2=get(0,j-i-2,h21,fac1);                    LL t3=get(i+1,j-1,h12,fac2),t4=get(0,j-i-2,h22,fac2);                    if(t1==t2&&t3==t4) l=i,r=j;                }                p=next[p];                if(l!=-1) break;            }            if(l!=-1) break;        }        printf("%d %d\n",l,r);    }    return 0;}





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.