萌新求助:程序厌氧了怎么办

P3804 【模板】后缀自动机(SAM)

![](//图.tk/j) ```cpp #include <bits/stdc++.h> using namespace std; class Samfun { protected: enum { lettersetsize = 26, maxsize = 1000000 }; #define foreach for (int i = 0; i < cnt; i++) #define foreach1 for (int i = 1; i < cnt; i++) #define foreachletter for (int i = 0; i < lettersetsize; i++) #define modify -'a' #define modified -= 'a' public: struct node { int len, link, next[26], firstpos, endpos, id; bool isfinal = false; node(int l = 0, int k = 0, int *n = nullptr, int f = 0, int d = -1) { len = l, link = k, firstpos = f, endpos = 0, id = d; if (n == nullptr) memset(next, 0, sizeof next); else foreachletter next[i] = n[i]; } node(const node &n) { len = n.len, link = n.link, firstpos = n.firstpos, endpos = n.endpos, id = n.id; foreachletter next[i] = n.next[i]; } }; protected: node value[maxsize << 1]; int cnt = 0, last = 0; #define len(x) (value[(x)].len) #define link(x) (value[(x)].link) #define linkto(x, y) (link(x) = (y)) #define next(x) (value[(x)].next) #define nextat(x, y) (next(x)[(y)]) #define nextto(x, y, z) (nextat(x, y) = (z)) #define fir(x) (value[(x)].firstpos) #define endpos(x) (value[(x)].endpos) #define id(x) (value[(x)].id) #define final(x) (value[(x)].isfinal) #define update(x) (x = link(x)) #define at(x) (value[(x)]) #define root 0 inline int newnode(int l = 0, int k = 0, int *n = nullptr, int f = 0) { value[cnt] = node(l, k, n, f, cnt); return cnt++; } public: Samfun() { newnode(0, -1); } int push_back(char ch) { ch modified; int now = newnode(len(last) + 1, 0, nullptr, len(last)), father = last; for (father; ~father && !nextat(father, ch); nextto(father, ch, now), update(father)) ; if (~father) { int son = nextat(father, ch); if (len(son) == len(father) + 1) linkto(now, son); else { int copy = newnode(len(father) + 1, link(son), next(son), fir(son)); for (father; ~father && nextat(father, ch) == son; nextto(father, ch, copy), update(father)) ; linkto(now, copy), linkto(son, copy); } } return last = now, final(now) = true, endpos(now) = 1, cnt; } int push_back(string s) { for (auto i : s) push_back(i); return cnt; } bool is_in(string s) { for (int i = 0, j = 0; i < s.size(); j = nextat(j, s[i++] modify)) if (!nextat(j, s[i] modify)) return false; return true; } int longest_prefix_in(string s) { int ans = 0; for (int i = 0, j = 0; i < s.size(); j = nextat(j, s[i++] modify), ans++) if (!nextat(j, s[i] modify)) return ans; return ans; } int first_in(string s) { int ans = 0, j = 0; for (int i = 0; i < s.size(); j = nextat(j, s[i++] modify), ans++) if (!nextat(j, s[i] modify)) return -1; return fir(j) - s.size() + 1; } int substr_count() { int ans = 0; foreach1 ans += len(i) - len(link(i)); return ans; } int substr_lensum() { int ans = 0; foreach1 ans += len(i) * (len(i) + 1) / 2 - len(link(i)) * (len(link(i)) + 1) / 2; return ans; } int dfs_endpos() { node sv[cnt]; foreach sv[i] = at(i); sort(sv, sv + cnt, [](node &a, node &b) { return a.len > b.len; }); foreach sv[i].id ? (endpos(link(sv[i].id)) += endpos(sv[i].id)) : 0; } int ep(int x) { return endpos(x); } int in_times(string s) { int ans = 0, j = 0; for (int i = 0; i < s.size(); j = nextat(j, s[i++] modify), ans++) if (!nextat(j, s[i] modify)) return 0; return endpos(j); } int P3804() { int ans = 0; foreach1 ans = max(ans, endpos(i) * len(i)); return ans; } }; int main() { Samfun a; string s; cin >> s; a.push_back(s); a.dfs_endpos(); cout << a.P3804() << endl; } ```
by TensorFlow_js @ 2022-07-03 20:09:23


另:`出现次数不为 1` 还未加上。
by TensorFlow_js @ 2022-07-03 20:11:36


@[Suruka](/user/321566) 毒氧,正常现象
by Lai_Da_Ji @ 2022-07-03 20:12:33


@[dpkajj](/user/515930) 这题强制开 O2 ![](//图.tk/i)![](//图.tk/h)
by TensorFlow_js @ 2022-07-03 20:12:55


@[Suruka](/user/321566) 可以试试把内存压一压
by Lai_Da_Ji @ 2022-07-03 20:14:20


@[dpkajj](/user/515930) `Runtime Error. Received signal 7: Bus error (bad memory access).`
by TensorFlow_js @ 2022-07-03 20:17:08


@[Suruka](/user/321566) 对不起,我不太擅长处理这种问题,你可以另请高人 @[S11EDG](/user/186045)
by Lai_Da_Ji @ 2022-07-03 20:21:41


oK
by TensorFlow_js @ 2022-07-03 20:23:11


@[dpkajj](/user/515930) 别说正常现象了,到时候到 NOI 决定命运的时候就爆零了
by liqingyang @ 2022-07-03 20:23:21


@[Suruka](/user/321566) 提供几种可能:函数有非void返回值但最终没有return,变量不是全局但没赋初值
by liqingyang @ 2022-07-03 20:26:41


| 下一页