Educational Codeforces Round 17 C. Two strings 打表二分
2 seconds
256 megabytes
standard input
standard output
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).
hi
bob
-
abca
accepted
ac
abacaba
abcdcba
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.
题意:可以删除b字符串中的一个连续子串,需要你删除留下的最小字符;
思路:首先最开始的思路是n*n*log(n)的,TLE;
枚举b的左端点,二分右端点,check需要O(n);
打表求出从1-i的字符串是a的子串的最小位置,同理求i-n的;
可以优化成n*log(n);
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
#define bug(x) cout<<"bug"<<x<<endl;
const int N=1e5+,M=1e6+;
const ll INF=1e18+,mod=;
char a[N],b[N];
int l[N],r[N];
int n,m;
int main()
{
scanf("%s%s",a+,b+);
n=strlen(a+),m=strlen(b+);
int st=;
for(int i=;i<=m;i++)
{
while(st<=n&&a[st]!=b[i])st++;
r[i]=st;
if(st<=n)st++;
}
l[m+]=n+;
int en=n;
for(int i=m;i>=;i--)
{
while(en>=&&a[en]!=b[i])en--;
l[i]=en;
if(en>=)en--;
}
if(r[m]<=n) return *printf("%s\n",b+);
int ansl=,ansr=m;
for(int i=;i<=m;i++)
{
int st=i;
int en=m;
int ans=1e6;
while(st<=en)
{
int mid=(st+en)>>;
if(r[i-]<l[mid+])
{
en=mid-;
ans=mid;
}
else
st=mid+;
}
//cout<<ans<<" "<<i<<endl;
if(ans-i<ansr-ansl)
{
ansl=i;
ansr=ans;
}
}
if(ansl==&&ansr==m)
printf("-\n");
for(int i=;i<ansl;i++)
printf("%c",b[i]);
for(int i=ansr+;i<=m;i++)
printf("%c",b[i]);
printf("\n");
return ;
}
Educational Codeforces Round 17 C. Two strings 打表二分的更多相关文章
- Educational Codeforces Round 17
Educational Codeforces Round 17 A. k-th divisor 水题,把所有因子找出来排序然后找第\(k\)大 view code //#pragma GCC opti ...
- Educational Codeforces Round 12 C. Simple Strings 贪心
C. Simple Strings 题目连接: http://www.codeforces.com/contest/665/problem/C Description zscoder loves si ...
- Educational Codeforces Round 17 颓废记
又被虐了... (记一次惨痛的Codeforces) 好不容易登上去了Codeforces,22:35准时开打 第一题,一看:这不SB题嘛?直接枚举因数上啊.9min才过掉了pretest 第二题.. ...
- Educational Codeforces Round 17 D. Maximum path DP
题目链接:http://codeforces.com/contest/762/problem/D 多多分析状态:这个很明了 #include<bits/stdc++.h> using na ...
- Codeforces Educational Codeforces Round 17 Problem.A kth-divisor (暴力+stl)
You are given two integers n and k. Find k-th smallest divisor of n, or report that it doesn't exist ...
- Educational Codeforces Round 37 G. List Of Integers (二分,容斥定律,数论)
G. List Of Integers time limit per test 5 seconds memory limit per test 256 megabytes input standard ...
- Educational Codeforces Round 11 C. Hard Process 前缀和+二分
题目链接: http://codeforces.com/contest/660/problem/C 题意: 将最多k个0变成1,使得连续的1的个数最大 题解: 二分连续的1的个数x.用前缀和判断区间[ ...
- Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings
Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...
- Educational Codeforces Round 5
616A - Comparing Two Long Integers 20171121 直接暴力莽就好了...没什么好说的 #include<stdlib.h> #include&l ...
随机推荐
- c# winform窗体边框风格的设计
1.首先,窗体的FormBorderStyle设置成None,不要控制边框. 2.然后,TransparencyKey和BackColor颜色设置成相同的,这样,窗体就透明了. 3.最后,窗体的拖动 ...
- idea的svn插件中compare with the same repository version和compare with latest repository version的区别?
Idea的svn插件中compare with the same repository version和compare with latest repository version的区别? 1.com ...
- pycharm 和 Anaconda 下的 opencv 安装
学习真的切忌三天打鱼两天晒网!! 一开始python下的opencv已经都弄好了,中间电脑坏了一次,好久没有接触这个,就全部都忘完了.深感惋惜. 今天又从新安装了一下opencv.在anaconda下 ...
- 三维凸包求内部一点到表面的最近距离(HDU4266)
http://acm.hdu.edu.cn/showproblem.php?pid=4266 The Worm in the Apple Time Limit: 50000/20000 MS (Jav ...
- HDU_2586_How far away ?
How far away ? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- SUBSTRING_INDEX()
http://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_substring-index ) DEFAULT 'fru ...
- supervisor - Python进程管理工具(转)
add by zhj: 下面是在ubuntu上的一些使用经验 1. 简介 supervisor有两个组件:supervisord和supervisorctl,组成了client/server结构. s ...
- Python总结篇——知识大全
python基础 Python开发环境搭建 Python变量和基本数据类型 python基本数据类型之操作 python的语法规范及for和while python编码 python文件操作 pyth ...
- 关于Controller层返回JSON字符串
/** * 导入jackson包. * @param pn * @return */ @RequestMapping("/emps") @ResponseBody public M ...
- oracle 安装,登陆,配置
1:查看: https://blog.csdn.net/u014177640/article/details/71023380/ 2:登陆 https://zhidao.baidu.com/ques ...