该算法具体如下:对一个长度为 n 的字符串 S,首先根据它构造 n 个字符串,其中第 i 个字符串由将 S 的前 i−1 个字符置于末尾得到。然后把这 n 个字符串按照首字符从小到大排序,如果两个字符串的首字符相等,则按照它们在 S 中的位置从小到大排序。排序后的字符串的尾字符可以组成一个新的字符串 S′,它的长度也是 n,并且包含了 S 中的每一个字符。最后输出 S′ 以及 S 的首字符在 S′ 中的位置 p。
举例:S 是 example
构造 n 个字符串。
1 2 3 4 5 6 7
example xamplee ampleex mpleexa pleexam leexamp eexampl
将字符串排序。
1 2 3 4 5 6 7
ampleex example eexampl leexamp mpleexa pleexam xamplee
n=int(input())#字符串长度 s=input()#压缩串 pos=int(input())#压缩串中首字母位置 pos=pos-1 box=[]#压缩串得到列表 for i inrange(n): box.append(s[i]) boxs=box.copy() boxs.sort()#字典序列表 res=[] for i inrange(0,n): if boxs[i]==box[pos]: pos=i boxs[i]='0' res.append(box[pos]) break
for j inrange(1,n): for i inrange(n-1,-1,-1): if boxs[i]==box[pos]: pos=i res.append(box[pos]) boxs[i]='0' break
ans="" res.reverse() for each in res: ans=ans+each print(ans)