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

本文介绍了一种特殊的字符串匹配问题,即如何从字符串B中删除最少数量的连续字符,使得剩余部分成为字符串A的子序列。文章通过使用前缀和后缀数组记录匹配长度,并通过枚举确定最优解的方法来解决该问题。

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.


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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值