CF
来源:互联网 发布:怎么区分中日韩 知乎 编辑:程序博客网 时间:2024/05/02 05:05
1.题目描述:
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.
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).
Print a single integer number, the maximum number of letters that Nastya can remove.
ababcbaabb5 3 4 1 7 6 2
3
bbbabbbb1 6 3 4 2 5
4
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.
2.题意概述:
给出两个字符串str1和str2弟弟按照数列a的顺序删除字符串,其中第a[i]次删除str1[a[i]]的元素,哥哥希望能在某次删除后使得str1 == str2,求最小的删除次数
3.解题思路:
最开始想的是STL的str.erase()模拟,然后re了好久,最终想起了二分,可能是我姿势不太对23333。然后这题就是二分答案,如果check成功则在mid ~ lena之间继续找,否则在1 ~ mid - 1之间找。
4.AC代码:
#include <bits/stdc++.h>#define INF 0x3f3f3f3f#define maxn 200100#define N 1111#define eps 1e-6#define pi acos(-1.0)#define e 2.718281828459#define mod (int)1e9 + 7using namespace std;typedef long long ll;char a[maxn], b[maxn];bool vis[maxn];int lena, lenb, p[maxn];bool check(int ed){ memset(vis, 0, sizeof(vis)); for (int i = 1; i <= ed; i++) vis[p[i]] = 1; for (int i = 1, j = 1; i <= lena; i++) { while (i <= lena && vis[i]) i++; if (i > lena) break; if (a[i] == b[j]) j++; if (j > lenb) return 1; } return 0;}int main(){#ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); long _begin_time = clock();#endif while (~scanf("%s%s", a + 1, b + 1)) { lena = strlen(a + 1); lenb = strlen(b + 1); for (int i = 1; i <= lena; i++) scanf("%d", &p[i]); int l = 0, r = lena, mid; while (l < r) { mid = (l + r + 1) >> 1; if (check(mid)) l = mid; else r = mid - 1; } printf("%d\n", l); }#ifndef ONLINE_JUDGE long _end_time = clock(); printf("time = %ld ms.", _end_time - _begin_time);#endif return 0;}
- CF
- CF
- cf
- cf
- CF
- CF
- CF
- CF
- CF
- CF
- cf
- CF
- cf
- cf
- cf ***
- CF
- CF
- CF
- Add Two Numbers (Leetcode medium algorithm problem)
- Gradle Wrapper介绍
- 剑指offer读书笔记(3)
- 秒杀系统的架构解决之道
- Max and Bike CodeForces
- CF
- WEB服务器:Apache、Tomcat、JBoss、WebLogic、Websphere、IIS的区别与关系
- UEditor之——与SpringMVC整合
- SpringMVC Failed to convert from type java.lang.String to type java.util.Date for value '2014-12-02'
- A1050. String Subtraction (20)
- LeetCode-algorithms 22. Generate Parentheses
- POJ1753 Flip Game (DFS)
- JDBC应用中的事务管理
- UVA-455 Periodic Strings