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. HTML5 2D平台游戏开发#1

    在Web领域通常会用到一组sprite来展示动画,这类动画从开始到结束往往不会有用户参与,即用户很少会用控制器(例如鼠标.键盘.手柄.操作杆等输入设备)进行操作.但在游戏领域,sprite动画与控制器 ...

  2. SQL Server中排名函数row_number,rank,dense_rank,ntile详解

    SQL Server中排名函数row_number,rank,dense_rank,ntile详解 从SQL SERVER2005开始,SQL SERVER新增了四个排名函数,分别如下:1.row_n ...

  3. php解码“&#”编码的中文用函数html_entity_decode()

    遇到类似 ' 这种编码的字,我们可以用html_entity_decode()函数来解码. html_entity_decode() 函数把 HTML 实体转换为字符. 语法 html_entity_ ...

  4. Github的基本功能:

    作者:Fadeoc Khaos链接:http://www.zhihu.com/question/20070065/answer/30521531来源:知乎著作权归作者所有,转载请联系作者获得授权. G ...

  5. Drupal 主题的表现形式

    1.template.php /**  * Implements hook_theme().  */ function yourtheme_theme($existing, $type, $theme ...

  6. 华为基于策略划分VLAN的配置方法及示例

     学过思科交换机的朋友,可能对基于策略划分VLAN的配置方法印象非常深,感觉确实比较复杂,先要配置VMPS以及VMPS数据库,但在华为交换机中,这种现象得到了彻底改变,因为它有了一种特殊的端口类型—— ...

  7. 苹果开发之COCOA编程(第三版)上半部分

    第一章:什么是Cocoa 1.1 历史简介 1.2 开发工具:Xcode.Interface Builder(一个GUI构建工具).在它们内部,使用gcc为编译器来编译代码,并使用gdb来查找错误 1 ...

  8. native2ascii转码工具的使用

    native2ascii转码工具是JDK自带的一种,方便我们将非unicode的编码文件转为unicode格式的文件,位置一般是位于JAVA_HOME/bin目录下. Why? 在做Java开发的时候 ...

  9. dubbo介绍及其使用案例

    dubbo介绍及其使用案例 1. Dubbo是什么? Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案.简单的说,dubbo就是个服务框架,如果 ...

  10. TCP/UDP server

    Simple: Sample TCP/UDP server https://msdn.microsoft.com/en-us/library/aa231754(v=vs.60).aspx Simple ...