CF1654B.Prefix Removals

入门

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given a string ss consisting of lowercase letters of the English alphabet. You must perform the following algorithm on ss:

  • Let xx be the length of the longest prefix of ss which occurs somewhere else in ss as a contiguous substring (the other occurrence may also intersect the prefix). If x=0x = 0, break. Otherwise, remove the first xx characters of ss, and repeat.

A prefix is a string consisting of several first letters of a given string, without any reorders. An empty prefix is also a valid prefix. For example, the string "abcd" has 5 prefixes: empty string, "a", "ab", "abc" and "abcd".

For instance, if we perform the algorithm on s=s = "abcabdc",

  • Initially, "ab" is the longest prefix that also appears somewhere else as a substring in ss, so s=s = "cabdc" after 11 operation.
  • Then, "c" is the longest prefix that also appears somewhere else as a substring in ss, so s=s = "abdc" after 22 operations.
  • Now x=0x=0 (because there are no non-empty prefixes of "abdc" that also appear somewhere else as a substring in ss), so the algorithm terminates.

Find the final state of the string after performing the algorithm.

给你一个由英文字母小写组成的字符串 ss。你需要对 ss 执行以下算法:

  • 设 xx 为 ss 的最长前缀的长度,该前缀在 ss 中某处(作为连续子串)至少另出现一次(另一处出现的位置可以与该前缀重叠)。若 x=0x = 0,则终止算法;否则,从 ss 中删除最前面的 xx 个字符,并重复执行。

前缀是指由给定字符串的若干个开头字母按原序组成的字符串(不允许重新排序)。空字符串也是一个合法的前缀。例如,字符串 "abcd" 共有 5 个前缀:空字符串、"a"、"ab"、"abc" 和 "abcd"。

例如,对 s=s = "abcabdc" 执行该算法:

  • 初始时,"ab" 是 ss 的最长前缀,且在 ss 中某处(作为连续子串)另出现(如位置 3–4),因此执行 1 次操作后 s=s = "cabdc"。
  • 接着,"c" 是当前 ss 的最长前缀,且在 ss 中某处另出现(如位置 2),因此执行 2 次操作后 s=s = "abdc"。
  • 此时 x=0x = 0(因为 "abdc" 的所有非空前缀均未在 ss 中其他位置作为连续子串出现),算法终止。

求执行该算法后字符串的最终状态。

输入格式

The first line contains a single integer tt (1≤t≤1041 \le t \le 10^4) — the number of test cases.

This is followed by tt lines, each containing a description of one test case. Each line contains a string ss. The given strings consist only of lowercase letters of the English alphabet and have lengths between 11 and 2⋅1052 \cdot 10^5 inclusive.

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

第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)—— 表示测试用例的数量。

接下来是 tt 行,每行描述一个测试用例。每行包含一个字符串 ss。给定的字符串仅由英文字母的小写字母组成,且长度在 11 到 2⋅1052 \cdot 10^5(含)之间。

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

输出格式

For each test case, print a single line containing the string ss after executing the algorithm. It can be shown that such string is non-empty.

对于每个测试用例,输出执行该算法后得到的字符串 ss。可以证明,这样的字符串非空。

输入输出样例

  • 输入#1

    6
    abcabdc
    a
    bbbbbbbbbb
    codeforces
    cffcfccffccfcffcfccfcffccffcfccf
    zyzyzwxxyyxxyyzzyzzxxwzxwywxwzxxyzzw

    输出#1

    abdc
    a
    b
    deforces
    cf
    xyzzw

说明/提示

The first test case is explained in the statement.

In the second test case, no operations can be performed on ss.

In the third test case,

  • Initially, s=s = "bbbbbbbbbb".
  • After 11 operation, s=s = "b".

In the fourth test case,

  • Initially, s=s = "codeforces".
  • After 11 operation, s=s = "odeforces".
  • After 22 operations, s=s = "deforces".

第一个测试用例已在题目描述中说明。

第二个测试用例中,无法对字符串 ss 执行任何操作。

第三个测试用例中:

  • 初始时,s=s = "bbbbbbbbbb"。
  • 经过 11 次操作后,s=s = "b"。

第四个测试用例中:

  • 初始时,s=s = "codeforces"。
  • 经过 11 次操作后,s=s = "odeforces"。
  • 经过 22 次操作后,s=s = "deforces"。

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

首页