AC自动机模板先存着,还不会...
来源:互联网 发布:甬温动车事故 知乎 编辑:程序博客网 时间:2024/06/09 14:58
//my first ACAutomaton# include <set># include <map># include <list># include <queue># include <stack># include <cmath># include <string># include <cstdio># include <vector># include <cstdlib># include <cstring># include <iostream># include <algorithm>using namespace std ;# define ll long long# define uint unsigned int# define ull unsigned long long# define PB push_back# define SIZE(x) (int)x.size()# define clr(x,y) memset(x,y,sizeof(x))# define MP(x,y) make_pair(x,y)# define read(n) scanf ( "%d" , & (n) )# define reads(n) scanf ( "%s" , n ) ;# define ALL(t) (t).begin(),(t).end()# define FOR(i,n,m) for ( int i = n ; i <= m ; i ++ )# define IT ::iterator# define vint vector<int># define vstring vector<string># define code(x) (x-'a')const ll mod = 1e9+7 ;const ll LMAX = 1e18 ;const int MAX = 1e9 ;const double EPS = 1e-8;const int MAX_NODE = 500005;const int CHILD_NUM = 26;int ans;class ACAutomaton {private: int chd[MAX_NODE][CHILD_NUM]; int val[MAX_NODE]; int fail[MAX_NODE]; int sz ;public: void init() { memset(chd[0] , 0 , sizeof(chd[0])); sz = 1; } void insert(char *s,int key) { int p = 0; for ( ; *s ; s ++) { int c = code (*s); if (!chd[p][c]) { memset(chd[sz] , 0 , sizeof(chd[sz])); val[sz] = 0; chd[p][c] = sz ++; } p = chd[p][c]; } val[p] += key; //键值 } void getfail() { queue < int > q ; for (int i = 0 ; i < CHILD_NUM ; i ++) { if (chd[0][i]) { fail[ chd[0][i] ] = 0; q.push (chd[0][i]); } } while ( !q.empty() ) { int u = q.front(); q.pop(); for (int i = 0 ; i < CHILD_NUM ; i ++) { int v = chd[u][i]; if (v) { q.push (v); int tmp = fail[u]; while (tmp && !chd[tmp][i]) tmp = fail[tmp]; if (chd[tmp][i]) { tmp = chd[tmp][i]; } fail[v] = tmp; } } } } void find(char* t) { int len = strlen (t) ,p = 0; for (int i = 0 ; i < len ; i ++) { int c = code (t[i]); while (p && !chd[p][c]) { p = fail[p]; } if (chd[p][c]) { p = chd[p][c]; int tmp = p; while (tmp && val[tmp] != -1) { ans += val[tmp]; val[tmp] = -1; tmp = fail[tmp]; } } } }} ac;char s[55] ,t[1000005];int main() { /*int T; read (T); while (T--) { ac.init(); ans = 0; int n; read (n); while (n--) { reads (s); ac.insert(s,1); } ac.getfail(); reads (t); ac.find(t); cout << ans << endl ; } */ int a,b; read(a),read(b); cout << a + b << endl ;}