题目链接: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 。

  1. #define IOS ios_base::sync_with_stdio(0); cin.tie(0);
  2. #include <cstdio>//sprintf islower isupper
  3. #include <cstdlib>//malloc exit strcat itoa system("cls")
  4. #include <iostream>//pair
  5. #include <fstream>
  6. #include <bitset>
  7. //#include <map>
  8. //#include<unordered_map> https://codeforces.com/contest/1203/problem/D2
  9. #include <vector>
  10. #include <stack>
  11. #include <set>
  12. #include <string.h>//strstr substr
  13. #include <string>
  14. #include <time.h>//srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
  15. #include <cmath>
  16. #include <deque>
  17. #include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
  18. #include <vector>//emplace_back
  19. //#include <math.h>
  20. //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
  21. #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
  22. using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
  23. #define fo(a,b,c) for(register int a=b;a<=c;++a)
  24. #define fr(a,b,c) for(register int a=b;a>=c;--a)
  25. #define mem(a,b) memset(a,b,sizeof(a))
  26. #define pr printf
  27. #define sc scanf
  28. #define ls rt<<1
  29. #define rs rt<<1|1
  30. void swapp(int &a,int &b);
  31. double fabss(double a);
  32. int maxx(int a,int b);
  33. int minn(int a,int b);
  34. int Del_bit_1(int n);
  35. int lowbit(int n);
  36. int abss(int a);
  37. //const long long INF=(1LL<<60);
  38. const double E=2.718281828;
  39. const double PI=acos(-1.0);
  40. const int inf=(<<);
  41. const double ESP=1e-;
  42. const int mod=(int)1e9+;
  43. const int N=(int)1e6+;
  44.  
  45. int L[N],R[N];
  46. char s[N],t[N];
  47.  
  48. int main()
  49. {
  50. // freopen("C:\\Users\\13606\\Desktop\\草稿.txt","r",stdin);
  51. int l1,l2;
  52. s[]=t[]='$';
  53. // while(sc("%s%s",s+1,t+1)==2)
  54. // {
  55. sc("%s%s",s+,t+);
  56. l1=strlen(s)-;
  57. l2=strlen(t)-;
  58. for(int i=,pos=;i<=l1&&pos<=l2;++i)
  59. {
  60. if(t[pos]==s[i])
  61. L[pos]=i,pos++;
  62. }
  63. for(int i=l1,pos=l2;i>=&&pos>=;--i)
  64. {
  65. if(t[pos]==s[i])
  66. R[pos]=i,pos--;
  67. }
  68. int ans=;
  69. ans=maxx(ans,maxx(L[]-,R[]-));
  70. ans=maxx(ans,maxx(l1-L[l2],l1-R[l2]));
  71. for(int i=;i<=l2;++i)
  72. ans=maxx(ans,R[i]-L[i-]-);
  73. pr("%d\n",ans);
  74. // }
  75. return ;
  76. }
  77.  
  78. /**************************************************************************************/
  79.  
  80. int maxx(int a,int b)
  81. {
  82. return a>b?a:b;
  83. }
  84.  
  85. void swapp(int &a,int &b)
  86. {
  87. a^=b^=a^=b;
  88. }
  89.  
  90. int lowbit(int n)
  91. {
  92. return n&(-n);
  93. }
  94.  
  95. int Del_bit_1(int n)
  96. {
  97. return n&(n-);
  98. }
  99.  
  100. int abss(int a)
  101. {
  102. return a>?a:-a;
  103. }
  104.  
  105. double fabss(double a)
  106. {
  107. return a>?a:-a;
  108. }
  109.  
  110. int minn(int a,int b)
  111. {
  112. return a<b?a:b;
  113. }

