Two strings CodeForces - 762C (字符串,双指针)
大意: 给定字符串$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 (字符串,双指针)的更多相关文章
- Dreamoon and Strings CodeForces - 477C (字符串dp)
大意: 给定字符串$s$, $p$, 对于$0\le x\le |s|$, 求$s$删除$x$个字符后, $p$在$s$中的最大出现次数. 显然答案是先递增后递减的, 那么问题就转化求最大出现次数为$ ...
- golang学习笔记15 golang用strings.Split切割字符串
golang用strings.Split切割字符串 kv := strings.Split(authString, " ") if len(kv) != 2 || kv[0] != ...
- [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 ...
- [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. ...
- Codeforces 762C Two strings 字符串
Cpdeforces 762C 题目大意: 给定两个字符串a,b\((len \leq 10^5)\),让你去b中的一个连续的字段,使剩余的b串中的拼接起来的两个串是a穿的子序列.最大化这个字串的长度 ...
- [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 ...
- [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. ...
- [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 ...
- [LeetCode] Split Concatenated Strings 分割串联字符串
Given a list of strings, you could concatenate these strings together into a loop, where for each st ...
随机推荐
- python3 selenium使用
其实这个就相当于模拟人的点击事件来连续的访问浏览器.如果你玩过王者荣耀的话在2016年一月份的版本里面就有一个bug. 安卓手机下载一个按键精灵就可以在冒险模式里面设置按键,让手机自动玩闯关,一局19 ...
- webpack搭建多页面系统(一):对webpack 构建工具的理解
为什么使用webpack构建工具? 1.开发效率方面: 在一般的开发过程中,分发好任务后,每个人完成自己单独的页面,如果有的人开发完成之后,接手别人的任务,就有可能造成开发时候的冲突. 如果利用模块化 ...
- C++入门经典-友元
1:在讲述类的内容时说明了隐藏数据成员的好处,但是有时类会允许一些特殊的函数直接读写其私有数据成员. 使用friend关键字可以使特定的函数或者别的类的所有成员函数对私有数据成员进行读写.这既可以保持 ...
- 2 大O表示法
1.大O表示法 表示程序的执行时间或占用空间随数据规模的增长趋势. 算法操作 时间复杂度 线性查找 O(n) 二分查找 O(logn) 无序数组插入 O(1) 无序数组删除 O(n) 有序数组插入 O ...
- LeetCode 130. 被围绕的区域(Surrounded Regions)
题目描述 给定一个二维的矩阵,包含 'X' 和 'O'(字母 O). 找到所有被 'X' 围绕的区域,并将这些区域里所有的 'O' 用 'X' 填充. 示例: X X X X X O O X X X ...
- GitHub:Baidu
ylbtech-GitHub:Baidu 1.返回顶部 · duedge-recipes DuEdge百度边缘网络计算样例代码 edgeedge-computingduedge JavaScri ...
- linux命令行下常用快捷键
快捷键的使用: ctrl+d或者使用logout.exit退出终端ctrl+a跳到开始ctrl+e跳到最后ctrl+u向前删除ctrl+k向后删除ctrl+c中断命令ctrl+z暂停命令 fg:将暂停 ...
- Kafka-manager安装部署
一.kafka-manager 简介 为了简化开发者和服务工程师维护Kafka集群的工作,yahoo构建了一个叫做Kafka管理器的基于Web工具,叫做 Kafka Manager.这个管理工具可以很 ...
- Centos 在线安装 nginx
centos 在线安装 nginx 安装nginx 参考文档: http://nginx.org/en/linux_packages.html 中的RHEL/CentOS章节,按照步骤安装repo ...
- Linux的ifconfig看到的信息详解
Linux的ifconfig看到的信息详解 [root@localhost ~]# ifconfig eth0 Link encap:Ethernet HWaddr :::BF:: inet addr ...