CF1506G.Maximize the Remaining String

普及+/提高

通过率:0%

时间限制:2.50s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a string ss, consisting of lowercase Latin letters. While there is at least one character in the string ss that is repeated at least twice, you perform the following operation:

  • you choose the index ii (1≤i≤∣s∣1 \le i \le |s|) such that the character at position ii occurs at least two times in the string ss, and delete the character at position ii, that is, replace ss with s1s2…si−1si+1si+2…sns_1 s_2 \ldots s_{i-1} s_{i+1} s_{i+2} \ldots s_n.

For example, if s=s="codeforces", then you can apply the following sequence of operations:

  • i=6⇒s=i=6 \Rightarrow s="codefrces";
  • i=1⇒s=i=1 \Rightarrow s="odefrces";
  • i=7⇒s=i=7 \Rightarrow s="odefrcs";

Given a given string ss, find the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.

A string aa of length nn is lexicographically less than a string bb of length mm, if:

  • there is an index ii (1≤i≤min⁡(n,m)1 \le i \le \min(n, m)) such that the first i−1i-1 characters of the strings aa and bb are the same, and the ii-th character of the string aa is less than ii-th character of string bb;
  • or the first min⁡(n,m)\min(n, m) characters in the strings aa and bb are the same and n<mn \lt m.

For example, the string a=a="aezakmi" is lexicographically less than the string b=b="aezus".

给你一个由小写拉丁字母组成的字符串 ss。只要字符串 ss 中至少存在一个字符出现次数不少于两次,你就执行如下操作:

  • 选择一个下标 ii(1≤i≤∣s∣1 \le i \le |s|),使得位置 ii 上的字符在字符串 ss 中至少出现两次,并删除该位置上的字符,即用 s1s2…si−1si+1si+2…sns_1 s_2 \ldots s_{i-1} s_{i+1} s_{i+2} \ldots s_n 替换 ss。

例如,若 s=s = "codeforces",则可执行如下操作序列:

  • i=6⇒s=i = 6 \Rightarrow s = "codefrces";
  • i=1⇒s=i = 1 \Rightarrow s = "odefrces";
  • i=7⇒s=i = 7 \Rightarrow s = "odefrcs"。

给定字符串 ss,求在执行若干次上述操作(使得最终字符串中所有字符互不相同)后,所能得到的字典序最大的字符串。

长度为 nn 的字符串 aa 在字典序上小于长度为 mm 的字符串 bb,当且仅当满足以下任一条件:

  • 存在某个下标 ii(1≤i≤min⁡(n,m)1 \le i \le \min(n, m)),使得字符串 aa 和 bb 的前 i−1i-1 个字符完全相同,且 aa 的第 ii 个字符严格小于 bb 的第 ii 个字符;
  • 或字符串 aa 和 bb 的前 min⁡(n,m)\min(n, m) 个字符完全相同,且 n<mn \lt m。

例如,字符串 a=a = "aezakmi" 在字典序上小于字符串 b=b = "aezus"。

输入格式

The first line contains one integer tt (1≤t≤1041 \le t \le 10^4). Then tt test cases follow.

Each test case is characterized by a string ss, consisting of lowercase Latin letters (1≤∣s∣≤2⋅1051 \le |s| \le 2 \cdot 10^5).

It is guaranteed that the sum of the lengths of the strings in all test cases does not exceed 2⋅1052 \cdot 10^5.

第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)。随后是 tt 组测试用例。

每组测试用例由一个字符串 ss 描述,该字符串仅由小写拉丁字母组成(1≤∣s∣≤2⋅1051 \le |s| \le 2 \cdot 10^5)。

保证所有测试用例中字符串长度的总和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, output the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.

对于每个测试用例,输出在执行某一操作序列后所能得到的字典序最大的字符串,使得该字符串中所有字符均互不相同。

输入输出样例

  • 输入#1

    6
    codeforces
    aezakmi
    abacaba
    convexhull
    swflldjgpaxs
    myneeocktxpqjpz

    输出#1

    odfrces
    ezakmi
    cba
    convexhul
    wfldjgpaxs
    myneocktxqjpz

输入解题思路,AI测评打分。不知道怎么写?

首页