zoukankan      html  css  js  c++  java
  • D2. Remove the Substring (hard version) (KMP-next数组 ) ( Codeforces Round #579 (Div. 3) )

    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 second 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
    Copy
    bbaba
    bb
    
    output
    Copy
    3
    
    input
    Copy
    baaba
    ab
    
    output
    Copy
    2
    
    input
    Copy
    abcde
    abcde
    
    output
    Copy
    0
    
    input
    Copy
    asdfasdf
    fasd
    
    output
    Copy
    3



    #include<bits/stdc++.h>
    #define SIZE 500010
    #define rep(i, a, b) for(long long i = a; i <= b; ++i)
    #define TLE std::ios::sync_with_stdio(false);   cin.tie(NULL);   cout.tie(NULL);   cout.precision(10);
    using namespace std;
    typedef long long ll;
    
    char s1[SIZE], s2[SIZE];
    int pre[SIZE], post[SIZE], cnt, len1, len2, ans = 0;
    int main()
    {
        TLE;
        cin >> (s1 + 1) >> (s2 + 1);
        len1 = strlen(s1 + 1), len2 = strlen(s2 + 1);
        for (int i = 1, j = 1; i <= len2; ++i)
        {
            while (s1[j] != s2[i]) ++j;
            pre[i] = j++;
        }
        for (int i = len2, j = len1; i; --i)
        {
            while (s1[j] != s2[i]) --j;
            post[i] = j--;
        }
        ans = max(len1 - pre[len2], post[1] - 1);
        rep(i, 1, len2 - 1) ans = max(ans, post[i + 1] - pre[i] - 1);
        cout << ans;
    }
    所遇皆星河
  • 相关阅读:
    Python数据抓取(3) —抓取标题、时间及链接
    Python数据抓取(2) —简单网络爬虫的撰写
    Python数据抓取(2) —简单网络爬虫的撰写
    Apache 显示网页
    Linux 启动 Apache 时报错:(98)Address already in use: make_sock: could not bind to address [::]:80
    Shell编程 之 条件判断式语句
    Shell编程 之 字符处理命令
    Shell编程 之 字符截取命令
    Shell编程 之 正则表达式
    Shell编程 之 环境变量配置文件
  • 原文地址:https://www.cnblogs.com/Shallow-dream/p/11604650.html
Copyright © 2011-2022 走看看