Two strings

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.

题解:维护一个前缀,表示第 二 个字符串中 前 i 个字符在第一个字符串中 要到 a[i] 的长度才能全部找到;

同理维护一个后缀,然后二分查找即可;

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn=1e5+7;
#define inf 0x3f3f3f3f
char s1[maxn];
char s2[maxn];
int a[maxn];//前缀;
int b[maxn];//后缀;
int main()
{
    while(cin>>s1>>s2)
    {
        int len1=strlen(s1);
        int len2=strlen(s2);
        for(int i=1; i<=len2; i++)
            a[i]=b[i]=inf;
        a[0]=b[0]=0;
        int i,j;
        j=0;
        for(i=0; i<len2; i++)
        {
            while(j<len1)
            {
                if(s2[i]==s1[j])
                {
                    a[i+1]=j+1;
                    j++;
                    break;
                }
                else j++;
            }
            if(j>=len1) break;
        }
        j=0;
        for(i=0; i<len2; i++)
        {
            while(j<len1)
            {
                if(s2[len2-1-i]==s1[len1-1-j])
                {
                    b[i+1]=j+1;
                    j++;
                    break;
                }
                else j++;
            }
            if(j>=len1) break;
        }
        int sum=0,left=0,right=0;
        for(i=0; i<=len2; i++)
        {
            if(a[i]>len1) break;
            int l=0, r=len2, mid;
            while(l<=r)
            {
                mid=(l+r)/2;
                if(a[i]+b[mid]<=len1) l=mid+1;
                else r=mid-1;
            }
            if(l==0) l++;
            //cout<<i<<" "<<l-1<<endl;
            if(i+l-1>sum)
            {
                sum=i+l-1;
                left=i;
                right=l-1;
            }
        }
        //cout<<left<<" "<<right<<"    "<<sum<<endl;
        if(sum==0) printf("-\n");
        else if(left+right<len2)
        {

            for(i=0; i<left; i++)
                printf("%c",s2[i]);
            for(i=len2-right ; i<len2; i++)
                printf("%c",s2[i]);
            printf("\n");
        }
        else
            printf("%s\n",s2);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值