[2019.8.14 Cixi simulation game T1] I am not! I do not have! Bite your tongue ah! (Notme) (BFS + DP)

\ (IDA ^ * \)

To be honest, I started this question thought out positive solutions, then wrote a \ (IDA ^ * \) . . .

But the magic is that this \ (IDA ^ * \) not even the length of the string are \ (2500,4000 \) data is run fast,But after the data sent down a bit I measured only 45 points.

Just constantly optimize \ (IDA ^ * \) in the process, I suddenly came up with the practice of positive solutions,It seems after failing to decide the first violence.

\ (DP \) to solve the first query

Consider a \ (the DP \) , we set \ (f_ {i, j} \) indicates that the current is the first in the first string \ (I \) bits of the second string is \ (J \ ) the minimum number of steps bits.

When the recording \ (nxt1_ {x, 0/ 1}, nxt2_ {x, 0/1} \) represent two strings (X \) \ a lower rear position \ (0/1 \) where they appear, then we can get this shift:

\[f_{nxt1_{i,0},nxt2_{j,0}}=min(f_{nxt1_{i,0},nxt2_{j,0}},f_{i,j})\]

\[f_{nxt1_{i,1},nxt2_{j,1}}=min(f_{nxt1_{i,1},nxt2_{j,1}},f_{i,j})\]

This would address the first asked.

\ (BFS \) to solve the second inquiry

If we consider \ (DP \) when recording a \ (lst \) indicates the position of the transfer to, you can program the output.

But the problem requires the lexicographically smallest, Common \ (the DP \) or \ (the DFS \) in the form of \ (the DP \) can not meet this condition.

So we can think of \ (BFS \) .

According to (BFS \) \ be the order of \ (DP \) , we can guarantee the minimum conditions lexicographically inevitable satisfied.

Code

#include<bits/stdc++.h>
#define Tp template<typename Ty>
#define Ts template<typename Ty,typename... Ar>
#define Reg register
#define RI Reg int
#define Con const
#define CI Con int&
#define I inline
#define W while
#define N 4000
using namespace std;
int n,m;string s1,s2;
class DpSolver//BFS+DP
{
    private:
        string ans;short qx[(N+2)*(N+2)+5],qy[(N+2)*(N+2)+5],nxt1[N+5][2],nxt2[N+5][2];
        short f[N+5][N+5],gx[N+5][N+5],gy[N+5][N+5];bool glst[N+5][N+5];
    public:
        I void Solve()
        {
            RI i,j,x,y,H=1,T=0,p[2];s1="%"+s1,s2="%"+s2;
            for(p[0]=p[1]=n+1,i=n+1;~i;--i) nxt1[i][0]=p[0],nxt1[i][1]=p[1],p[s1[i]&1]=i;//初始化nxt1
            for(p[0]=p[1]=m+1,i=m+1;~i;--i) nxt2[i][0]=p[0],nxt2[i][1]=p[1],p[s2[i]&1]=i;//初始化nxt2
            for(i=0;i<=n+1;++i) for(j=0;j<=m+1;++j) f[i][j]=m+1;f[0][0]=0,qx[++T]=0,qy[T]=0;//初始化f数组和BFS队列
            W(H<=T) i=qx[H],j=qy[H++],//取出队首
                f[x=nxt1[i][0]][y=nxt2[j][0]]==m+1&&(qx[++T]=x,qy[T]=y),//未访问过就入队
                f[i][j]+1<f[x][y]&&(f[x][y]=f[i][j]+1,gx[x][y]=i,gy[x][y]=j,glst[x][y]=0),//更新f和g
                f[x=nxt1[i][1]][y=nxt2[j][1]]==m+1&&(qx[++T]=x,qy[T]=y),//未访问过就入队
                f[i][j]+1<f[x][y]&&(f[x][y]=f[i][j]+1,gx[x][y]=i,gy[x][y]=j,glst[x][y]=1);//更新f和g
            x=n+1,y=m+1;W(x||y) ans=(char)(glst[x][y]+48)+ans,i=gx[x][y],j=gy[x][y],x=i,y=j;//倒着找答案
            cout<<ans<<endl;//输出答案
        }
}D;
int main()
{
    freopen("notme.in","r",stdin),freopen("notme.out","w",stdout);
    cin>>s1>>s2,s1.length()>s2.length()&&(swap(s1,s2),0),n=s1.length(),m=s2.length();
    return D.Solve(),0;
}

Guess you like

Origin www.cnblogs.com/chenxiaoran666/p/Contest20190814T1.html