HBC209390SorttheStringsRevision题解

回忆凄美了谁 算法基础篇 45 0
挑战自我,勇攀编程高峰!全网最全C++题库,助您成为编程达人。
. The characters in the string are numbered from 0 to n-1 .The character i of. 0n1 :p[0], p[1], ..., p[n-1] and a sequence of digits of length n : d[0], d[1], ..., d[n-1].with digit d[i]. Note that if d[i] is as same as thecharacterp[i] of. Now we want to sort these n+1 strings. The string with index i should be placed to the left of string with index j if and only if. is the i-th string from the left after sorting. . For example, if n = 5, p = [4, 3, 0, 1, 2] and d = [5, 0, 0, 0, 0], then

There are n+1 strings of length n --- s_0, s_1, ldots, s_n s ​ ,s 1 ​ ,…,s n ​ . The characters in the string are numbered from 0 to n-1 (from left to right). The character i of s_0 s ​ is the digit i modulo 10. For example, if n = 5, s_0 s ​ is "01234", and if n=14, s_0 s ​ is "01234567890123". You are given a permutation of 0 sim n-1 0∼n−1 : p[0], p[1], ..., p[n-1] and a sequence of digits of length n : d[0], d[1], ..., d[n-1]. s_{i+1} s i+1 ​ can be obtained by replacing character p[i] of s_i s i ​  with digit d[i]. Note that if d[i] is as same as the character p[i] of s_i s i ​ , then s_{i+1} s i+1 ​ will be the same as s_i s i ​ . Now we want to sort these n+1 strings. The string with index i should be placed to the left of string with index j if and only if s_i s i ​ is lexicographically smaller than s_j s j ​ , or s_i s i ​ equal to s_j s j ​ and i < j. Let r_i r i ​ be the new position of string s_i s i ​ from the left, that is, S_{r_i} S r i ​ ​  is the i-th string from the left after sorting. (Positions are number start from 0.) For example, if n = 5, p = [4, 3, 0, 1, 2] and d = [5, 0, 0, 0, 0], then s_0 = s ​ = "01234", s_1 = s 1 ​ = "01235", s_2 = s 2 ​ = "01205", s_3 = s 3 ​ = "01205", s_4 = s 4 ​ = "00205", s_5 = s 5 ​ = "00005". So r_0 = 4 r ​ =4, r_1 = 5 r 1 ​ =5, r_2 = 2 r 2 ​ =2, r_3 = 3 r 3 ​ =3, r_4 = 1 r 4 ​ =1, and r_5 = 0 r 5 ​ =0. Please calculate all r_i r i ​ for i from 0 to n.

HBC209390SorttheStringsRevision题解
-第1张图片-东莞河马信息技术
(图片来源网络,侵删)
成为编程大师,不再是梦想!全网最全C++题库,助您开启编程新篇章。

标签: HBC209390SorttheStringsRevision题解