题目大意
题目分析
根据题目给出的末尾,可以得到整个串的元素组成。
不难想到可以通过字典序排序得到排好序的串的首位字符。
因此将题目给出的末尾字符编号,将其按照字符作为第一关键词,编号作为第二关键词重排序,得到的就是排好序的串的首位字符(第二关键词排序的原因类似后缀平衡树的思想,通过已经有的后面位字符确定后面的字典序大小)。
因此我们得到了每个串的开始位置与结束位置:
考虑将原串扩展两倍。
$AB\mid CAAA*AB\mid CAAA*$
发现每个末尾字符($B$)对应的是开头字符($C$),也正是其下一个字符开头串的字典序。
故我们即可从首字母开始向后找到下一个字符串迭代直到结束。
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| #include<algorithm> #include<iostream> #include<iomanip> #include<cstring> #include<cstdlib> #include<climits> #include<vector> #include<cstdio> #include<cmath> #include<queue> using namespace std;
inline const int Get_Int() { int num=0,bj=1; char x=getchar(); while(x<'0'||x>'9') { if(x=='-')bj=-1; x=getchar(); } while(x>='0'&&x<='9') { num=num*10+x-'0'; x=getchar(); } return num*bj; }
const int maxn=200005;
struct sequence { int x,id; bool operator < (const sequence& b) const { return x<b.x||(x==b.x&&id<b.id); } } a[maxn];
int n;
int main() { n=Get_Int(); Get_Int(); for(int i=0; i<=n; i++) { a[i].x=Get_Int(); a[i].id=i; } sort(a,a+n+1); int now=a[0].id; for(int i=1; i<=n; i++) { printf("%d ",a[now].x); now=a[now].id; } return 0; }
|