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. caffe学习--cifar10学习-ubuntu16.04-gtx650tiboost--1g--03--20171103

    classification ./examples/cifar10/cifar10_full.prototxt ./examples/cifar10/cifar10_full_iter_70000.c ...

  2. spring整合hibernate,在获取sessionFactory的时候报错,求解决办法!!

    applicationContext.xml文件 <!-- 开启扫包 --> <context:component-scan base-package="cn.edu&qu ...

  3. RPMBUILD源码打包资源汇总(转)

    http://mattshma.github.io/2015/11/04/rpm%E6%89%93%E5%8C%85/ http://400053.blog.51cto.com/390053/7210 ...

  4. mfc 小程序---在系统菜单中添加菜单项

    1建立一个对话框工程:在dlg类里定义一个菜单指针m_pMenu,在对话框OnInitDialog函数里添加代码: m_pMenu=GetSystemMenu(FALSE);//获取系统菜单的指针 m ...

  5. Android-Android进程间通讯之messenger

    转自‘https://www.cnblogs.com/makaruila/p/4869912.html 平时一说进程间通讯,大家都会想到AIDL,其实messenger和AIDL作用一样,都可以进行进 ...

  6. 1355: [Baltic2009]Radio Transmission[循环节]

    1355: [Baltic2009]Radio Transmission Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 796  Solved: 538 ...

  7. 【题解】P2444 病毒

    [题解][P2444 POI2000]病毒 一道\(ac\)自动机好题... 考虑危险的字符串是什么意思,就是在这个文本串中有模式串的匹配,这样的匹配可以通过\(ac\)自动机完成. 那么给定一个字符 ...

  8. python+NLTK 自然语言学习处理三:如何在nltk/matplotlib中的图片中显示中文

    我们首先来加载我们自己的文本文件,并统计出排名前20的字符频率 if __name__=="__main__": corpus_root='/home/zhf/word' word ...

  9. 宿舍更换的新淋浴喷头"水温vs旋钮角度"关系的研究(曲线)

    版权声明:我极少创造新知识,大部分情况下是个知识的二道贩子 https://blog.csdn.net/stereohomology/article/details/24478825 应该非常一目了然 ...

  10. Oracle伪列rownum

    Oracle基础知识:伪列rownum,伪列就像表中的列一样,但是在表中并不存储.伪列只能查询,不能进行增删改操作. 在查询的结果集中,ROWNUM为结果集中每一行标识一个行号,第一行返回1,第二行返 ...