Educational Codeforces Round 17 C. Two strings(二分)

C. Two strings

time limit per test:2 seconds

memory limit per test:256 megabytes

input:standard input

output: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.

Input

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.

Output

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).

Examples

Input
hi
bob

Output

Input
abca
accepted

Output
ac

Input
abacaba
abcdcba

Output
abcba

Note

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.
题意:给两个字符串a.b,将b删除连续的一部分后,成为a的子串,问这个最大子串,没有输出”-“.
题解:大神详解
代码:

#include <bits/stdc++.h>
using namespace std;
const int N=100005;
char a[N],b[N];
int lena,lenb,L,R;
int needh[N],needl[N];
int main()
{
    scanf("%s",a+1);
    scanf("%s",b+1);
    lena=strlen(a+1);
    lenb=strlen(b+1);
    for(int i=1;i<=lenb;i++) needh[i]=needl[i]=lena+1;
    int pos=1;
    L=lenb;
    R=1;
    for(int i=1;i<=lenb;i++)
    {
        while(pos<=lena&&a[pos]!=b[i])++pos;
        if(pos<=lena) needh[i]=pos;
        else
        {
            L=i-1;
            break;
        }
        ++pos;
    }
    pos=lena;
    for(int i=lenb;i>=1;i--)
    {
        while(pos>=1&&a[pos]!=b[i]) --pos;
        if(pos>=1) needl[i]=lena-pos+1;
        else
        {
            R=i+1;
            break;
        }
        --pos;
    }
    if(L<1&&R>lenb)printf("-\n");//b中没有a的子串
    else if(L==lenb||R==1) printf("%s\n",b+1);//b已经是a的子串
    else
    {
        int len,left,right;
        if(L>lenb-R+1)//只有前缀
        {
            len=L;
            left=L;
            right=lenb+1;
        }
        else//只有后缀
        {
            len=lenb-R+1;
            left=0;
            right=R;
        }
        for(int i=R;i<=lenb;i++)//在后缀中枚举一个右端点,二分找最大左端点。
        {
            int p=upper_bound(needh+1,needh+L+1,lena-needl[i])-needh-1;
            if(p<=L&&p+lenb-i+1>len)
            {
                left=p;
                right=i;
                len=p+lenb-i+1;
            }

        }
        for(int i=1;i<=left;i++) printf("%c",b[i]);
        for(int i=right;i<=lenb;i++) printf("%c",b[i]);
        printf("\n");
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值