zoukankan      html  css  js  c++  java
  • D2. Remove the Substring (hard version)(思维 )

    D2. Remove the Substring (hard version)
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    The only difference between easy and hard versions is the length of the string.

    You are given a string ss and a string tt, both consisting only of lowercase Latin letters. It is guaranteed that tt can be obtained from ss by removing some (possibly, zero) number of characters (not necessary contiguous) from ss without changing order of remaining characters (in other words, it is guaranteed that tt is a subsequence of ss).

    For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test".

    You want to remove some substring (contiguous subsequence) from ss of maximum possible length such that after removing this substring tt will remain a subsequence of ss.

    If you want to remove the substring s[l;r]s[l;r] then the string ss will be transformed to s1s2sl1sr+1sr+2s|s|1s|s|s1s2…sl−1sr+1sr+2…s|s|−1s|s| (where |s||s| is the length of ss).

    Your task is to find the maximum possible length of the substring you can remove so that tt is still a subsequence of ss.

    Input

    The first line of the input contains one string ss consisting of at least 11 and at most 21052⋅105 lowercase Latin letters.

    The first line of the input contains one string tt consisting of at least 11 and at most 21052⋅105 lowercase Latin letters.

    It is guaranteed that tt is a subsequence of ss.

    Output

    Print one integer — the maximum possible length of the substring you can remove so that tt is still a subsequence of ss.

    Examples
    input
    bbaba
    bb
    
    output
    3
    
    input
    baaba
    ab
    
    output
    2
    
    input
    abcde
    abcde
    
    output
    0
    
    input
    asdfasdf
    fasd
    
    output
    3
    

    算法:思维

     题解:匹配字串问题,我们可以先找到第一个子串出现的位置,记录到一个数组f里面,然后从后往前匹配,每次获取最大的长度,前面那个字符第一次出现的位置,如果a串中有一个字符和b串的字符相等,就向前推一个字符,继续寻找。

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cmath>
     
    using namespace std;
     
    const int maxn = 2e5+7;
     
    typedef long long ll;
    
    string a;
    string b;
    int f[maxn];
     
    int main() {
        cin >> a >> b;
        f[0] = -1;
        int k = 1;
        for(int i = 0, j = 0; i < a.size(); i++) {
            if(j < b.size() && a[i] == b[j]) {      //找到第一个子串的位置
                f[k++] = i;
                j++;
            }
        }
        int ans = 0;
        for(int i = a.size() - 1, j = 0; i >= 0; i--) {
            ans = max(ans, i - f[b.size() - j]);
            if(j < b.size() && a[i] == b[b.size() - j - 1]) {
                j++;
            }
        }
        cout << ans << endl;
        return 0;
    }
  • 相关阅读:
    Cookie数据的编码及解码
    删除单链表节点,时间复杂度为O(1)
    匹配URL
    C#文本框允许使用ctrl+A
    实现统计一个字符串所含的不同字符的总数
    调用win32 api 函数SendMessage() 实现消息直接调用
    关于C++的const对象
    从一个文本文件中找出使用频率最高的五个字符
    C++基础中的基础(深拷贝与浅拷贝)
    python+Django CRM客户关系管理系统开发(十)--左右移动选择框功能开发
  • 原文地址:https://www.cnblogs.com/buhuiflydepig/p/11350368.html
Copyright © 2011-2022 走看看