C.Two strings
来源:互联网 发布:zabbix server mysql 编辑:程序博客网 时间:2024/06/05 12: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;
- }
- 762C - Two strings
- C.Two strings
- CodeForces 762C Two strings
- [codeforces] 762C - Two strings 线段树
- Educational Codeforces Round 17-C Two strings
- 【字符串+乱搞】Codeforces 762C Two strings
- Two Strings
- [Educational Codeforces Round 17 C (762C)] Two strings
- C. Two strings-二分的威力,思路题
- Educational Codeforces Round 17 C. Two strings(二分)
- Codeforces 762C Two Strings 二分判定+预处理
- 第十三周:( LeetCode583) Delete Operation for Two Strings(c++)
- Codeforces 762C Two strings (前缀与后缀)
- [LintCode]Two Strings Are Anagrams(C++|Java|Python)
- BNU29375:Two Strings
- BNU 29375 Two Strings
- Two Strings Are Anagrams
- Two Strings Are Anagrams
- 数据库实验四(用户权限管理 )
- 《linux程序设计学习笔记》之一---几个常用的目录和底层调用
- 【mysql】 SQL中inner join、outer join和cross join的区别
- Hibernate代码
- 类模板
- C.Two strings
- IT公司笔试之精选7道Java笔试题解答
- Leetcode 164 Maximum Gap
- 183.Customers Who Never Order题解
- explain sql实际分析
- 指数分布族和广义线性模型
- Java学习笔记---泛型
- PAT--1035. Password
- NYOJ 5 Binary String Mathing (substr函数)