标签:har code operation bsp blog turn += fine can
void Manacher(){ for (int i=0;t[i];++i,len+=2){ s[i<<1]=‘#‘; if (t[i]>=‘A‘&&t[i]<=‘Z‘) s[i<<1|1]=t[i]-‘A‘+‘a‘; else s[i<<1|1]=t[i]; } s[len++]=‘#‘; int max_r=0,pos=0; for (int i=0;i<len;++i){ if (max_r>i) r[i]=min(max_r-i,r[2*pos-i]); else r[i]=0; while (i+r[i]+1<len&&i-r[i]-1>=0&&s[i+r[i]+1]==s[i-r[i]-1]) r[i]++; if (r[i]+i>max_r){ max_r=r[i]+i; pos=i; } } } //马拉车
#define N 1010 char s[N],p[N]; int nxt[N]; void get_nxt(char * p){ int i=0,j=-1; nxt[i]=j; while (p[i]){ if (j==-1||p[j]==p[i]) nxt[++i]=++j; else j=nxt[j]; } } int kmp(char *s,char *p){ get_nxt(p); int i=0,j=0,ret=0; for (;s[i];++i){ while (j!=-1&&s[i]!=p[j]) j=nxt[j]; j++; if (!p[j]){ ret++; j=next[j]; //operations } } return ret; } int main(){ scanf("%s%s",s,p); printf("%d\n",kmp(s,p)); return 0; }
//KMP算法
标签:har code operation bsp blog turn += fine can
原文地址:http://www.cnblogs.com/ZDHYXZ/p/7635780.html