zoukankan      html  css  js  c++  java
  • Two strings CodeForces

    大意: 给定字符串$a$,$b$, $b$可以任选一段连续的区间删除, 要求最后$b$是$a$的子序列, 求最少删除区间长度.

    删除一段连续区间, 那么剩余的一定是一段前缀和后缀. 判断是否是子序列可以用序列自动机, 最后双指针合并前缀与后缀的答案.

    #include <iostream>
    #include <sstream>
    #include <algorithm>
    #include <cstdio>
    #include <math.h>
    #include <set>
    #include <map>
    #include <queue>
    #include <string>
    #include <string.h>
    #include <bitset>
    #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 '
    '
    #define DB(a) ({REP(__i,1,m) 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
    
    
    
    #ifdef ONLINE_JUDGE
    const int N = 1e6+10;
    #else
    const int N = 111;
    #endif
    
    char s[N], p[N];
    int n, m, nxt[26][N], pre[26][N];
    int L[N], R[N];
    
    int main() {
    	scanf("%s%s", s+1,p+1);
    	n = strlen(s+1);
    	m = strlen(p+1);
    	REP(i,1,n) s[i]-='a';
    	REP(i,1,m) p[i]-='a';
    	REP(c,0,25) {
    		nxt[c][n+1]=nxt[c][n+2]=n+1;
    		PER(i,1,n) {
    			if (s[i]==c) nxt[c][i]=i;
    			else nxt[c][i]=nxt[c][i+1];
    		}
    		REP(i,1,n) {
    			if (s[i]==c) pre[c][i]=i;
    			else pre[c][i]=pre[c][i-1];
    		}
    	}
    	L[1] = nxt[p[1]][1];
    	REP(i,2,m) L[i] = nxt[p[i]][L[i-1]+1];
    	R[m] = pre[p[m]][n];
    	PER(i,1,m-1) R[i] = pre[p[i]][R[i+1]-1];
    	int ans = 0, posR = 0, posL = 0;
    	PER(i,1,m) if (R[i]) ans = m-i+1, posR = i;
    	REP(i,1,m) p[i]+='a';
    	if (ans==m) return puts(p+1),0;
    	R[m+1] = INF;
    	int now = posR;
    	REP(i,1,m) {
    		while (R[now]<=L[i]) ++now;
    		if (L[i]==n+1) break;
    		if (ans<i+m-now+1) {
    			ans = i+m-now+1;
    			posL = i, posR = now;
    		}
    	}
    	if (!ans) return puts("-"),0;
    	REP(i,1,posL) putchar(p[i]);
    	REP(i,posR,m) putchar(p[i]);hr;
    }
    
  • 相关阅读:
    luoguP3822 [NOI2017]整数
    luoguP2150 [NOI2015]寿司晚宴
    luoguP3868 [TJOI2009]猜数字
    luoguP4777 【模板】扩展中国剩余定理(EXCRT)
    luoguP2048 超级钢琴
    题解 P1004 【方格取数】
    戊戌年西安游记
    题解 P4388 【付公主的矩形】
    题解 P4277 【河城荷取的烟花】
    001 dynamic Linq
  • 原文地址:https://www.cnblogs.com/uid001/p/10826409.html
Copyright © 2011-2022 走看看