• Codeforces Round #402 (Div. 2) D String Game —— 二分法


    D. String Game
    time limit per test
    2 seconds
    memory limit per test
    512 megabytes
    input
    standard input
    output
    standard output

    Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her.

    Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word ta1... a|t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya "nastya "nastya "nastya "nastya "nastya "nastya".

    Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey.

    It is guaranteed that the word p can be obtained by removing the letters from word t.

    Input

    The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≤ |p| < |t| ≤ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t.

    Next line contains a permutation a1, a2, ..., a|t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≤ ai ≤ |t|, all ai are distinct).

    Output

    Print a single integer number, the maximum number of letters that Nastya can remove.

    Examples
    input
    ababcba
    abb
    5 3 4 1 7 6 2
    
    output
    3
    input
    bbbabb
    bb
    1 6 3 4 2 5
    
    output
    4
    Note

    In the first sample test sequence of removing made by Nastya looks like this:

    "ababcba "ababcba "ababcba "ababcba"

    Nastya can not continue, because it is impossible to get word "abb" from word "ababcba".

    So, Nastya will remove only three letters.




    题解:

     

    1.记录每个字符在第几次被删除,这样很巧妙。在判断时,只需知道这个字符的删除次序是否大于当前的删除次序,就可以判断它当前是否已经被删除。

    如果已删除,则直接跳过,如果未删除,则判断字符是否相等,若相等,则j++。要是记录每次删除的字符哪个时,则必须要直接对字符串进行修改,

    再用二分查找的话,则字符串将要改来改去,复杂。

    2.用二分查找逐步逼近答案。

    二分查找需要十分注意细节。

    /*以后就规定二分查找这样用吧:

    m = (h+t)/2;

    h = m +1/-1;

    t = m +1/-1;

    不要用:

    m = (h+t)/2;

    h = m;

    t = m;

    因为这样可能会永远跳不出循环,例如当 h = 0;t  = 1,m = 0, 且h的条件符合,那么这3个数永远不会改变。

    */


    学习之处:

    1.用数组记录数据时,可以正向记录:

    for(int i = 0,t; i<n; i++)//这样的好处是顺着去记录,缺点是查找时必须遍历。这样记录重点在其存储的内容  
    {  
            scanf("%d",&t);  
            a[i] = t;  
    }  

     

    也可以反向记录:

    for(int i = 0,t; i<n; i++)//这样的好处是可以直接访问,缺点是元素分散,有哈希表的雏形。这样记录重点在于其功能。  
    {  
            scanf("%d",&t);  
            a[t] = i;  
    } 

     

    2.二分查找原来不只是查找数据,还能查找操作步骤啊,这就是举一反三?


    3.似乎很多高明的做法都尽量不对原始数据进行改动,而是用另一种方法达到同样的效果。


    代码如下:

     1 #include<bits/stdc++.h>  
     2   
     3 using namespace std;  
     4   
     5 char a[200005],b[200005];  
     6 int op[200005],lena,lenb;  
     7   
     8 int ok(int last)//判断删除last个字符是否合法  
     9 {  
    10     int j = 0;  
    11     for(int i = 0; i<lena; i++)  
    12     {  
    13         if(op[i]>last)  
    14         {  
    15             if(a[i]==b[j]) j++;  
    16             if(j==lenb) return 1;  
    17         }  
    18     }  
    19     return 0;  
    20 }  
    21   
    22 int main()  
    23 {  
    24    scanf("%s%s",a,b);  
    25     lena = strlen(a); lenb = strlen(b);  
    26     for(int i = 1,j; i<=lena; i++)  
    27     {  
    28         scanf("%d",&j);  
    29         op[j-1] = i;  
    30     }  
    31   
    32     int h = 0,t = lena, mid;  
    33   
    34     /*这里需要十分注意:由于mid可能就是答案,但是由于mid合法,h仍然会继续加1, 
    35     所以结果可能会比实际答案多1,那么可以在h==t时,继续进行操作,这次操作其实 
    36     时判断结果是否合法,如果不合法,则t会-1,t成为正确答案。如果已经是正确答案, 
    37     那么h会+1,而t仍然为正确答案。所以两种情况中,t为正确答案。*/  
    38     while(h<=t)  
    39     {  
    40         mid = (h+t+1)/2;  
    41         if(ok(mid)) h = mid+1;  
    42         else t = mid-1;  
    43     }  
    44     printf("%d
    ",t);  
    45     return 0;  
    46 }  
    View Code
  • 相关阅读:
    C#的System.Diagnostics.Trace.WriteLine 写入到文件中案例
    ubuntu开放指定端口
    mysql 启报错报 The server quit without updating PID file
    【WebMisCentral WMC】基于Extjs 4.2x的企业级用户授权认证中心系统(SSO+AM+SM),多租户SAAS应用
    Ajaxupload.js在最新版chrome 83版浏览器oncomplete失效问题解决方法
    SQLServer 父子结构group汇总显示
    jqweui 关于$(document.body).infinite的bug
    EntityFramework 动态构造排序 Func<IQueryable<T>, IOrderedQueryable<T>> Dynamic
    Safari 3D transform变换z-index层级渲染异常的研究
    Asp.net Core中使用NLog,并封装成公共的日志方法
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/7538770.html
Copyright © 2020-2023  润新知