#include<iostream>
using namespace std;
const int HASH_SIZE=256;
int table[HASH_SIZE]; //存放查找文本的信息
void ShiftTable(char pattern[]) {
int m=strlen(pattern);
for(int i=0;i<HASH_SIZE;i++) //table所有元素初始化为m
table[i]=m;
for(int j=0;j<m-1;j++)
table[ pattern[j] ]=m-1-j;
}
int HorspoolMatching(char pattern[],char text[]) { //pattern为模式串;text为查找字符串
ShiftTable(pattern);
int m=strlen(pattern);
int n=strlen(text);
int i=m-1; //模式最右端位置
while(i <= n-1){
int k=0; //匹配字符的个数
while(k<=m-1 && pattern[m-1-k] == text[i-k] )
k++;
if(k==m)
return i-m+1;
else
i=i+table[ text[i] ]; //
}
return -1;
}
int main() {
char p[20]={"China"};
char t[1000]={"People's Republic of China,Welcome!"};
int pos = HorspoolMatching(p,t);
cout<<"\n"<<pos<<endl;
return 0;
}