串串亂記(長期

這裏貼板子, 近一週每天都要打截止那天早上已經會的。

知識筆記貼在:

note 1


KMP模板

#include<bits/stdc++.h>

using namespace std;

const int N = 1e6 + 233;

char s1[N], s2[N];
int l_1, l_2;
int fil[N];

int main()
{
	scanf("%s%s",s1+1,s2+1);
	l_1 = strlen(s1+1), l_2 = strlen(s2+1);
	fil[1]=0;
	for(int i=2,j=0; i<=l_2; ++i) {
		while(j && s2[j+1]!=s2[i]) j=fil[j];
		if(s2[j+1] == s2[i]) ++j;
		fil[i] = j;
	}
	for(int i=1,j=0; i<=l_1; ++i) {
		while(j && (j==l_2 || s2[j+1]!=s1[i])) j=fil[j];
		if(s2[j+1] == s1[i]) ++j;
		if(j==l_2) cout << (i-l_2+1) << '\n';
	}
	for(int i=1; i<=l_2; ++i) cout << fil[i] << ' ';
	return 0;
}

AC 自動機板子

struct acam{
	int tr[N][26], fil[N], fa[N], tot;
	void ins(char *s, int n)
	{
		int x=0;
		for(int i=0 i<n; ++i) {
			char  ch = s[i] - 'a';
			if(!tr[x][ch]) {
				int y = ++tot;
				tr[x][ch] = y;
				fa[y] = x;
			}
			x = tr[x][ch];
		}
	}
	void get_fail()
	{
		queue<int>q;
		for(int i=0; i<26; ++i)
			if(tr[0][i]) q.push(tr[0][i]), fil[tr[0][i]] = 0;
		while(!q.empty())
		{
			int x = q.front(); q.pop();
			for(int i=0; i<26; ++i)
				if(tr[x][i]) fil[tr[x][i]] = tr[fil[x]][i], q.push(tr[x][i]);
				else tr[x][i] = tr[fil[x]][i];
		}
	}
} A;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章