zoukankan      html  css  js  c++  java
  • CodeForces

    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"  "nasya"  "asya"  "sya"  "sa"  "a"  "".

    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
    Copy
    ababcba
    abb
    5 3 4 1 7 6 2
    output
    Copy
    3
    input
    Copy
    bbbabb
    bb
    1 6 3 4 2 5
    output
    Copy
    4
    Note

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

    "ababcba"  "ababba"  "abbba"  "abba"

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

    So, Nastya will remove only three letters.

    题目大意:给两个字符串s1,s2和一个s1长度的数组od[ ],问按od[]给定的顺序去掉s1的字母,最多可以去掉多少个字母使s2仍然是s1的子串。

    解题思路:本题暴力必定会超时,所以可以对数组进行二分查找。用一个Check函数检查s1去掉前mid位是否还能使s2是s1的子串,可以则head = mid + 1,否则tail = mid - 1。

    代码:

     1 #include<cstdio>
     2 #include<cstdlib>
     3 #include<cmath>
     4 #include<cstring>
     5 #include<string>
     6 #include<iostream>
     7 #include<algorithm>
     8 #include<map>
     9 #include<stack>
    10 #include<queue>
    11 using namespace std;
    12 typedef long long LL;
    13 const LL MaxN = 2e5;
    14 
    15 string s1, s2;
    16 int od[MaxN+5], len1, len2;
    17 int head, tail, mid, cnt;
    18 bool vis[MaxN + 5];
    19 
    20 int Check(int x)
    21 {
    22     memset(vis, 0, sizeof(vis));
    23     cnt = 0;
    24     for(int i = 0;i <= x;i++)
    25     {
    26         vis[od[i]-1] = 1;
    27     }
    28     for(int i = 0;i < len1;i++)
    29     {
    30         if(vis[i]) continue;
    31         if(s1[i] == s2[cnt]) cnt++;
    32         if(cnt == len2) return 1;
    33     }
    34     return 0;
    35 }
    36 
    37 int main()
    38 {
    39     cin >> s1 >> s2;
    40     len1 = s1.length();len2 = s2.length();
    41     for(int i = 0;i < len1;i++)
    42     {
    43         scanf("%d", od + i);
    44     }
    45         head = 0, tail = len1-1;
    46         while(head <= tail)
    47         {
    48             mid = (head + tail)/2;
    49             if(Check(mid)) head = mid + 1;
    50             else tail = mid - 1;
    51             if(head > tail)
    52             {
    53                  if(tail == mid) printf("%d
    ", mid+1);
    54                  if(head == mid) printf("%d
    ", mid);
    55                  break;
    56             }
    57         }
    58     return 0;
    59 }
  • 相关阅读:
    Re:从零开始的领域驱动设计
    领域驱动设计和Spring
    深入JVM分析spring-boot应用hibernate-validator
    深入Spring Boot:那些注入不了的Spring占位符(${}表达式)
    Android 使用OKhttp获取response时遇到的坑
    Android EditText组件drawableLeft属性设置的图片和hint设置的文字之间的距离
    Android 自定义spinner下拉框实现的实现
    Android You need to use a Theme.AppCompat theme (or descendant) with this activity.
    Android中取消GridView & ListView默认的点击背景色
    Android 显示 PDF 文件
  • 原文地址:https://www.cnblogs.com/Lightfall/p/9304123.html
Copyright © 2011-2022 走看看