zoukankan      html  css  js  c++  java
  • Codeforces Round #402 (Div. 2) D题 【字符串二分答案+暴力】

    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 t: a1... 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.

    Input

    ababcba
    abb
    5 3 4 1 7 6 2

    Output

    3

    Input

    bbbabb
    bb
    1 6 3 4 2 5

    Output

    4

     题意:给你一段操作序列;按顺序依次删掉字符串1中相应位置的字符;问你最多能按顺序删掉多少个字符;使得s2是剩下的字符构成的字符串的子列;

     思路:二分答案+暴力

    AC代码:

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 
     4 const int N = 2e5 + 1000;
     5 char s1[N], s2[N];
     6 bool vis[N];
     7 int a[N], n, l2;
     8 bool ok(){
     9     for (int i = 1, j = 1; i <= n && j <= l2; i++){
    10         if (!vis[i]) continue;
    11         if (s1[i] == s2[j]){
    12             j++;
    13             if (j > l2)
    14                 return true;
    15         }
    16     }
    17     return false;
    18 }
    19 int main(){
    20     scanf("%s", s1 + 1);
    21     n = strlen(s1 + 1);
    22     scanf("%s", s2 + 1);
    23     l2 = strlen(s2 + 1);
    24     for(int i=1;i<=n;i++)
    25         scanf("%d",&a[i]);
    26     int l = 0, r = n, ans = 0;
    27     while (l <= r){
    28         int m = (l + r) >> 1;
    29         for(int i=1;i<=n;i++)
    30             vis[a[i]] = true;
    31         for(int i=1;i<=m;i++)
    32             vis[a[i]] = false;
    33         if (ok()){
    34             ans = m;
    35             l = m + 1;
    36         }
    37         else
    38             r = m - 1;
    39     }
    40     printf("%d
    ", ans);
    41     return 0;
    42 }
  • 相关阅读:
    rsync 安装使用详解
    shell全备份脚本(借鉴别人的,在其基础上修复完善了bug)
    完全备份、差异备份以及增量备份的区别
    云主机格式化和挂载数据盘
    JSONP跨域
    php的多线程使用
    tp其他功能
    Zend Guard Loader和Zend Optimizer的安装(更新中)
    前端编码规范
    前端优化
  • 原文地址:https://www.cnblogs.com/pengge666/p/11545079.html
Copyright © 2011-2022 走看看