双指针(最大删除子串)Codeforces Round #579 (Div. 3)--Remove the Substring (hard version)
题目链接:https://codeforces.com/contest/1203/problem/D2
题意:
给你S串、T串,问你最长删除多长的子串使得S串里仍然有T的子序列。
思路:
想了好久,先正着跑一下S串,记录T串每一个字符最左边在哪里,再倒着跑一下,记录T串的每一个字符最右边在哪里。
最后跑一下答案:
1. 开头和结尾特判一下,但不是max( L[1]-1 , l1-R[l2] ) , 而是对两个max( L[1]-1 , l1-L[l2]-1 )、max( R[1]-1 , l1-R[l2]-1 )再取max。
2. 对于中间部分:R[i]-L[i-1]-1 。
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#include <cstdio>//sprintf islower isupper
#include <cstdlib>//malloc exit strcat itoa system("cls")
#include <iostream>//pair
#include <fstream>
#include <bitset>
//#include <map>
//#include<unordered_map> https://codeforces.com/contest/1203/problem/D2
#include <vector>
#include <stack>
#include <set>
#include <string.h>//strstr substr
#include <string>
#include <time.h>//srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
#include <cmath>
#include <deque>
#include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
#include <vector>//emplace_back
//#include <math.h>
//#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
#include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
#define fo(a,b,c) for(register int a=b;a<=c;++a)
#define fr(a,b,c) for(register int a=b;a>=c;--a)
#define mem(a,b) memset(a,b,sizeof(a))
#define pr printf
#define sc scanf
#define ls rt<<1
#define rs rt<<1|1
void swapp(int &a,int &b);
double fabss(double a);
int maxx(int a,int b);
int minn(int a,int b);
int Del_bit_1(int n);
int lowbit(int n);
int abss(int a);
//const long long INF=(1LL<<60);
const double E=2.718281828;
const double PI=acos(-1.0);
const int inf=(<<);
const double ESP=1e-;
const int mod=(int)1e9+;
const int N=(int)1e6+; int L[N],R[N];
char s[N],t[N]; int main()
{
// freopen("C:\\Users\\13606\\Desktop\\草稿.txt","r",stdin);
int l1,l2;
s[]=t[]='$';
// while(sc("%s%s",s+1,t+1)==2)
// {
sc("%s%s",s+,t+);
l1=strlen(s)-;
l2=strlen(t)-;
for(int i=,pos=;i<=l1&&pos<=l2;++i)
{
if(t[pos]==s[i])
L[pos]=i,pos++;
}
for(int i=l1,pos=l2;i>=&&pos>=;--i)
{
if(t[pos]==s[i])
R[pos]=i,pos--;
}
int ans=;
ans=maxx(ans,maxx(L[]-,R[]-));
ans=maxx(ans,maxx(l1-L[l2],l1-R[l2]));
for(int i=;i<=l2;++i)
ans=maxx(ans,R[i]-L[i-]-);
pr("%d\n",ans);
// }
return ;
} /**************************************************************************************/ int maxx(int a,int b)
{
return a>b?a:b;
} void swapp(int &a,int &b)
{
a^=b^=a^=b;
} int lowbit(int n)
{
return n&(-n);
} int Del_bit_1(int n)
{
return n&(n-);
} int abss(int a)
{
return a>?a:-a;
} double fabss(double a)
{
return a>?a:-a;
} int minn(int a,int b)
{
return a<b?a:b;
}
双指针(最大删除子串)Codeforces Round #579 (Div. 3)--Remove the Substring (hard version)的更多相关文章
- Codeforces Round #575 (Div. 3) D2. RGB Substring (hard version)
传送门 题意: 给你一个长为n的仅由'R','G','B'构成的字符串s,你需要在其中找出来一个子串.使得这个子串在"RGBRGBRGBRGB........(以RGB为循环节,我们称这个串 ...
- Codeforces Round #575 (Div. 3) D2. RGB Substring (hard version) 水题
D2. RGB Substring (hard version) inputstandard input outputstandard output The only difference betwe ...
- Codeforces Round #575 (Div. 3) D2. RGB Substring (hard version) 【递推】
一.题目 D2. RGB Substring (hard version) 二.分析 思路一开始就想的对的,但是,用memset给数组初始化为0超时了!超时了! 然后我按照题解改了个vector初始化 ...
- Codeforces Round #579 (Div. 3)
Codeforces Round #579 (Div. 3) 传送门 A. Circle of Students 这题我是直接把正序.逆序的两种放在数组里面直接判断. Code #include &l ...
- Codeforces Round #501 (Div. 3) F. Bracket Substring
题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60 ...
- Codeforces Round #579 (Div. 3) 题解
比赛链接:https://codeforc.es/contest/1203/ A. Circle of Students 题意:\(T\)组询问,每组询问给出\(n\)个数字,问这\(n\)个数字能否 ...
- 【cf比赛练习记录】Codeforces Round #579 (Div. 3)
思考之后再看题解,是与别人灵魂之间的沟通与碰撞 A. Circle of Students 题意 给出n个数,问它们向左或者向右是否都能成一个环.比如样例5是从1开始向左绕了一圈 [3, 2, 1, ...
- Codeforces Round #579 (Div. 3) 套题 题解
A. Circle of Students 题目:https://codeforces.com/contest/1203/problem/A 题意:一堆人坐成一个环,问能否按逆时针或者顺时针 ...
- Codeforces Round #579 (Div. 3) D2. Remove the Substring (hard version) (思维,贪心)
题意:给你一个模式串\(t\),现在要在主串\(s\)中删除多个子串,使得得到的\(s\)的子序列依然包含\(t\),问能删除的最长子串长度. 题解:首先,我们不难想到,我们可以选择\(s\)头部到最 ...
随机推荐
- DB 分库分表(4):多数据源的事务处理
系统经sharding改造之后,原来单一的数据库会演变成多个数据库,如何确保多数据源同时操作的原子性和一致性是不得不考虑的一个问题.总体上看,目前对于一个分布式系统的事务处理有三种方式:分布式事务.基 ...
- git忽略提交:.gitignore
在使用Git的过程中,我们喜欢有的文件比如日志,临时文件,编译的中间文件等不要提交到代码仓库,这时就要设置相应的忽略规则,来忽略这些文件的提交. Git 忽略文件提交的方法 有三种方法可以实现忽略Gi ...
- Python 之目录处理
目录处理 OS目录处理目录-->路径,文件夹 文件:txt 1. 新建和删除一个目录 import os #引入os目录from xx import xx os.mkdir("D:\\ ...
- LeetCode 144. 二叉树的前序遍历(Binary Tree Preorder Traversal)
题目描述 给定一个二叉树,返回它的 前序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,2,3] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 解题思路 由 ...
- Linux 常用命令之df du
1.du 命令:显示每个文件或目录的磁盘使用空间 1) du -h --max-depth [root@ip101 app]# pwd /opt/app [root@ip101 app]# du -h ...
- CentOS7 yum install elasticsearch
首先安装 JDK 环境 # 本机是否已经安装,ElasticSearch 最低支持 jdk 1.7 yum list installed | grep java # 查看 yum 库中的 java 安 ...
- RN在Mac环境下开发环境搭建
1.推荐使用Homebrew来安装 Node 和 Watchman.在命令行中执行下列命令安装: brew install node brew install watchman 如果你已经安装了 No ...
- EvenBus源码分析
概述 一般使用EventBus的组件类,类似下面这种方式: public class SampleComponent extends Fragment { @Override public void ...
- 如何在网页中添加 jQuery。
转自:http://www.runoob.com/jquery/jquery-install.html 网页中添加 jQuery 可以通过多种方法在网页中添加 jQuery. 您可以使用以下方法: 从 ...
- mysql 索引失效的几种情况+