生活随笔
收集整理的這篇文章主要介紹了
回文树、回文自动机
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
回文樹可以求回文串的一些問題
空間復雜度OOO(NNN× 字符集)
時間復雜度OOO(NNN× logloglog(字符集))
- 本質不同的回文串的個數:p
- 每種回文串的長度 len[i]
- 每種回文串出現的次數 cnt[i]
- 以某個字符為右端點的回文串的個數 num[i]
- 各種變形
struct Palindrome_Tree
{int nex
[maxn
][26];int fail
[maxn
], cnt
[maxn
], num
[maxn
]; int len
[maxn
], S
[maxn
]; int last
, n
, p
;int newnode(int l
) { for (int i
= 0; i
< 26; ++i
) nex
[p
][i
] = 0;cnt
[p
] = num
[p
] = 0;len
[p
] = l
;return p
++;}void init() { p
= 0;newnode(0), newnode(-1); last
= n
= 0;S
[n
] = -1; fail
[0] = 1; }int get_fail(int x
) { while (S
[n
- len
[x
] - 1] != S
[n
]) x
= fail
[x
];return x
;}void add(int c
) { c
-= 'a';S
[++n
] = c
;int cur
= get_fail(last
);if (!nex
[cur
][c
]) {int now
= newnode(len
[cur
] + 2);fail
[now
] = nex
[get_fail(fail
[cur
])][c
];nex
[cur
][c
] = now
;num
[now
] = num
[fail
[now
]] + 1;}last
= nex
[cur
][c
];cnt
[last
]++;}void count() { for (int i
= p
- 1; i
>= 0; --i
) cnt
[fail
[i
]] += cnt
[i
];}
}Tree
;
總結
以上是生活随笔為你收集整理的回文树、回文自动机的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。