#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
#include<string.h>
using namespace std;
char pattern[10000],str[10000];
int last(char *p,char ch)
{
int length=strlen(p),num=0;
char *pp=p+length+1;
while(pp>=p)
{
if(*pp==ch)
{
return length-num-1;
}
pp--;
num++;
}
return -1;
}
int bminndex(char *s,char *p)
{
int n=strlen(s);
int m=strlen(p);
int i=m-1,j=m-1;
while(i<=n-1)
{
if(s[i]==p[j])
{
if(j==0)
return i;
else
{
i--;
j--;
}
}
else
{
i=i+m-min(j,i+last(p,s[i]));
j=m-1;
}
}
return -1;
}
int sum(char *s,char *p,int m)
{
int n=bminndex(s+m,p);
return n==-1?0:1+sum(s,p,n+m+1);
}
int main()
{
while(gets(str))
{
printf("%s\n",sum(str,"bkpstor",0)?"warning":"safe");
}
return 0;
}
BM算法
最新推荐文章于 2017-04-20 18:49:28 发布