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 }
  • 相关阅读:
    jquery 一键复制文本到剪切板
    C#根据当前时间获取周,月,季度,年度等时间段的起止时间
    Asp.Net : Page.RegisterStartupScript及 不执行的原因
    mysql 查询当天、本周,本月,上一个月的数据
    ASP.NET: Cookie会话丢失,Session超时配置
    SQL :“传入的表格格式数据流(TDS)远程过程调用(RPC)协议流不正确” 错误
    JS:Math 对象方法
    ASP.NET上传文件到远程服务器(HttpWebRequest)
    Javascript 使用postMessage对iframe跨域传值或通信
    C#中out和ref之间的区别
  • 原文地址:https://www.cnblogs.com/wangyiming/p/6445530.html
Copyright © 2011-2022 走看看