【CodeForces】779D

来源:互联网 发布:华为网络机顶盒密码 编辑:程序博客网 时间:2024/06/05 04:38

题目链接:这里写链接内容

这里写图片描述


题意:依次删除字符串的字符,直到a串不含b串。问最多删多少。


题解:
直接二分结果,用一个数组记录要删除的字符在第几个位置。


代码如下:

#include <cstdio>#include <cstring>#include <queue>#include <cmath>#include <stack>#include <vector>#include <algorithm>using namespace std;#define INF 0x3f3f3f3f#define CLR(a,b) memset(a,b,sizeof(a))#define PI acos(-1.0)#define LL long longchar str1[200000+5],str2[200000+5];int arr[200000+5];int l1,l2;bool check(int mid){    int pos1 = 1,pos2 = 1;    while (pos2 <= l2)    {        if (pos1 > l1)            return false;        if (arr[pos1] <= mid)        {            pos1++;            continue;        }        if (str1[pos1] == str2[pos2])        {            pos1++;            pos2++;        }        else            pos1++;    }    return true;}int main(){    scanf ("%s %s",str1+1,str2+1);    l1 = strlen(str1+1);    l2 = strlen(str2+1);    for (int i = 1 ; i <= l1 ; i++)    {        int t;        scanf ("%d",&t);        arr[t] = i;    }    int l = 0,r = l1-l2;    while (r >= l)    {        int mid = (l + r) >> 1;        if (check(mid))            l = mid+1;        else            r = mid-1;    }    printf ("%d\n",r);    return 0;}
0 0