题目:https://codeforces.com/problemset/problem/1560/E
题解:
- 先获取从最后遍历记录下每个第一个出现的字母,这就是删除字母的逆序。
- 记录下每个字母出现的次数,再按删除的顺序除与他们。
- 例:abacabaaacaac 1. b 2 a 8 c 3 2.2 4 1
- 再截取t的前几个字母得出s。
- 模拟一下,对比模拟后是否与t相同。
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long int ll;
const int N = 1e5 + 5;
int main()
{
int n;
cin >> n;
while (n--)
{
string t, a, s;
//t是给出的字符串 a是依次删除的字符串 s是开始的字符串
int num[30] = { 0 };
cin >> t;
for (int i = t.size() - 1; i >= 0; i--)
{
if (num[t[i] - 'a'] == 0)
a = t[i] + a;
num[t[i] - 'a']++;
}
for(int i=0;i<a.size();i++)
{
num[a[i] - 'a'] /= (i + 1);
}
for (int i = 0; i < t.size(); i++)
{
num[t[i] - 'a']--;
if (num[t[i] - 'a'] < 0)
break;
else s += t[i];
}
string x;//模拟
string s1 = s;
x += s1;
for (int i = 0; i < a.size(); i++)
{
string s2;
for (int j = 0; j < s1.size(); j++)
{
if (s1[j] != a[i])
s2 += s1[j];
}
s1 = s2;
x += s1;
}
if (x == t)
cout << s << " " << a << endl;
else cout << "-1" << endl;
}
}