目录
KMP
#include#include #define R registerconst int MAXN=1000007;int l,l1;char s[MAXN],s1[MAXN];int fail[MAXN];inline void get_next(){ int p=0; for(R int i=2;i<=l;++i) { while(p>0&&s[i]!=s[p+1]) p=fail[p]; if(s[i]==s[p+1]) p++; fail[i]=p; }}inline void kmp(){ int p=0; for(R int i=1;i<=l1;++i) { while(p>0&&s[p+1]!=s1[i]) p=fail[p]; if(s[p+1]==s1[i]) p++; if(p==l) { printf("%d\n",i-l+1); p=fail[p]; } }}int main(){ scanf("%s%s",s1+1,s+1); l=strlen(s+1); l1=strlen(s1+1); get_next(); kmp(); for(int i=1;i<=l;++i) printf("%d ",fail[i]); return 0;}
字典树,trie树
#include#include #include using namespace std;int n;struct node { int next[27]; bool w;} a[300000];int tot;bool flag[300000];void build(char *b) { int p=0,len=strlen(b); for(int i=0; i
马拉车
#include#include #include using namespace std;const int maxn=11000007;char s[maxn],S[maxn];int p[maxn<<1];int main(){ scanf("%s",s); S[0]='@';S[1]='#'; int l=1,i=-1; while(s[++i]!='\0') S[++l]=s[i],S[++l]='#'; int mx=0,id=0,ans=0; for(int i=1;i<=l;++i) { if(mx>i) p[i]=min(p[id*2-i],mx-i);//id*2-i即i-k else p[i]=1; while(S[i+p[i]]==S[i-p[i]]) p[i]++; if(mx
AC自动机
#include#include #include #include #include #include #include using namespace std;const int N=2000100;struct Tree{ int fail,end; int vis[27]; }e[N]; int cnt,n;char s[N];inline int read() { int n=0,f=1;char ch=getchar(); while (ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();} while (ch<='9'&&ch>='0') {n=n*10+ch-'0';ch=getchar();} return n*f;}inline void insert(char *s) { int n=strlen(s); int now=0; for(int i=0;i