zoukankan      html  css  js  c++  java
  • CF778A(round 402 div.2 D) String Game

    题意:

    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

    思路:

    二分。

    实现:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <string>
     4 #include <cstring>
     5 using namespace std;
     6 
     7 int a[200005], n, m;
     8 string s, p;
     9 
    10 bool check(int x)
    11 {
    12     string tmp = s;
    13     for (int i = 0; i < x; i++)
    14     {
    15         tmp[a[i] - 1] = '*';
    16     }
    17     int i = 0, j = 0;
    18     while (i < n && j < m)
    19     {
    20         if (tmp[i] != p[j])
    21             i++;
    22         else
    23         {
    24             i++;
    25             j++;
    26         }
    27     }
    28     return j == m;
    29 }
    30 
    31 int search()
    32 {
    33     int l = 0, r = n - 1, m;
    34     int res = 0;
    35     while (l <= r)
    36     {
    37         m = (l + r) >> 1;
    38         if (check(m))
    39         {
    40             res = m;
    41             l = m + 1;
    42         }
    43         else
    44         {
    45             r = m - 1;
    46         }
    47     }
    48     return res;
    49 }
    50 
    51 int main()
    52 {
    53     cin >> s >> p;
    54     n = s.length();
    55     m = p.length();
    56     for (int i = 0; i < n; i++)
    57     {
    58         scanf("%d", &a[i]);
    59     }
    60     cout << search() << endl;
    61     return 0;
    62 }
  • 相关阅读:
    数据库
    SqlDataAdapter.Fill
    在应用程序级别之外使用注册为 allowDefinition='MachineToApplication' 的节是错误的。如果在 IIS 中没有将虚拟目录配置为应用程序,则可能导致此错误。
    GridView控件
    已成功与服务器建立连接,但是在登录过程中发生错误
    查看存储过程代码
    vs2005 调试时出现“无法附加。绑定句柄无效”的解决办法
    验证码
    ORACLE的几个函数在MYSQL里面的简单实现
    【100题】第四十五题 雅虎面试两道题(矩阵判断、数组划分)
  • 原文地址:https://www.cnblogs.com/wangyiming/p/6445530.html
Copyright © 2011-2022 走看看