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 ...
随机推荐
- 【BZOJ1854】[Scoi2010]游戏 二分图最大匹配
[BZOJ1854][Scoi2010]游戏 Description lxhgww最近迷上了一款游戏,在游戏里,他拥有很多的装备,每种装备都有2个属性,这些属性的值用[1,10000]之间的数表示.当 ...
- 微信小程序 --- Image组件
Image组件可以在小程序中展示图片,支持外链. Image组件可以调用API,进行三种缩放,九种裁剪. Image组件有默认值:300*225 属性: src:图片资源地址. mode:图片裁剪缩放 ...
- 树链剖分+离散+扫描(HDU5044)
Tree Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Subm ...
- Java开发环境的搭建(jdk,eclipse)
一.java 开发环境的搭建 这里主要说的是在windows 环境下怎么配置环境. 1.首先安装JDK java的sdk简称JDK ,去其官方网站下载最近的JDK即可. http://www.orac ...
- android使用library
http://www.vogella.com/tutorials/AndroidLibraryProjects/article.html 介绍support lib使用 http://de ...
- C#桌面程序设计复习
GGG //屏幕高度 int ScreenH = 1080; this.Location = new Point(this.Location.X, ScreenH - this.Height - 20 ...
- 解决SpringMVC中文乱码
第一种:表单提交后controller获得中文参数后乱码解决方案 注意: 1: form表单提交方式为必须为post,get方式下面spring编码过滤器不起效果 2: jsp页面编码设置为UTF-8 ...
- 【Python】Python 读取csv的某行或某列数据
Python 读取csv的某行 转载 2016年08月30日 21:01:44 标签: python / csv / 数据 站长用Python写了一个可以提取csv任一列的代码,欢迎使用.Gith ...
- sql server 2008维护计划配置
转自:https://blog.csdn.net/yunye114105/article/details/6594826 一:目标任务: 使用SQL Server 2008自带的维护计划自动备份数据库 ...
- 20165324_mybash
20165324_mybash 实验要求 实验要求: 使用fork,exec,wait实现mybash 写出伪代码,产品代码和测试代码 发表知识理解,实现过程和问题解决的博客(包含代码托管链接) 背景 ...