Cpdeforces 762C

题目大意:

给定两个字符串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 字符串的更多相关文章

  1. Codeforces 452E Three strings 字符串 SAM

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF542E.html 题目传送门 - CF452E 题意 给定三个字符串 $s1,s2,s3$ ,对于所有 $L ...

  2. Two strings CodeForces - 762C (字符串,双指针)

    大意: 给定字符串$a$,$b$, $b$可以任选一段连续的区间删除, 要求最后$b$是$a$的子序列, 求最少删除区间长度. 删除一段连续区间, 那么剩余的一定是一段前缀和后缀. 判断是否是子序列可 ...

  3. 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 ...

  4. codeforces 112APetya and Strings(字符串水题)

    A. Petya and Strings 点击打开题目 time limit per test 2 seconds memory limit per test 256 megabytes input ...

  5. Codeforces Round #313 (Div. 2) D.Equivalent Strings (字符串)

    感觉题意不太好懂 = =# 给两个字符串 问是否等价等价的定义(满足其中一个条件):1.两个字符串相等 2.字符串均分成两个子串,子串分别等价 因为超时加了ok函数剪枝,93ms过的. #includ ...

  6. CodeForces 1056E - Check Transcription - [字符串hash]

    题目链接:https://codeforces.com/problemset/problem/1056/E One of Arkady's friends works at a huge radio ...

  7. Codeforces 868D Huge Strings - 位运算 - 暴力

    You are given n strings s1, s2, ..., sn consisting of characters 0 and 1. m operations are performed ...

  8. codeforces 883H - Palindromic Cut - [字符串处理]

    题目链接:http://codeforces.com/problemset/problem/883/H Time limit: 3000 ms Memory limit: 262144 kB Koly ...

  9. [LeetCode] Add Strings 字符串相加

    Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...

随机推荐

  1. Source-php-request-2

    php比較坑的地方就是实现相同的目的,能够使用超级多种手段.比方(file_get_contents和fopen以及如今提到的curl以及fsockopen当然还有socket)这对于一个经验少的程序 ...

  2. shell脚本学习笔记 (流编辑器sed)

    sed意为流编辑器(Stream Editor),在Shell脚本和Makefile中作为过滤器使用很普遍,也就是把前一个程序的输出引入sed的输入,经过一系列编辑命令转换为另一种格式输出. sed不 ...

  3. dede中可以用系统设置中的添加新变量来调用频繁改变的文字内容

    这样真正使所有内容都可以在后台控制.

  4. 02-cookie案例-显示用户上次访问网站的时间

    package cookie; import java.io.IOException;import java.io.PrintWriter;import java.util.Date; import ...

  5. 阿里云Opensearch数据类型

    阿里云主要支持以下数据类型,详情参考:https://help.aliyun.com/document_detail/29121.html 类型 说明 INT int64整型 INT_ARRAY in ...

  6. dm层 集市层 四层 Build a multi-level data strategy

    集市层 四层模型 ODS(临时存储层) MID(中间层) DM(数据集市层) APP(应用层) http://www.datamartist.com/data-warehouse-vs-data-ma ...

  7. Webpack探索【8】--- 模块热替换详解

    本文主要讲模块热替换相关内容.

  8. 【题解】[P4178 Tree]

    [题解]P4178 Tree 一道点分治模板好题 不知道是不是我见到的题目太少了,为什么这种题目都是暴力开值域的桶QAQ?? 问点对,考虑点分治吧.直接用值域树状数组开下来,统计的时候直接往树状数组里 ...

  9. php操作apache服务器上的ftp

    在此之前,请先在window7上搭建apache-ftp服务器,请查看文章:Windows 上搭建Apache FtpServer test.php <?php set_time_limit(0 ...

  10. 出版物排版软件——对XML数据进行排版、浏览、转换、打印

    XML和XSL进行排版的功能强大的软件,并将排版结果进行打印或转换成各种各样的文件格式,满足各方需求.   随着出版物电子排版方式的普及,大部头出版物的排版,越来越多的应用在人们的工作中.比如,惠普公 ...