intmain(){ int n; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", v + i); int i = 0, j = 1, k = 0; while (i < n && j < n && k < n) { if (v[(i + k) % n] == v[(j + k) % n]) k++; else { v[(i + k) % n] < v[(j + k) % n] ? j += k + 1 : i += k + 1; k = 0; if (i == j) j++; } } int ans = min(i, j); for (int x = 0; x < n; x++) printf("%d ", v[(ans + x) % n]); }
voidsort(int* sa, int* rk, int* tp, int n, int m){ staticint s[N]; for (int i = 0; i < m; i++) s[i] = 0; //清空 for (int i = 0; i < n; i++) s[rk[i]]++; //计数 for (int i = 1; i < m; i++) s[i] += s[i - 1]; //前缀和 for (int i = n - 1; i >= 0; i--) sa[--s[rk[tp[i]]]] = tp[i]; //按tp枚举排序 }
voidgetsa(int* sa, int* s, int n, int m){ // s[i]<m i<n staticint rk[N], tp[N]; // rk和sa相对,tp是枚举顺序 for (int i = 0; i < n; i++) rk[i] = s[i]; for (int i = 0; i < n; i++) tp[i] = i; // tp是枚举顺序 sort(sa, rk, tp, n, m); // 第1轮排序 for (int k = 1; k <= n; k <<= 1) { // k 是已经排序完成的后缀长度 for (int i = 0; i < k; i++) tp[i] = n - 1 - i; // 短后缀的第二关键字为空,放到最前面 for (int t = k, i = 0; i < n; i++) if (sa[i] >= k) tp[t++] = sa[i] - k; // 按照第二关键字排好序
sort(sa, rk, tp, n, m); for (int i = 0; i < n; i++) tp[i] = rk[i]; //拷贝一份 rk[sa[0]] = 0; for (int i = 1; i < n; i++) { int x = sa[i], y = sa[i - 1]; if (tp[x] == tp[y] && x + k < n && y + k < n && tp[x + k] == tp[y + k]) rk[x] = rk[y]; else rk[x] = rk[y] + 1; } } }
intmain(){ int n; scanf("%d", &n); vector<int> v(n); for (int i = 0; i < n; i++) scanf("%d", &v[i]); vector<int> dist = v; sort(dist.begin(), dist.end()); dist.erase(unique(dist.begin(), dist.end()), dist.end()); for (int& x : v) x = lower_bound(dist.begin(), dist.end(), x) - dist.begin(); for (int i = 0; i < n; i++) v.push_back(v[i]); vector<int> sa(v.size()); getsa(sa.data(), v.data(), v.size(), v.size()); int ans = 0; while (sa[ans] >= n) ans++; for (int i = 0; i < n; i++) { printf("%d ", dist[v[sa[ans] + i]]); } }
constint N = 6e5 + 5; int par[N << 1], len[N << 1]; map<int, int> son[N << 1]; int p, tot;
voidextend(int c){ // s[i]-'a' int np = ++tot; par[np] = 0; len[np] = len[p] + 1; while (son[p][c] == 0) { son[p][c] = np; p = par[p]; } if (p != 0 || son[p][c] != np) { int q = son[p][c]; par[np] = q; if (len[q] != len[p] + 1) { int nq = ++tot; son[nq] = son[q]; par[nq] = par[q]; len[nq] = len[p] + 1; par[np] = par[q] = nq; while (son[p][c] == q) { son[p][c] = nq; p = par[p]; } } } p = np; }
intmain(){ ios::sync_with_stdio(false); int n;cin>>n; vector<int>s(n); for(int i=0;i<n;i++) cin>>s[i]; for (int x : s) extend(x); for (int x : s) extend(x);
for (int i = 0, cur = 0; i < s.size(); i++) { auto x =son[cur].begin(); cout<<x->first<<" "; cur=x->second; } }