_bzoj1031 [JSOI2007]字元加密Cipher【尾碼數組】

來源:互聯網
上載者:User

標籤:class   com   return   pre   string   允許   can   memset   http   

傳送門:http://www.lydsy.com/JudgeOnline/problem.php?id=1031

儲存尾碼數組模版。

其實如果資料範圍小一點,或者空間限制再大一點,或者甚至只要字母表再小一點就可以使用尾碼自動機了,只可惜空間不允許,就用尾碼數組!

其實還是不是很理解代碼,是否該當成黑盒代碼背下來呢?

#include <cstdio>#include <cstring>#include <algorithm>const int maxn = 200005;int n, mx, sa[maxn], t1[maxn], t2[maxn], c[maxn], *x, *y;char s[maxn];int main(void) {scanf("%s", s);n = strlen(s);memcpy(s + n, s, n - 1);n = (n << 1) - 1;x = t1;y = t2;mx = 128;memset(c, 0, sizeof c);for (int i = 0; i < n; ++i) {++c[x[i] = s[i]];}for (int i = 1; i < mx; ++i) {c[i] += c[i - 1];}for (int i = n - 1; ~i; --i) {sa[--c[x[i]]] = i;}int p;for (int k = 1; k <= n; k <<= 1) {p = 0;for (int i = n - k; i < n; ++i) {y[p++] = i;}for (int i = 0; i < n; ++i) {if (sa[i] >= k) {y[p++] = sa[i] - k;}}memset(c, 0, sizeof c);for (int i = 0; i < n; ++i) {++c[x[y[i]]];}for (int i = 0; i < mx; ++i) {c[i] += c[i - 1];}for (int i = n - 1; ~i; --i) {sa[--c[x[y[i]]]] = y[i];}std::swap(x, y);p = 1;x[sa[0]] = 0;for (int i = 1; i < n; ++i) {x[sa[i]] = y[sa[i - 1]] == y[sa[i]] && y[sa[i - 1] + k] == y[sa[i] + k]? p - 1: p++;}if (p >= n) {break;}mx = p;}int tem = (n + 1) >> 1;for (int i = 0; i < n; ++i) {if (sa[i] < tem) {printf("%c", s[sa[i] + tem - 1]);}}return 0;}

  

_bzoj1031 [JSOI2007]字元加密Cipher【尾碼數組】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.