其他分享
首页 > 其他分享> > Liar CodeForces - 822E (dp,后缀数组)

Liar CodeForces - 822E (dp,后缀数组)

作者:互联网

大意: 给定串$s,t$, 给定整数$x$, 求判断$t$是否能划分为至多$x$段, 使这些段在$s$中按顺序,不交叉的出现.

 

设$dp_{i,j}$表示$s$匹配到$i$位, 划分了$j$段, 匹配到$t$中的最大位置

每次取一个极长的lcp转移即可, lcp可以二分哈希或者用后缀数组+RMQ求 

#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <cstring>
#include <bitset>
#include <functional>
#include <random>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<',';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head



const int N = 1e6+50;
int n,m,x;
char s[N],t[N];
int dp[N][40];
void chkmax(int &a, int b) {a<b?a=b:0;}

int Log[N],f[20][N];
void init(int a[N],int n) {
    Log[0] = -1;
    REP(i,1,n) f[0][i] = a[i], Log[i]=Log[i>>1]+1;
    REP(j,1,19) for (int i=0;i+(1<<j-1)-1<=n; ++i) {
        f[j][i] = min(f[j-1][i],f[j-1][i+(1<<j-1)]);
    }
}
int RMQ(int l, int r) {
    int t = Log[r-l+1];
    return min(f[t][l],f[t][r-(1<<t)+1]);
}

int c[N],rk[N],h[N],sa[N];
void build(int *a, int n, int m) {
    a[n+1] = rk[n+1] = h[n+1] = 0;
    int i,*x=rk,*y=h;
    for(i=1;i<=m;i++) c[i]=0;
    for(i=1;i<=n;i++) c[x[i]=a[i]]++;
    for(i=1;i<=m;i++) c[i]+=c[i-1];
    for(i=n;i;i--) sa[c[x[i]]--]=i;
    for(int k=1,p;k<=n;k<<=1) {
        p=0;
        for(i=n-k+1;i<=n;i++) y[++p]=i;
        for(i=1;i<=n;i++) if(sa[i]>k) y[++p]=sa[i]-k;
        for(i=1;i<=m;i++) c[i]=0;
        for(i=1;i<=n;i++) c[x[y[i]]]++;
        for(i=1;i<=m;i++) c[i]+=c[i-1];
        for(i=n;i;i--) sa[c[x[y[i]]]--]=y[i];
        swap(x,y); x[sa[1]]=1; p=1;
        for(i=2;i<=n;i++)
            x[sa[i]]=(y[sa[i-1]]==y[sa[i]] && y[sa[i-1]+k]==y[sa[i]+k])?p:++p;
        if(p==n) break; m=p;
    }
    for(i=1;i<=n;i++) rk[sa[i]]=i;
    for(int i=1,j,k=0;i<=n;i++) {
        if(k) k--;
        j=sa[rk[i]-1];
        while(a[i+k]==a[j+k]) k++;
        h[rk[i]] = k;
    }
}

int lcp(int x, int y) {
	x = rk[x], y = rk[y];
	if (x>y) swap(x,y);
	return RMQ(x+1,y);
}

int a[N];
int main() {
	scanf("%d%s%d%s%d",&n,s+1,&m,t+1,&x);
	REP(i,1,n) a[i]=s[i]-'a'+1;
	a[n+1]=30;
	REP(i,n+2,n+m+1) a[i]=t[i-n-1]-'a'+1;
	build(a,n+m+1,100);
	init(h,n+m+1);
	//dp[i][j] = s的前i位,t中分j段的最长匹配位置
	//dp[i][j] <- dp[i-1][j]
	//dp[i+lcp(i,dp[i-1][j]+1)-1][j+1] <- dp[i-1][j]
	REP(i,1,n) REP(j,0,x) {
		int &r = dp[i-1][j];
		chkmax(dp[i][j],r);
		if (r!=m) { 
			int t = lcp(i,n+r+2);
			chkmax(dp[i+t-1][j+1],r+t);
		}
	}
	REP(i,0,x) if (dp[n][i]==m) return puts("YES"),0;
	puts("NO");
}

 

标签:Liar,int,ll,CodeForces,822E,return,include,REP,define
来源: https://www.cnblogs.com/uid001/p/11614840.html