bzoj4032 / luoguP4112 [HEOI2015] common string is not the shortest (dp + a suffix automaton automata sequence)

bzoj Luogu

Problem solution time

A two-letter lowercase string $ A $, $ B $, please Calculated:

(1) $ A $ a shortest substring, it is not $ B $ substring

(2) $ A $ substring of a shortest, it is not a sequence $ B $

(3) $ A $ sequence is the shortest, it is not $ B $ substring

(4) $ A $ sequence is the shortest, it is not a sequence $ B $

Four water issues, one question more than four questions sao

First, as in all from $ A $ to find, so follow routine is to match the $ A $ $ B $

Therefore, first construct the SAM and $ B $ sequence automaton.

Automaton sequence: is a $ next [i] [ch] $ $ I $ array represents the next bit of the first character position of occurrence $ $ CH, purely for matching.

(1) directly to the $ A $ violence on every match, starting at $ B $ of SAM, as long as the mismatch will update the answer and then jump out.

(2) was changed in sequence automaton.

(3) $ dp [i] [j] $ denotes sequences $ A $ front $ I $ characters formed to match in the $ B $ of SAM to $ J $ position, the shortest sequence length thus formed, this is a very classic dp up.

(4) was changed in sequence automaton.

#include<bits/stdc++.h>
using namespace std;
namespace RKK
{
const int N=2011,inf=0x3f3f3f3f;
int n1;char s1[N];
int n2;char s2[N];
struct remilia{int tranc[26],len,pre;};
struct sakuya
{
    remilia s[N<<1];
    int fin,size;
    sakuya(){fin=size=1;}
    void ins(int ch)
    {
        int npx,npy,lpx,lpy;
        npx=++size;
        s[npx].len=s[fin].len+1;
        for(lpx=fin;lpx&&!s[lpx].tranc[ch];lpx=s[lpx].pre) s[lpx].tranc[ch]=npx;
        if(!lpx) s[npx].pre=1;
        else
        {
            lpy=s[lpx].tranc[ch];
            if(s[lpy].len==s[lpx].len+1) s[npx].pre=lpy;
            else
            {
                npy=++size;
                s[npy]=s[lpy];
                s[npy].len=s[lpx].len+1;
                s[lpy].pre=s[npx].pre=npy;
                while(s[lpx].tranc[ch]==lpy)
                {
                    s[lpx].tranc[ch]=npy;
                    lpx=s[lpx].pre;
                }
            }
        }
        fin=npx;
    }
    void insert(char *str,int sl)
    {
        for(int i=1;i<=sl;i++)
            ins(str[i]-'a');
    }
}sam;
struct flandre
{
    int ne[N][26],tmp[26];
    void insert(char *s,int sl)
    {
        for(int i=sl;i>=0;i--)
            memcpy(ne[i],tmp,104),tmp[s[i]-'a']=i;
    }
}sem;
void solve1()
{
    int ans=inf;
    for(int sp=1;sp<=n1;sp++)
    {
        int px=1;
        for(int i=sp;i<=n1;i++)
        {
            if(!sam.s[px].tranc[s1[i]-'a']){ans=min(ans,i-sp+1);break;}
            px=sam.s[px].tranc[s1[i]-'a'];
        }
    }
    printf("%d\n",ans==inf?-1:ans);
}
void solve2()
{
    int ans=inf;
    for(int sp=1;sp<=n1;sp++)
    {
        int px=0;
        for(int i=sp;i<=n1;i++)
        {
            if(!sem.ne[px][s1[i]-'a']){ans=min(ans,i-sp+1);break;}
            px=sem.ne[px][s1[i]-'a'];
        }
    }
    printf("%d\n",ans==inf?-1:ans);
}
int dp[N<<1];
void solve3()
{
    int ans=inf;
    memset(dp,0x3f,sizeof(dp));
    dp[1]=0;
    for(int i=1;i<=n1;i++)
    {
        for(int x=sam.size;x;x--)
        {
            if(sam.s[x].tranc[s1[i]-'a']) dp[sam.s[x].tranc[s1[i]-'a']]=min(dp[sam.s[x].tranc[s1[i]-'a']],dp[x]+1);
            else ans=min(ans,dp[x]+1);
        }
    }
    printf("%d\n",ans==inf?-1:ans);
}
void solve4()
{
    int ans=inf;
    memset(dp,0x3f,sizeof(dp));
    dp[0]=0;
    for(int i=1;i<=n1;i++)
    {
        for(int x=n2;x>=0;x--)
        {
            if(!sem.ne[x][s1[i]-'a']) ans=min(ans,dp[x]+1);
            else dp[sem.ne[x][s1[i]-'a']]=min(dp[sem.ne[x][s1[i]-'a']],dp[x]+1);
        }
    }
    printf("%d\n",ans==inf?-1:ans);
}
int Iris()
{
    scanf("%s%s",s1+1,s2+1),n1=strlen(s1+1),n2=strlen(s2+1);
    sam.insert(s2,n2),sem.insert(s2,n2);
    solve1(),solve2(),solve3(),solve4();
    return 0;
}
}
int main(){return RKK::Iris();}

Guess you like

Origin www.cnblogs.com/rikurika/p/12079153.html