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. 深度 | Facebook的图像识别很强大,一次开源了三款机器视觉工具(附论文)

    http://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&mid=2650718597&idx=1&sn=56aa4e5deff9962 ...

  2. 【TensorFlow-windows】(七) CNN之VGG-net的测试

    主要内容: 1.CNN之VGG-net的测试 2.该实现中的函数总结 平台: 1.windows 10 64位 2.Anaconda3-4.2.0-Windows-x86_64.exe (当时TF还不 ...

  3. 【LeetCode从零单排】No.135Candy(双向动态规划)

    1.题目 There are N children standing in a line. Each child is assigned a rating value. You are giving ...

  4. C#中的let字句应用示例

    一.应用场景 在查询表达式中,存储子表达式的结果有时很有用,这样可以在随后的子句中使用. 可以使用 let 关键字完成这一工作,该关键字可以创建一个新的范围变量,并且用您提供的表达式的结果初始化该变量 ...

  5. Android中的android:layout_width和android:width

    最近在看android的东西,发现很多和web前台的东西一样(思想).只是看到很多属性的写法和前台有差别,刚刚看到这样的属性: android:width 其实是定义控件上面的文本(TextView) ...

  6. 广播、多播和IGMP的一点记录

    广播和多播:仅应用于UDP 广播分为: 1.受限的广播(255.255.255.255) 2.指向网络的广播(eg:A类网络 netid.255.255.255)主机号为全1的地址 3.指向子网的广播 ...

  7. 【BZOJ4619/3709】[Wf2016]Swap Space/[PA2014]Bohater 贪心

    [BZOJ4619][Wf2016]Swap Space Description 你有许多电脑,它们的硬盘用不同的文件系统储存数据.你想要通过格式化来统一文件系统.格式化硬盘可能使它的容量发生变化.为 ...

  8. 使用@Scheduled注解编写spring定时任务

    import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframewor ...

  9. HIbernate 级联删除

    在一对多的情形下如 Cinema - > Screen; 1.正常在不设置级联(casCade)的情况下 删除一的一方(Cinema)会报外键关联异常 (Screen 中包含Cinema的外键) ...

  10. jQuery学习笔记(9)--jquery中的事件--$(document).ready()

    1.$(document).ready()方法是事件模块中最重要的一个函数,可以极大地提高web应用程序的相应速度. 2.执行时机:DOM就绪后就会执行,而javascript中window.onlo ...