双指针(最大删除子串)Codeforces Round #579 (Div. 3)--Remove the Substring (hard version)的更多相关文章

  1. Codeforces Round #575 (Div. 3) D2. RGB Substring (hard version)

    传送门 题意: 给你一个长为n的仅由'R','G','B'构成的字符串s,你需要在其中找出来一个子串.使得这个子串在"RGBRGBRGBRGB........(以RGB为循环节,我们称这个串 ...

  2. Codeforces Round #575 (Div. 3) D2. RGB Substring (hard version) 水题

    D2. RGB Substring (hard version) inputstandard input outputstandard output The only difference betwe ...

  3. Codeforces Round #575 (Div. 3) D2. RGB Substring (hard version) 【递推】

    一.题目 D2. RGB Substring (hard version) 二.分析 思路一开始就想的对的,但是,用memset给数组初始化为0超时了!超时了! 然后我按照题解改了个vector初始化 ...

  4. Codeforces Round #579 (Div. 3)

    Codeforces Round #579 (Div. 3) 传送门 A. Circle of Students 这题我是直接把正序.逆序的两种放在数组里面直接判断. Code #include &l ...

  5. Codeforces Round #501 (Div. 3) F. Bracket Substring

    题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60 ...

  6. Codeforces Round #579 (Div. 3) 题解

    比赛链接:https://codeforc.es/contest/1203/ A. Circle of Students 题意:\(T\)组询问,每组询问给出\(n\)个数字,问这\(n\)个数字能否 ...

  7. 【cf比赛练习记录】Codeforces Round #579 (Div. 3)

    思考之后再看题解,是与别人灵魂之间的沟通与碰撞 A. Circle of Students 题意 给出n个数,问它们向左或者向右是否都能成一个环.比如样例5是从1开始向左绕了一圈 [3, 2, 1, ...

  8. Codeforces Round #579 (Div. 3) 套题 题解

    A. Circle of Students      题目:https://codeforces.com/contest/1203/problem/A 题意:一堆人坐成一个环,问能否按逆时针或者顺时针 ...

  9. Codeforces Round #579 (Div. 3) D2. Remove the Substring (hard version) (思维,贪心)

    题意:给你一个模式串\(t\),现在要在主串\(s\)中删除多个子串,使得得到的\(s\)的子序列依然包含\(t\),问能删除的最长子串长度. 题解:首先,我们不难想到,我们可以选择\(s\)头部到最 ...

随机推荐

  1. TensorFlow使用记录 (九): 模型保存与恢复

    模型文件 tensorflow 训练保存的模型注意包含两个部分:网络结构和参数值. .meta .meta 文件以 “protocol buffer”格式保存了整个模型的结构图,模型上定义的操作等信息 ...

  2. Python3学习笔记(十八):文件上传和下载

    文件上传 以人人网上传头像为例,用Fiddler抓取的上传头像接口报文如下 上传头像图片代码: import requests upload_url = 'http://upload.renren.c ...

  3. 网络爬虫技术实现java依赖库整理输出

    网络爬虫技术实现java依赖库整理输出 目录 1       简介... 2 1.1      背景介绍... 2 1.2      现有方法优缺点对比... 2 2       实现方法... 2 ...

  4. python递归获取目录下指定文件

    获取一个目录下所有指定格式的文件是实际生产中常见需求. import os #递归获取一个目录下所有的指定格式的文件 def get_jsonfile(path,file_list): dir_lis ...

  5. 2018-2019-2 20165215《网络对抗技术》Exp9 :Web安全基础

    目录 实验目的及内容 实验过程记录 一.Webgoat安装 二. 注入缺陷(Injection Flaws) (一)命令注入(Command Injection) (二)数字型注入(Numeric S ...

  6. Error:java: 错误: 不支持发行版本 5

    本文链接:https://blog.csdn.net/wo541075754/article/details/70154604 在Intellij idea中新建了一个Maven项目,运行时报错如下: ...

  7. IDEA问题java: -source 1.6 中不支持diamond、 lambda 表达式

    文章目录 一.问题:连片的java: -source 1.6 中不支持 diamond 运算符.lambda 表达式 二.解决方法: 1.在微信群里问大佬,大佬在玩游戏,回复的比较慢 2.自己查Goo ...

  8. tomcat manager 配置

    使用网页部署新 Web 应用程序或取消现有 Web 应用程序部署,且无需重启容器. 一.开启管理 编辑 conf/tomcat-users.xml 添加如下内容,这里用户名和密码都为 tomcat & ...

  9. centos 6和centos7关闭防火墙的方法

    centos 6 关闭命令:  service iptables stop 永久关闭防火墙:chkconfig iptables off 两个命令同时运行,运行完成后查看防火墙关闭状态         ...

  10. VI快捷键速记

    enjoy :P