Custom Sort String

原题链接

S and T are strings composed of lowercase letters. In S, no letter occurs more than once.

S was sorted in some custom order previously. We want to permute the characters of T so that they match the order that S was sorted. More specifically, if x occurs before y in S, then x should occur before y in the returned string.

Return any permutation of T (as a string) that satisfies this property.

Example :
Input:
S = “cba”
T = “abcd”
Output: “cbad”
Explanation:
“a”, “b”, “c” appear in S, so the order of “a”, “b”, “c” should be “c”, “b”, and “a”. Since “d” does not appear in S, it can be at any position in T. “dcba”, “cdba”, “cbda” are also valid outputs.

题意:字符串S中重新规定了字母的先后顺序,要求你按照该顺序重新给字符串T排序

其实是个很简单的字符串排序问题(写排序规则),用一个数组$reorder$记录新的顺序,排序规则中按照该数组的大小关系排序即可

但是在写匿名函数的时候,如何引用这个外部数组成了问题,所以学习了一下

c++的匿名函数是在c++11标准中才支持的,语法是

中括号中的capture参数就是允许变量被按值或按引用捕获,规则如下:

那么我们想要引入reorder到排序规则的匿名函数中,只需要中括号中写上&或=即可

代码如下:

发表评论

电子邮件地址不会被公开。