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 }
  • 相关阅读:
    IOS tableView的数据刷新
    IOS Modal(切换另外控件器方式)
    IOS UITabBarController(控制器)的子控制器
    iOS 应用数据存储的常用方式
    IOS 获取文本焦点 主动召唤出键盘(becomeFirstResponder) and 失去焦点(退下键盘)
    集合类型
    提取URL的搜索字符串中的参数
    本地对象、内置对象、宿主对象
    声明函数 执行上下文 匿名函数
    完善tab页面定位
  • 原文地址:https://www.cnblogs.com/wangyiming/p/6445530.html
Copyright © 2011-2022 走看看