CodeForces 625B War of the Corporations

暴力匹配+一点判断

#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <queue>
#include <stack>
#include <map>
#include <vector>
using namespace std;

const int maxn=100000+50;
char s[maxn];
char t[35];
struct Seg
{
    int l,r;
    Seg(int a,int b){l=a;r=b;}
};
vector<Seg>v;
int L,R;

bool CMP(int pos)
{
    bool flag=1;

    for(int i=0;t[i];i++)
    {
        if(t[i]!=s[i+pos]) {flag=0;break;}
    }
    return flag;
}

int main()
{
    scanf("%s",s);
    scanf("%s",t);
    v.clear();

    int lens=strlen(s);
    int lent=strlen(t);

    for(int i=0;i<lens;i++)
    {
        if(CMP(i))
        {
            Seg seg(i,i+lent-1);
            v.push_back(seg);
        }
    }
    int ans;
    if(v.size()==0) ans=0;
    else
    {
        ans=1;
        L=v[0].l,R=v[0].r;
        for(int i=1;i<v.size();i++)
        {
            if(v[i].l<=R) L=v[i].l;
            else
            {
                ans++;
                L=v[i].l;
                R=v[i].r;
            }
        }
    }
    printf("%d
",ans);
    return 0;
}
原文地址:https://www.cnblogs.com/zufezzt/p/5188491.html