problem-1654B. Prefix Removals-codeforces

题目:

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.

输入:

The first line contains a single integer tt (1≤t≤1041≤t≤104) — 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⋅105 inclusive.

It is guaranteed that the sum of the lengths of ss over all test cases does not exceed 2⋅1052⋅105.

输出:

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.

Examples

input

6
abcabdc
a
bbbbbbbbbb
codeforces
cffcfccffccfcffcfccfcffccffcfccf
zyzyzwxxyyxxyyzzyzzxxwzxwywxwzxxyzzw

output

abdc
a
b
deforces
cf
xyzzw

代码:

#include <iostream>
#include <string>
#include <cstring>
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>

using namespace std;
const int N = 2e5 + 10;
const long long mod = 998244353 ;


typedef long long ll;
typedef pair<int, int> PII;

#define forn(i,a,b) for(int i=a;i<b;i++)
#define all(c) (c).begin(),(c).end()
#define srt(c) sort(all(c))
#define pb push_back

ll gcd(ll a, ll b) {
	return b == 0 ? a : gcd(b, a % b);
}

int t;
string s;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
    cin>>t;
    while(t--)
    {
        cin>>s;
        map<char,int> m;
        for(auto i:s) m[i]++;
        for(int i=0;i<s.size();i++)
        {
            if(--m[s[i]]==0)
            {
                cout<<s.substr(i)<<endl;
                break;
            }
        }
    }
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

'<王半仙>'

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值