这题我用 cin/cout T了,本机测试时会 RE,然而换成scanf/printf 就过了,本机也没有 RE,这是为什么?求助大佬。(最大点时间差了近30秒,应该不是输入输出效率问题)
代码:
/*他说他是乱打的
md, 写的这么 nb, wc
-- 余华
*/
#include <bits/stdc++.h>
using namespace std;
int bucket[1000010], s[1000010], tmp[1000010];
int sa[1000010];
enum type_t {
L = 0, S
};
inline bool isLMS(type_t *type, int p) {
return (p > 0 && type[p] == S && type[p - 1] == L);
}
template<class T>
inline bool sameLMS(T st, type_t *type, int p, int q) {
if(p == -1 || q == -1) return 0;
int k = -1;
while(1) {
//cout << p << " " << q << endl;
k++;
if(st[p + k] != st[q + k] || type[p + k] != type[q + k]) return 0;
if(k == 0) continue;
if(isLMS(type, p + k) != isLMS(type, q + k)) return 0;
if(isLMS(type, p + k)) break;
}
return 1;
}
template<class T>
inline void induced_sort(T st, int len, int sigma, type_t *type, int *LMS, int LMSsize) {
//cout << len << endl;
memset(bucket, 0, sizeof(int) * sigma);
memset(sa, -1, sizeof(int) * len);
for(int i = 0; i < len; i++) ++bucket[st[i]];
s[0] = bucket[0];
for(int i = 1; i < sigma; i++) s[i] = s[i - 1] + bucket[i];
for(int i = LMSsize - 1; i >= 0; i--) sa[--s[st[LMS[i]]]] = LMS[i];
s[0] = 0;
for(int i = 1; i < sigma; i++) s[i] = s[i - 1] + bucket[i - 1];
for(int i = 0; i < len; i++) {
if(sa[i] <= 0 || type[sa[i] - 1] != L) continue;
sa[s[st[sa[i] - 1]]++] = sa[i] - 1;
}
s[0] = bucket[0];
for(int i = 1; i < sigma; i++) s[i] = s[i - 1] + bucket[i];
for(int i = len - 1; i >= 0; i--) {
if(sa[i] <= 0 || type[sa[i] - 1] != S) continue;
sa[--s[st[sa[i] - 1]]] = sa[i] - 1;
}
}
template<class T>
inline void sais(T st, int len, int sigma, int *LMS) {
//cout << 1 << endl;
//cout << len << endl;
type_t *type = new type_t[len];
type[len - 1] = S;
for(int i = len - 2; i >= 0; i--) {
if(st[i] < st[i + 1]) type[i] = S;
else if(st[i] > st[i + 1]) type[i] = L;
else type[i] = type[i + 1];
}
// cout << "st: "; for(int i = 0; i < len; i++) cout << st[i] << " ";
// puts("");
// cout << "type: ";for(int i = 0; i < len; i++) cout << type[i] << " ";
// puts("");
int LMSsize = 0;
for(int i = 0; i < len; i++)
if(isLMS(type, i)) LMS[LMSsize++] = i;
induced_sort(st, len, sigma, type, LMS, LMSsize);
memset(tmp, 0, sizeof(tmp[0]) * len);
int pre = -1;
int cnt = 0;
//cout << 1 << endl;
//cout << len << endl;
for(int i = 0; i < len; i++) {
if(!isLMS(type, sa[i])) continue;
if(!sameLMS(st, type, pre, sa[i])) ++cnt;
tmp[sa[i]] = cnt - 1;
pre = sa[i];
}
// cout << "sa: "; for(int i = 0; i < 5; i++) cout << sa[i] << " ";
// puts("");
// cout << "tmp: "; for(int i = 0; i < len; i++) cout << tmp[i] << " ";
// puts("");
int *s1 = new int[LMSsize];
LMSsize = 0;
for(int i = 0; i < len; i++) {
if(isLMS(type, i)) s1[LMSsize++]= tmp[i];
}
// cout << "s1: ";for(int i = 0; i < LMSsize; i++) cout << s1[i] << " ";
// puts("");
int *newLMS = new int[LMSsize];
//cout << cnt << " " << LMSsize << endl;
if(cnt < LMSsize) sais(s1, LMSsize, cnt, newLMS);
else for(int i = 0; i < LMSsize; i++) sa[s1[i]] = i;
delete[] s1;
for(int i = 0; i < LMSsize; i++) newLMS[i] = LMS[sa[i]];
induced_sort(st, len, sigma, type, newLMS, LMSsize);
delete[] newLMS;
delete[] type;
}
template<class T>
inline void init(T st, int len, int sigma) {
int *LMS = new int[len];
sais(st, len + 1, sigma, LMS);
//cout << 1 << endl;
delete[] LMS;
for(int i = 0; i < len; i++) {
sa[i] = sa[i + 1] + 1;
}
}
char st[1000010];
int main() {
//freopen("text.in", "r", stdin);
//freopen(".out", "w", stdout);
//ios::sync_with_stdio(false);
scanf("%s", st);
int len = strlen(st);
init(st, len, 128);
for(int i = 0; i < len; i++) printf("%d ", sa[i]);
puts("");//cout << endl;
return 0;
/*
*/
}
/*后记
无
*/