#abc314c. C - Rotate Colored Subsequence

C - Rotate Colored Subsequence

Score : 300300 points

问题陈述

给定一个由小写英文字母组成的字符串 SS,长度为 NNSS 中的每个字符都涂有一种颜色,共有 MM 种颜色:颜色 11、颜色 22、...、颜色 MM;对于每个 i=1,2,,Ni = 1, 2, \ldots, NSS 的第 ii 个字符涂有颜色 CiC_i

按照顺序,对于每个 i=1,2,,Mi = 1, 2, \ldots, M,执行以下操作。

  • 对涂有颜色 iiSS 的部分执行一次向右的循环移动 11 位。也就是说,如果从左到右,第 p1p_1 个、第 p2p_2 个、第 p3p_3 个、\ldots、第 pkp_k 个字符涂有颜色 ii,那么同时将 SS 中的第 p1p_1 个、第 p2p_2 个、第 p3p_3 个、\ldots、第 pkp_k 个字符分别替换为 SS 中的第 pkp_k 个、第 p1p_1 个、第 p2p_2 个、\ldots、第 pk1p_{k-1} 个字符。

在上述操作完成后,打印最终的字符串 SS

约束条件保证 SS 中至少有一个字符涂有 MM 种颜色中的每种颜色。

以上为kimi 翻译,仅供参考。

Problem Statement

You are given a string SS of length NN consisting of lowercase English letters. Each character of SS is painted in one of the MM colors: color 11, color 22, ..., color MM; for each i=1,2,,Ni = 1, 2, \ldots, N, the ii-th character of SS is painted in color CiC_i.

For each i=1,2,,Mi = 1, 2, \ldots, M in this order, let us perform the following operation.

  • Perform a right circular shift by 11 on the part of SS painted in color ii. That is, if the p1p_1-th, p2p_2-th, p3p_3-th, \ldots, pkp_k-th characters are painted in color ii from left to right, then simultaneously replace the p1p_1-th, p2p_2-th, p3p_3-th, \ldots, pkp_k-th characters of SS with the pkp_k-th, p1p_1-th, p2p_2-th, \ldots, pk1p_{k-1}-th characters of SS, respectively.

Print the final SS after the above operations.

The constraints guarantee that at least one character of SS is painted in each of the MM colors.

Constraints

  • 1MN2×1051 \leq M \leq N \leq 2 \times 10^5
  • 1CiM1 \leq C_i \leq M
  • NN, MM, and CiC_i are all integers.
  • SS is a string of length NN consisting of lowercase English letters.
  • For each integer 1iM1 \leq i \leq M, there is an integer 1jN1 \leq j \leq N such that Cj=iC_j = i.

Input

The input is given from Standard Input in the following format:

NN MM

SS

C1C_1 C2C_2 \ldots CNC_N

Output

Print the answer.

Sample Input 1

8 3
apzbqrcs
1 2 3 1 2 2 1 2

Sample Output 1

cszapqbr

Initially, S=S = apzbqrcs.

  • For i=1i = 1, perform a right circular shift by 11 on the part of SS formed by the 11-st, 44-th, 77-th characters, resulting in S=S = cpzaqrbs.
  • For i=2i = 2, perform a right circular shift by 11 on the part of SS formed by the 22-nd, 55-th, 66-th, 88-th characters, resulting in S=S = cszapqbr.
  • For i=3i = 3, perform a right circular shift by 11 on the part of SS formed by the 33-rd character, resulting in S=S = cszapqbr (here, SS is not changed).

Thus, you should print cszapqbr, the final SS.

Sample Input 2

2 1
aa
1 1

Sample Output 2

aa

update @ 2024/3/10 08:58:01