大意: 给定字符串$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 '\n'
#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;
}

Two strings CodeForces - 762C (字符串,双指针)的更多相关文章

  1. Dreamoon and Strings CodeForces - 477C (字符串dp)

    大意: 给定字符串$s$, $p$, 对于$0\le x\le |s|$, 求$s$删除$x$个字符后, $p$在$s$中的最大出现次数. 显然答案是先递增后递减的, 那么问题就转化求最大出现次数为$ ...

  2. golang学习笔记15 golang用strings.Split切割字符串

    golang用strings.Split切割字符串 kv := strings.Split(authString, " ") if len(kv) != 2 || kv[0] != ...

  3. [LeetCode] 583. Delete Operation for Two Strings 两个字符串的删除操作

    Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 t ...

  4. [LeetCode] 712. Minimum ASCII Delete Sum for Two Strings 两个字符串的最小ASCII删除和

    Given two strings s1, s2, find the lowest ASCII sum of deleted characters to make two strings equal. ...

  5. Codeforces 762C Two strings 字符串

    Cpdeforces 762C 题目大意: 给定两个字符串a,b\((len \leq 10^5)\),让你去b中的一个连续的字段,使剩余的b串中的拼接起来的两个串是a穿的子序列.最大化这个字串的长度 ...

  6. [LeetCode] Encode and Decode Strings 加码解码字符串

    Design an algorithm to encode a list of strings to a string. The encoded string is then sent over th ...

  7. [LeetCode] Minimum ASCII Delete Sum for Two Strings 两个字符串的最小ASCII删除和

    Given two strings s1, s2, find the lowest ASCII sum of deleted characters to make two strings equal. ...

  8. [LeetCode] Delete Operation for Two Strings 两个字符串的删除操作

    Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 t ...

  9. [LeetCode] Split Concatenated Strings 分割串联字符串

    Given a list of strings, you could concatenate these strings together into a loop, where for each st ...

随机推荐

  1. [CSP-S模拟测试]:循环依赖(拓扑)

    题目传送门(内部题148) 输入格式 每个测试点第一行为一个正整数$T$,表示该测试点内的数据组数. 接下来$T$组数据,每组数据第一行一个正整数$n$,表示有引用单元格进行计算的单元格数,接下来$n ...

  2. jvisualvm安装Visual GC插件

    jdk自带了查看和分析jvm的一系列工具,在%JAVA_HOME%/bin目录下,包括jvisualvm.jconsole.jmap.jstack.jstat等: 其中jvisualvm.exe提供一 ...

  3. PHP 分页 (分页类)

    分页类  以后可直接使用 /** file: page.class.php 完美分页类 Page */ class Page { private $total; //数据表中总记录数 private ...

  4. js设计模式-代理模式

    1.什么是设计模式? 设计模式:在软件设计过程中常用的代码规范,针对特定的场景 2.应用场景: 麦当劳点餐  观察者模式   规定的代码格式 花店送花  :代理模式 真实对象(男同学)-----代理对 ...

  5. Orcal数据类型总结

    一.Oracle中的varchar2类型 我们在Oracle数据库存储的字符数据一般是用VARCHAR2.VARCHAR2既分PL/SQL Data Types中的变量类型,也分Oracle Data ...

  6. C++学习 之 初识C++

    声明:            本人自学C++, 没有计算机基础,在学习的过程难免会出现理解错误,出现风马牛不相及的现象,甚至有可能会贻笑大方. 如果有幸C++大牛能够扫到本人的博客,诚心希望大牛能给予 ...

  7. linux性能分析之平均负载

    平均负载 1,执行 top 或者 uptime 命令 来了解系统负载 uptime 分析显示 当前时间,系统运行时间,正在登录用户数 平均负载是指单位时间内,系统处于可运行状态和不可中断状态的平均进程 ...

  8. [Python]python-jenkins获取正在构建中的job

    需求: 我现在需要完成1个接口,这个接口会启动jenkins构建jobA, jobA构建结束, 返回job的构建结果 思路: 首先使用get_job_info获取最后1次构建的构建序号,然后再通过ge ...

  9. ArrayList去除集合中字符串的重复值

    package com.heima.list; import java.util.ArrayList; import java.util.Iterator; import java.util.List ...

  10. OpenStack 节点重启后无法联网的问题

    问题 五一归来,大量服务器被重启了,其中一台服务器失联了.通过远程桌面登录之后发现服务器的 IP 地址是 "正常" 的,可以 Ping 通自己的 IP,但 Ping 不同网关. 解 ...