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 ...
随机推荐
- 什么是真正的APM?
近年来APM行业被越来越多的企业所关注,尤其是在2014年末,NewRelic的成功上市,更加激发了人们对这个行业前景的无限遐想.那么究竟什么是APM?APM的目的是什么?要求我们做什么?有不少企业对 ...
- linux本机root账户无法登录,但是远程ssh可登录
1.故障状态 a.linux本机root账户无法登录(root和密码无误的情况下也无法登录,但是用远程ssh软件可以登录) b.查看/var/log/secure登录日志(提示无法找到pam_limi ...
- SQL Fundamentals:替代变量(&,&&)以及DEFINE,UNDEFINE,ACCEPT指令
替代变量 利用替代变量可以实现数据操作的交互性.替代变量的操作类似于键盘输入操作. 所谓的替代变量,指的就是在进行查询或更新操作时,某些数据是由用户所输入的,而这些数据前可以使用“&”标记. ...
- as modern frameworks have warmed people to the idea of using builder-type patterns and anonymous inner classes for such things
mybatis – MyBatis 3 | SQL语句构建器 http://www.mybatis.org/mybatis-3/zh/statement-builders.html SqlBuilde ...
- python网络编程知识体系
python的网络编程包括: 1.mvc-socket-线程-进程-并发-IO异步-消费者生产者 2.mysql-paramiko-审计堡垒机-redis-分布式监控 线程.进程 和 协程 原理剖析 ...
- top与with ties用法
使用top中把与最后一条记录值相同的数据也放入列表中 一.SQL SERVER中使用WITH TIES的用途 with ties一般是和Top , order by相结合使用的,会查询出最后一条数据额 ...
- python 类 __call__
__call__ 对象后面加括号,触发执行. 即:对象() 或者 类()() class dog(object): def __init__(self,name): self.name = name ...
- centos LB负载均衡集群 三种模式区别 LVS/NAT 配置 LVS/DR 配置 LVS/DR + keepalived配置 nginx ip_hash 实现长连接 LVS是四层LB 注意down掉网卡的方法 nginx效率没有LVS高 ipvsadm命令集 测试LVS方法 第三十三节课
centos LB负载均衡集群 三种模式区别 LVS/NAT 配置 LVS/DR 配置 LVS/DR + keepalived配置 nginx ip_hash 实现长连接 LVS是四层LB ...
- (转)在 ListViewItem 上拖动进行框选
public partial class Form1 : Form { private bool IsMouseDown = false; Rectangle MouseRect = Rectangl ...
- Spring—Ioc
IoC容器,最主要的就是完成对象的创建以及维护对象的依赖关系等. 所谓控制反转,包括两部分:一是控制,二是反转,就是把传统方式需要由代码来实现对象的创建.维护对象的依赖关系,反转给容器来帮忙管理和实现 ...