Codeforces 762C Two strings (前缀与后缀)
来源:互联网 发布:汽车行业概况知乎 编辑:程序博客网 时间:2024/05/18 14:43
You are given two strings a and b. You have to remove the minimum possible number of consecutive (standing one after another) characters from string b in such a way that it becomes a subsequence of string a. It can happen that you will not need to remove any characters at all, or maybe you will have to remove all of the characters from b and make it empty.
Subsequence of string s is any such string that can be obtained by erasing zero or more characters (not necessarily consecutive) from string s.
The first line contains string a, and the second line — string b. Both of these strings are nonempty and consist of lowercase letters of English alphabet. The length of each string is no bigger than 105 characters.
On the first line output a subsequence of string a, obtained from b by erasing the minimum number of consecutive characters.
If the answer consists of zero characters, output «-» (a minus sign).
hibob
-
abcaaccepted
ac
abacabaabcdcba
abcba
In the first example strings a and b don't share any symbols, so the longest string that you can get is empty.
In the second example ac is a subsequence of a, and at the same time you can obtain it by erasing consecutive symbols cepted from string b.
123
不得不说CF的 题目 真是又爱又恨啊, 第一遍读上去,感觉像 公共子序列的问题, ,看第二个示例,又不是, 晕啊晕,
题目大意: 给你A 串 B 串 问 在 B串中删除x连续的串后, 剩下的 连在一起是A的子序列,
注意: A 可以不连续, 只要B剩下的字符, A里面有就可以
用两个数组, 一个前缀,一个后缀, 记录 A串中 B的子序列长度, 然后 枚举A 从中选取 删除后缀减去前缀的部分;
选个最小的; 题目是 最小连续的B子串
注意 赋初值的问题 以及 后缀最大的 要存最大值; 是为了删除串的作用
#include <iostream>#include <map>#include <queue>#include <string>#include <algorithm>#include <cmath>#include <vector>#include <cstring>#include <stdio.h>using namespace std;typedef long long ll;const int MAXN=1e5+155;int pre[MAXN],suf[MAXN];//存前缀和后缀 相当于存 A的 对于B的子序列的长度char a[MAXN],b[MAXN];int main(){ while(~scanf("%s%s",a+1,b+1)) { int len1=strlen(a+1); int len2=strlen(b+1); int last=0; // 不论是前缀还是后缀, 只要A 中有B的 某些串就可以 可以不连续, 例如A :abc B: ac B为A的子串 for(int i=1;i<=len1;i++) //赋初值 pre[i]=len2,suf[i]=1; suf[len1+1] = len2+1;// 必须有 目的是为了删除子串的作用, pre[i-1] for(int i=1,j=1;i<=len1&&j<=len2;i++) //这里是构造A的前缀 { if(a[i]==b[j])// 相同记录前缀, 更新 { pre[i]=j; last=j; j++; } else pre[i]=last; //如果这个不相同,就等于前面的last } last=len2+1; for(int i=len1,j=len2;i>=1&&j>=1;i--)//构造A的后缀 { if(a[i]==b[j]) { suf[i]=j; last =j; j--; } else suf[i]=last; } int ans=len2+1, left=1,right=len2+1; //初始化答案为最大; for(int i=1;i<=len1+1;i++)//枚举所有的情况 { if(pre[i-1]<suf[i]&&ans>suf[i]-pre[i-1]) { ans=suf[i]-pre[i-1]; left=pre[i-1]; right=suf[i]; } } if(ans== len2+1 ) { cout<<"-"<<endl; continue; } for(int i=1;i<=len2;i++)// 输出 删除后的b { if(i<=left||i>=right) cout<<b[i]; } } return 0;}
- Codeforces 762C Two strings (前缀与后缀)
- Educational Codeforces Round 17 C && codeforces 762C C. Two strings(前缀后缀的妙用)
- Educational Codeforces Round 17 C. Two strings(字符串前缀后缀处理,好题)
- CodeForces 762C Two strings
- Educational Codeforces Round 17 C && codeforces 762C C. Two strings 详细的题解 (前后缀应用)
- [codeforces] 762C - Two strings 线段树
- 【字符串+乱搞】Codeforces 762C Two strings
- [Educational Codeforces Round 17 C (762C)] Two strings
- 762C - Two strings
- Codeforces 762C Two Strings 二分判定+预处理
- Educational Codeforces Round 17-C Two strings
- codeforces 762C Strings
- codeforces 234C C. Weather(枚举+前缀后缀预处理)
- Two strings CodeForces
- Codeforces 490C Hacking Cypher【前缀模+后缀模+暴力】
- Educational Codeforces Round 17 C. Two strings(二分)
- [FJOI2011] 前缀与后缀
- C.Two strings
- 转 使用迅雷下载百度云盘大文件方法
- 结构体实现通讯录文件流版
- Virtual-Box Ubuntu 16.04 你应该这样来安装Chrome google 浏览器
- CSS即将具备的6种处理器特性
- java.io.CharConversionException: isHexDigit
- Codeforces 762C Two strings (前缀与后缀)
- HDU 2605 Snake(DFS+BFS+技巧+细节)(好题)
- Mysql优化4-合适的索引
- [LeetCode]202. Happy Number(判断正整数是不是Happy Number)
- Android-service之aidl
- 编写qt程序的流程
- x86 中32位寻址模式
- Paint的高级渲染
- 什么是微信小程序?