CodeForces - 779D String Game(二分)
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" "nasya" "asya" "sya" "sa" "a" "".
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.
ababcba
abb
5 3 4 1 7 6 2
3
bbbabb
bb
1 6 3 4 2 5
4
In the first sample test sequence of removing made by Nastya looks like this:
"ababcba" "ababba" "abbba" "abba"
Nastya can not continue, because it is impossible to get word "abb" from word "ababcba".
So, Nastya will remove only three letters.
题目大意:给两个字符串s1,s2和一个s1长度的数组od[ ],问按od[]给定的顺序去掉s1的字母,最多可以去掉多少个字母使s2仍然是s1的子串。
解题思路:本题暴力必定会超时,所以可以对数组进行二分查找。用一个Check函数检查s1去掉前mid位是否还能使s2是s1的子串,可以则head = mid + 1,否则tail = mid - 1。
代码:
1 #include<cstdio> 2 #include<cstdlib> 3 #include<cmath> 4 #include<cstring> 5 #include<string> 6 #include<iostream> 7 #include<algorithm> 8 #include<map> 9 #include<stack> 10 #include<queue> 11 using namespace std; 12 typedef long long LL; 13 const LL MaxN = 2e5; 14 15 string s1, s2; 16 int od[MaxN+5], len1, len2; 17 int head, tail, mid, cnt; 18 bool vis[MaxN + 5]; 19 20 int Check(int x) 21 { 22 memset(vis, 0, sizeof(vis)); 23 cnt = 0; 24 for(int i = 0;i <= x;i++) 25 { 26 vis[od[i]-1] = 1; 27 } 28 for(int i = 0;i < len1;i++) 29 { 30 if(vis[i]) continue; 31 if(s1[i] == s2[cnt]) cnt++; 32 if(cnt == len2) return 1; 33 } 34 return 0; 35 } 36 37 int main() 38 { 39 cin >> s1 >> s2; 40 len1 = s1.length();len2 = s2.length(); 41 for(int i = 0;i < len1;i++) 42 { 43 scanf("%d", od + i); 44 } 45 head = 0, tail = len1-1; 46 while(head <= tail) 47 { 48 mid = (head + tail)/2; 49 if(Check(mid)) head = mid + 1; 50 else tail = mid - 1; 51 if(head > tail) 52 { 53 if(tail == mid) printf("%d\n", mid+1); 54 if(head == mid) printf("%d\n", mid); 55 break; 56 } 57 } 58 return 0; 59 }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
· .NET 9 new features-C#13新的锁类型和语义
· Sdcb Chats 技术博客:数据库 ID 选型的曲折之路 - 从 Guid 到自增 ID,再到
· 语音处理 开源项目 EchoSharp
· 《HelloGitHub》第 106 期
· Spring AI + Ollama 实现 deepseek-r1 的API服务和调用
· 使用 Dify + LLM 构建精确任务处理应用