查找子字符串的个数(二分法查找)

来源:互联网 发布:ip什么意思网络用语 编辑:程序博客网 时间:2024/05/17 22:55

题目:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=28723#problem/D

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <stack>#include <cstdlib>#define LL long longusing namespace std;char str1[500020],str2[500020];bool check(int n){    int l1=strlen(str1);    int l2=strlen(str2);    int count=0,k=0;    for(int i=0; i<l2; i++)    {        if(str1[k]==str2[i])            count++;        if(count==n)        {            count=0;            k++;        }        if(k==l1)            return true;    }    return false;}int main(){    int n;    cin>>n;    while(n--)    {        cin>>str1>>str2;        int l1=strlen(str1);        int l2=strlen(str2);        int l=0,r=l2/l1;        while(r>l)        {            int m=l+(r-l)/2;            if(check(m))            {                l=m+1;            }            else            {                r=m;            }        }        if(check(l))        cout<<l<<endl;        else if(!check(l)&&l>0)        cout<<l-1<<endl;        else        cout<<"0"<<endl;    }    return 0;}


0 0