D2. Remove the Substring (hard version)

给字符串s,t,保证t为s的子序列,求s删掉最长多长的子串,满足t仍为s的子序列

记录t中每个字母在s中出现的最右的位置,

然后从s开头开始跑

遇到和当前t[j]相同的s[i],j++

即使得t中相邻两个字符距离最大化

注意j跑完t了,最后一位应该为s的长度

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. char s[];
  4. char t[];
  5. int R[];
  6. int main()
  7. {
  8. scanf("%s",s);
  9. scanf("%s",t);
  10. int m=strlen(t);
  11. int n=strlen(s);
  12. int j=m-;
  13. int i=n-;
  14. R[m]=n;
  15. while(s[i]!=t[j])
  16. {
  17. i--;
  18. }
  19. R[j--]=i;
  20.  
  21. while(j>=)
  22. {
  23. i--;
  24. while(s[i]!=t[j])i--;
  25. R[j]=i;
  26. j--;
  27. }
  28. j=;
  29. int ans=;
  30. //for(int i=0;i<=m;i++)cout<<R[i]<<' ';
  31. for(int i=;i<n;i++){
  32. // if(j==m)break;
  33. ans=max(ans,R[j]-i);
  34. //cout<<R[j]<<i<<endl;
  35. if(j<m&&s[i]==t[j])j++;
  36.  
  37. }
  38. cout<<ans<<'\n';
  39.  
  40. }

D2. Remove the Substring (hard version)的更多相关文章

  1. D2. Remove the Substring (hard version)(思维 )

    D2. Remove the Substring (hard version) time limit per test 2 seconds memory limit per test 256 mega ...

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

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

  3. CF #579 (Div. 3) D1.Remove the Substring (easy version)

    D1.Remove the Substring (easy version) time limit per test2 seconds memory limit per test256 megabyt ...

  4. 双指针(最大删除子串)Codeforces Round #579 (Div. 3)--Remove the Substring (hard version)

    题目链接:https://codeforces.com/contest/1203/problem/D2 题意: 给你S串.T串,问你最长删除多长的子串使得S串里仍然有T的子序列. 思路: 想了好久,先 ...

  5. Codeforces - 1203D2 - Remove the Substring (hard version) - 双指针

    https://codeforces.com/contest/1203/problem/D2 上次学了双指针求两个字符串之间的是否t是s的子序列.但其实这个双指针可以求出的是s的前i个位置中匹配t的最 ...

  6. CF1203D2 Remove the Substring (hard version) 题解

    这题初赛让我白给了6分,于是我决定回来解决一下它. 说实话,看原题题面和看CCF代码真是两种完全不同的感受…… ------------思路分析: 把$s$串删去一部分之后,会把$s$串分成两部分,当 ...

  7. Remove the Substring

    D2. Remove the Substring (hard version) 思路:其实就是贪心吧,先从前往后找,找到 t 可在 s 中存在的最小位置 (pre),再从后往前找,找到 t 可在 s ...

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

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

  9. Codeforces Round #575 (Div. 3) D1+D2. RGB Substring (easy version) D2. RGB Substring (hard version) (思维,枚举,前缀和)

    D1. RGB Substring (easy version) time limit per test2 seconds memory limit per test256 megabytes inp ...

随机推荐

  1. [Web 前端] 026 jQuery 初探

    目录 1. jQuery 简介 2. jQuery 的简单操作 2.1 jQuery 选择器 2.1.1 简介 2.1.2 基础选择器 2.2 过滤获取 2.3 父子关系获取 3. jQuery 元素 ...

  2. 字符串中的replace方法

    String.prototype.replace() 该方法作为字符串中非常常用的方法, 今天我们具体介绍一下它的用法 语法格式 someString.replace(regxp | substr, ...

  3. Sql server 执行计划详解

    序言 本篇主要目的有二: 1.看懂t-sql的执行计划,明白执行计划中的一些常识. 2.能够分析执行计划,找到优化sql性能的思路或方案. 如果你对sql查询优化的理解或常识不是很深入,那么推荐几骗博 ...

  4. 【洛谷 P1879】【[USACO06NOV]玉米田Corn Fields】

    题目: 链接 思路: Q:如何想到是状压DP? A:那是因为(我看了标签)\(1 ≤ M ≤ 12; 1 ≤ N ≤ 12\),\(2 ^ {12}\) 不过才...(Win7计算器使用中)\(409 ...

  5. HNUSTOJ-1695 跳格子(略感头疼)

    1695: 跳格子 时间限制: 1 Sec  内存限制: 128 MB提交: 230  解决: 57[提交][状态][讨论版] 题目描述 逸夫楼的大厅的地面有10行10列的石砖,我们用坐标(x,y)来 ...

  6. 开发jquery插件小结

    用jquery开发插件其实很简单.今天实现了一个入门级别的功能. 随便来个DIV,便于理解. div{ height:100px;width:100px;display:block;backgroun ...

  7. wordpress中page页添加非插件留言板功能

    把下面的代码插入到page页面中即可 <!-- 留言板 --> <div class="wrap"> <div id="primary&qu ...

  8. Python 中的Lock与RLock

    摘要 由于多线程共享进程的资源和地址空间,因此,在对这些公共资源进行操作时,为了防止这些公共资源出现异常的结果,必须考虑线程的同步和互斥问题. 为什么加锁:1.用于非线程安全, 2.控制一段代码,确保 ...

  9. Sql中使用With创建多张临时表

    CREATE PROC [dbo].[sp_VisitCount] ( @count INT ) AS BEGIN DECLARE @current DATETIME SET @current=GET ...

  10. MyBatis源码浅析

    什么是MyBatis MyBatis是支持定制化SQL.存储过程以及高级映射的优秀的持久层框架.MyBatis 避免了几乎所有的 JDBC 代码和手工设置参数以及抽取结果集.MyBatis 使用简单的 ...