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;
}