Codeforces 762C Two strings 字符串
题目大意:
给定两个字符串a,b\((len \leq 10^5)\),让你去b中的一个连续的字段,使剩余的b串中的拼接起来的两个串是a穿的子序列。最大化这个字串的长度。
题解:
删除这个操作不太好说,我们先换一个思路:实际上删除就是在b串中分别取出两个不相交的前缀和后缀,使得这两个串在a串中不重叠地出现
我们发现答案具有显然的单调性,所以我们首先二分答案(二分删去的字串长度)。
现在来考虑如何进行判定:
一个很直接的思路就是枚举所有的符合条件的前缀和后缀
然后对这个前缀求其在a中的最靠左的子序列的右端下标。
相应的后缀也这么处理.
如: a = "abbcbc",pre = "abc" [数组下标从1开始]
那么串pre,在a中最靠左的子序列有段下标为4.
那么我们我们只要找到一个点,从这个点劈开后,前缀的下标 < 后继的下标即可
枚举\(O(n)\)求出在a序列中拓展到的位置\(O(n)\),总时间复杂度为\(O(n^2logn)\)
TLE
我们发现每次对一个前缀求在a中出现的下标都是重复的问题
所以我们\(O(n)\)预处理一遍(后缀是类似的)
所以我们就做到了\(O(n)\)判定
时间复杂度\(O(nlogn)\)
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
inline void read(int &x){
x=0;char ch;bool flag = false;
while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
inline int cat_max(const int &a,const int &b){return a>b ? a:b;}
inline int cat_min(const int &a,const int &b){return a<b ? a:b;}
const int maxn = 200010;
char a[maxn],b[maxn];
int pre[maxn],suf[maxn];
int main(){
scanf("%s%s",a+1,b+1);
int n = strlen(a+1);
int m = strlen(b+1);
for(int i=1,j=0;b[i];++i){++j;
while(b[i] != a[j] && j <= n) ++ j;
pre[i] = j;
}
for(int i=m,j=n+1;b[i];--i){--j;
while(b[i] != a[j] && j > 0) -- j;
suf[i] = j;
}suf[m+1] = n+1;
int ans1,ans2,l= 0,r = m;
while(l <= r){
int mid = l+r >> 1,i;
for(i=0;i+mid <= m;++i) if(pre[i] < suf[i+mid+1]) break;
if(i + mid <= m){
ans1 = i,ans2 = i+mid+1,r = mid-1;
}else l = mid+1;
}
if(ans2 - ans1 > m) putchar('-');
else{
for(int i=1;i<=ans1;++i) putchar(b[i]);
for(int i=ans2;b[i];++i) putchar(b[i]);
}
getchar();getchar();
return 0;
}
Codeforces 762C Two strings 字符串的更多相关文章
- Codeforces 452E Three strings 字符串 SAM
原文链接https://www.cnblogs.com/zhouzhendong/p/CF542E.html 题目传送门 - CF452E 题意 给定三个字符串 $s1,s2,s3$ ,对于所有 $L ...
- Two strings CodeForces - 762C (字符串,双指针)
大意: 给定字符串$a$,$b$, $b$可以任选一段连续的区间删除, 要求最后$b$是$a$的子序列, 求最少删除区间长度. 删除一段连续区间, 那么剩余的一定是一段前缀和后缀. 判断是否是子序列可 ...
- Codeforces Round #358 (Div. 2) D. Alyona and Strings 字符串dp
题目链接: 题目 D. Alyona and Strings time limit per test2 seconds memory limit per test256 megabytes input ...
- codeforces 112APetya and Strings(字符串水题)
A. Petya and Strings 点击打开题目 time limit per test 2 seconds memory limit per test 256 megabytes input ...
- Codeforces Round #313 (Div. 2) D.Equivalent Strings (字符串)
感觉题意不太好懂 = =# 给两个字符串 问是否等价等价的定义(满足其中一个条件):1.两个字符串相等 2.字符串均分成两个子串,子串分别等价 因为超时加了ok函数剪枝,93ms过的. #includ ...
- CodeForces 1056E - Check Transcription - [字符串hash]
题目链接:https://codeforces.com/problemset/problem/1056/E One of Arkady's friends works at a huge radio ...
- Codeforces 868D Huge Strings - 位运算 - 暴力
You are given n strings s1, s2, ..., sn consisting of characters 0 and 1. m operations are performed ...
- codeforces 883H - Palindromic Cut - [字符串处理]
题目链接:http://codeforces.com/problemset/problem/883/H Time limit: 3000 ms Memory limit: 262144 kB Koly ...
- [LeetCode] Add Strings 字符串相加
Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...
随机推荐
- php 字符串内容是数组格式 转换成数组
一个简单的应用.. 例, $str = "array( 'USD'=>'1', 'GBP'=>'0.6494', 'EUR'=>'0.7668' ,'JPY'= ...
- hibernate的一级缓存和二级缓存详解
hibernate为我们提供了一级缓存和二级缓存,目的是为了减少应用程序对数据库的访问次数. 一级缓存: (1)所谓一级缓存就是session级别的缓存,当我们使用他的范围是当前的session,当s ...
- 简述什么是ajax、javascript、json、Jquery?
什么是Javascript? 基于对象.解释型.事件驱动.脚本语言.封装在<script>标签中使用.弱类型.与浏览器交互执行 什么 是Ajax? ajax是一种编程模式.在客户端与服务器 ...
- Docker入门系列7 动态映射端口port mapping
为何想要动态映射端口呢? 因为刚开始run启动容器时,并不知道里面需要映射哪些端口,等容器已创建了,想映射端口. 当然可以通过先commit成镜像,然后再次run时指定端口,但会生成中间的镜像,对于有 ...
- 手动删除引用nuget如何还原
1.不小心从项目的引用中删除了nuget安装的程序集; 2.从其他地方复制的packages.config到当前项目; 这两种情况 在解决方案中是无法通过还原nuget来还原程序集的,可以通过以下的方 ...
- iOS项目 -- 模仿花椒直播做的第一层次界面
公最近直播比较火爆,我也跟跟风,自己做一个直播app, 现在打算用金山云直播的,但是去注册的时候,联系那边的工作人员,他们讲使用金山云直播要有公司和他们线下签约才能授权开放直播平台. 怎么办呢?于是我 ...
- c++ 系统函数实现文件拷贝
#include "stdafx.h" #include <string> #include<windows.h> #include<iostream ...
- SPOJ SUBLEX - Lexicographical Substring Search 后缀自动机 / 后缀数组
SUBLEX - Lexicographical Substring Search Little Daniel loves to play with strings! He always finds ...
- .Net反射机制
现在谈.Net反射机制本不在计划中,因为本打算研究完设计模式后再去学习别的技术:但迫于设计模式系列一创建型之(抽象工厂模式)一章中遗留问题,才不得已在设计模式之游中插入本篇文章!签于本人对反射理解也不 ...
- 转义字符\r \n \t \b 截图