Polycarp and String Transformation(字符串)

E. Polycarp and String Transformation

#739 (Div. 3)E. Polycarp and String Transformation

Polycarp has a string s. Polycarp performs the following actions until the string s is empty (t is initially an empty string):

he adds to the right to the string t the string s, i.e. he does t=t+s, where t+s is a concatenation of the strings t and s;
he selects an arbitrary letter of s and removes from s all its occurrences (the selected letter must occur in the string s at the moment of performing this action).
Polycarp performs this sequence of actions strictly in this order.

Note that after Polycarp finishes the actions, the string s will be empty and the string t will be equal to some value (that is undefined and depends on the order of removing).

E.g. consider s=“abacaba” so the actions may be performed as follows:

t=“abacaba”, the letter ‘b’ is selected, then s=“aacaa”;
t=“abacabaaacaa”, the letter ‘a’ is selected, then s=“c”;
t=“abacabaaacaac”, the letter ‘c’ is selected, then s="" (the empty string).
You need to restore the initial value of the string s using only the final value of t and find the order of removing letters from s.

Input
The first line contains one integer T (1≤T≤104) — the number of test cases. Then T test cases follow.

Each test case contains one string t consisting of lowercase letters of the Latin alphabet. The length of t doesn’t exceed 5⋅105. The sum of lengths of all strings t in the test cases doesn’t exceed 5⋅105.

Output
For each test case output in a separate line:−1, if the answer doesn’t exist;
two strings separated by spaces. The first one must contain a possible initial value of s. The second one must contain a sequence of letters — it’s in what order one needs to remove letters from s to make the string t. E.g. if the string “bac” is outputted, then, first, all occurrences of the letter ‘b’ were deleted, then all occurrences of ‘a’, and then, finally, all occurrences of ‘c’. If there are multiple solutions, print any one.

  • 输入
    7
    abacabaaacaac
    nowyouknowthat
    polycarppoycarppoyarppyarppyrpprppp
    isi
    everywherevrywhrvryhrvrhrvhv
    haaha
    qweqeewew
  • 输出
    abacaba bac
    -1
    polycarp lcoayrp
    is si
    everywhere ewyrhv
    -1
    -1

还是一道思路比较直接的题目,当时卡的也很离谱,现在也不知道是为什么会被卡

  • 思路:首先会发现每一个字母的价值就是最后第几个删除,如果一共删除了n次,那么最后一个删除的字母就是重复了n次,所以统计一下这个字母总共出现的次数,然后再/这个字母删除的次序,就可以得到答案,而在这里,删除的次序就是从后往前数出现的次序,因为如果一个字母(假设x)在第二次删除,那么第三次输出的时候我们一定会枚举出第四个和第五个~~删除的字母,所以从后往前跑一边就可以
#include <bits/stdc++.h>
using namespace std;

int num[30];
int fie[30];

int main()
{
    int T;
    cin >> T;
    while (T--)
    {
        memset(num, 0, sizeof num);
        string s;
        cin >> s;
        string ans1 = "";  //这是我们的删除顺序

        int n = s.size();

        for (int i = n - 1; i >= 0; i--)
        {
            if(!num[s[i]-'a'])
                ans1+=s[i];
        
            num[s[i] - 'a']++;  //存储一共有多少个这样的字母
        }
        reverse(ans1.begin(), ans1.end());  //反转 得到我们所需要的删除顺序

        int liuzhuo = ans1.size();
        for (int i = 0; i < liuzhuo; i++)
            fie[ans1[i] - 'a'] = i;
        for (int i = 0; i < 26; i++)
            num[i] = num[i] / (fie[i] + 1);  //这就是原始字符串的每一个字母应该有的数量

        string ans = "";

        for (int i = 0; i < n; i++)
        {
            if (num[s[i] - 'a'])
            {
                num[s[i] - 'a']--;
                ans += s[i];
            }
            else
                break;
        }
        int fla = 1;
        for (int i = 0; i < 26; i++)  //如果无法用完所有的字母 说明这是不符合条件的
        {
            if (num[i])
            {
                fla = 0;
                break;
            }
        }

        if (!fla)
            cout << "-1\n";
        else
        {
            int m = ans1.size();
            string sss = ans;

            int l = 0;  //下面是跑了一个暴力 验证答案是否正确
            for (int i = 0; i < m; i++)
            {
                int n = ans.size();

                for (int j = l; j < n; j++)
                {
                    if (ans[j] == ans1[i])
                        continue;

                    ans +=ans[j];
                }
                l = n;
            }
            if (ans != s)
                cout << "-1\n";
            else
                cout << sss << " " << ans1 << endl;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

pig2687

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

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

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

打赏作者

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

抵扣说明:

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

余额充值