Codeforces 762C Two strings (前缀与后缀)

来源:互联网 发布:汽车行业概况知乎 编辑:程序博客网 时间:2024/05/18 14:43

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
hibob
output
-
input
abcaaccepted
output
ac
input
abacabaabcdcba
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.


123


不得不说CF的 题目 真是又爱又恨啊,  第一遍读上去,感觉像 公共子序列的问题,  ,看第二个示例,又不是,  晕啊晕,  

题目大意:     给你A 串 B 串  问 在 B串中删除x连续的串后,  剩下的 连在一起是A的子序列,

注意:  A 可以不连续,   只要B剩下的字符, A里面有就可以


用两个数组,  一个前缀,一个后缀,   记录 A串中 B的子序列长度,   然后 枚举A   从中选取 删除后缀减去前缀的部分;

选个最小的;    题目是 最小连续的B子串

 注意 赋初值的问题 以及 后缀最大的 要存最大值; 是为了删除串的作用

#include <iostream>#include <map>#include <queue>#include <string>#include <algorithm>#include <cmath>#include <vector>#include <cstring>#include <stdio.h>using namespace std;typedef long long ll;const int MAXN=1e5+155;int pre[MAXN],suf[MAXN];//存前缀和后缀    相当于存 A的  对于B的子序列的长度char a[MAXN],b[MAXN];int main(){    while(~scanf("%s%s",a+1,b+1))    {        int len1=strlen(a+1);        int len2=strlen(b+1);        int last=0;        // 不论是前缀还是后缀,  只要A 中有B的 某些串就可以 可以不连续,  例如A :abc   B: ac   B为A的子串        for(int i=1;i<=len1;i++)  //赋初值            pre[i]=len2,suf[i]=1;        suf[len1+1] = len2+1;// 必须有 目的是为了删除子串的作用, pre[i-1]        for(int i=1,j=1;i<=len1&&j<=len2;i++)  //这里是构造A的前缀        {            if(a[i]==b[j])// 相同记录前缀, 更新            {                pre[i]=j;                last=j;                j++;            }            else                pre[i]=last;  //如果这个不相同,就等于前面的last        }        last=len2+1;        for(int i=len1,j=len2;i>=1&&j>=1;i--)//构造A的后缀        {            if(a[i]==b[j])            {                suf[i]=j;                last =j;                j--;            }            else                suf[i]=last;        }        int ans=len2+1, left=1,right=len2+1; //初始化答案为最大;        for(int i=1;i<=len1+1;i++)//枚举所有的情况        {            if(pre[i-1]<suf[i]&&ans>suf[i]-pre[i-1])            {                ans=suf[i]-pre[i-1];                left=pre[i-1];                right=suf[i];            }        }        if(ans== len2+1 )        {            cout<<"-"<<endl;            continue;        }        for(int i=1;i<=len2;i++)// 输出 删除后的b        {            if(i<=left||i>=right)                cout<<b[i];        }    }    return 0;}