题目:
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;
}