# 【LOJ#2507】[CEOI2011]Matching（KMP，树状数组）

LOJ

## 题解

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
#define ll long long
#define MAX 1000100
{
int x=0;bool t=false;char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=true,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return t?-x:x;
}
vector<int> Ans;
int c[MAX],N,n,m,a[MAX],S[MAX],p[MAX],nt[MAX],rk[MAX];
int lb(int x){return x&(-x);}
int getsum(int x){int s=0;while(x)s+=c[x],x-=lb(x);return s;}
int main()
{
sort(&S[1],&S[m+1]);
for(int i=1;i<=m;++i)a[i]=lower_bound(&S[1],&S[m+1],a[i])-S;
memset(c,0,sizeof(c));
nt[0]=nt[1]=0;
for(int i=2,lst=2;i<=n;++i)
{
int t=nt[i-1];
while(t&&getsum(p[i])+1!=rk[t+1])
{
int v=nt[t];
t=v;
}
if(getsum(p[i])+1==rk[t+1])++t;
}
memset(c,0,sizeof(c));N=m;
for(int i=1,j=0,lst=1;i<=m;++i)
{
while(j&&getsum(a[i])+1!=rk[j+1])
{
int v=nt[j];
j=v;
}
if(getsum(a[i])+1==rk[j+1])++j;
if(j==n)
{
Ans.push_back(i-n+1);int v=nt[j];
}