zoukankan      html  css  js  c++  java
  • 二分枚举答案

    A. String Game
    time limit per test
    2 seconds
    memory limit per test
    512 megabytes
    input
    standard input
    output
    standard output

    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.

    Examples
    Input
    ababcba
    abb
    5 3 4 1 7 6 2
    Output
    3
    Input
    bbbabb
    bb
    1 6 3 4 2 5
    Output
    4
    Note

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

    "ababcba" "ababcba" "ababcba" "ababcba"

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

    So, Nastya will remove only three letters.

    思路 : 最暴力的解决方法不就是暴力枚举删去几个字母,在枚举的每次的时候去暴力匹配一下两个串, 会超时的。

      想想怎么优化 :

        最终问题的答案一定可以是 0 到 第一个串的长度间的一个值 ,因此就可以二分枚举这些值 。

    代码示例 :

    /*
     * Author:  ry 
     * Created Time:  2017/10/10 21:12:17
     * File Name: 1.cpp
     */
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <string>
    #include <vector>
    #include <stack>
    #include <queue>
    #include <set>
    #include <map>
    #include <time.h>
    using namespace std;
    const int eps = 2e5+5;
    const double pi = acos(-1.0);
    const int inf = 0x3f3f3f3f;
    #define Max(a,b) a>b?a:b
    #define Min(a,b) a>b?b:a
    #define ll long long
    
    char a[eps], b[eps];
    int pre[eps];
    int len1, len2;
    bool pt[eps];
    
    bool check(int x){   
        for(int i = 1; i <= x; i++){
            pt[pre[i]] = true;
        }
        int p = 1;
        int cnt = 0;
        for(int i = 1; i <= len2; i++){
            for(int j = p; j <= len1; j++){
                if (b[i] == a[j] && !pt[j]){
                    p = j + 1;
                    pt[j] = true;
                    cnt++;
                    break;
                }
                p = j + 1; // 这一句很关键,如果两个串都不相同,这样一遍遍扫就超时了。
            }          
        }    
        //printf("xxxxx%d %d
    ", x, cnt);
        if (cnt == len2) return true;
        else return false;
    }
    
    int main() {
        
        scanf("%s%s", a+1, b+1);
        len1 = strlen(a+1);
        len2 = strlen(b+1);
        //printf("%d  %d****
    ", len1, len2);
        for(int i = 1; i <= len1; i++){
            scanf("%d", &pre[i]);
        }  
        
        int l = 0, r = len1;
        int ans = 0;
        while (l <= r){
            memset(pt, 0, sizeof(pt));
            int mid = (l + r) >> 1;
            //if (len1 - mid < len2) r = mid - 1;
            if (check(mid)) {
                ans = max(ans, mid);
                l = mid + 1;
            } 
            else r = mid - 1;   
        }
        printf("%d
    ", ans);
        
        return 0;
    }
    
    东北日出西边雨 道是无情却有情
  • 相关阅读:
    微软软件
    绘图软件安装出错解决方法
    Windows平台 Faster-RCNN 制作自己的数据集
    POJ2456 Agressive Cows
    P1030 求先序排列
    Luogu P2015二叉苹果树
    P2234 [HNOI2002]营业额统计
    Luogu P1347排序
    Luogu P1038神经网络
    Luogu P1006传纸条
  • 原文地址:https://www.cnblogs.com/ccut-ry/p/7648152.html
Copyright © 2011-2022 走看看