Codeforces Round #827 (Div. 4) F. Smaller 解题报告

原题链接:

Problem - 1742F - Codeforces

题目描述:

Alperen has two strings, ss and tt which are both initially equal to "a".

He will perform qq operations of two types on the given strings:

  • 1kx1kx — Append the string xx exactly kk times at the end of string ss. In other words, s:=s+x+⋯+xk timess:=s+x+⋯+x⏟k times.
  • 2kx2kx — Append the string xx exactly kk times at the end of string tt. In other words, t:=t+x+⋯+xk timest:=t+x+⋯+x⏟k times.

After each operation, determine if it is possible to rearrange the characters of ss and tt such that ss is lexicographically smaller†† than tt.

Note that the strings change after performing each operation and don't go back to their initial states.

†† Simply speaking, the lexicographical order is the order in which words are listed in a dictionary. A formal definition is as follows: string pp is lexicographically smaller than string qq if there exists a position ii such that pi<qipi<qi, and for all j<ij<i, pj=qjpj=qj. If no such ii exists, then pp is lexicographically smaller than qq if the length of pp is less than the length of qq. For example, abdc<abeabdc<abe and abc<abcdabc<abcd, where we write p<qp<q if pp is lexicographically smaller than qq.

Input

The first line of the input contains an integer tt (1≤t≤1041≤t≤104) — the number of test cases.

The first line of each test case contains an integer qq (1≤q≤105)(1≤q≤105) — the number of operations Alperen will perform.

Then qq lines follow, each containing two positive integers dd and kk (1≤d≤21≤d≤2; 1≤k≤1051≤k≤105) and a non-empty string xx consisting of lowercase English letters — the type of the operation, the number of times we will append string xx and the string we need to append respectively.

It is guaranteed that the sum of qq over all test cases doesn't exceed 105105 and that the sum of lengths of all strings xx in the input doesn't exceed 5⋅1055⋅105.

Output

For each operation, output "YES", if it is possible to arrange the elements in both strings in such a way that ss is lexicographically smaller than tt and "NO" otherwise.

题目大意:

给定两个初始字符串A和B,两个字符出初始都为"a"。每次操作都指定在A或者B后边加一个非空字符串s,请问每次操作之后,是否可以通过重排A和B,使得A的字典序小于B。(每次操作都需要给出回答)

解题思路:

因为A和B初始都有一个字母'a',所以那就很好办了:
如果经历一次操作后字符串B内含有非’a'的其他字母,那么一定可以满足重排之后A的字典序更小,因为我们可以选择把B的非‘a'字母放到B的首位;

如果经历一次操作后字符串A和B都不含有除a以外的其他字母,此时谁的长度更小则字典序更小;

如果A内含有a以外的其他字母,并且B内没有,那么则不可能满足重拍后A的字典序能够小于B。

所以我们只需要记录A和B的长度以及是否包含除a以外的其他字母即可。

代码(CPP):

#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1e3 + 10;
const int INF = 0x3fffffff;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cout << fixed;
    cout.precision(18);

    int t;
    cin >> t;
    while(t--)
    {
        int q;
        cin >> q;
        // 记录两个字符串的长度,以及是否包含了a以外的其他字母
        ll a = 1, b = 1;
        bool flagA = false, flagB = false;
        while(q--)
        {
            int d, k;
            string s;
            cin >> d >> k >> s;
            if(d == 1)
            {
                a += 1LL * k * (s.length());
                for(auto c : s)
                {
                    if(c != 'a')
                    {
                        flagA = true;
                        break;
                    }
                }
            }
            else
            {
                b += 1LL * k * (s.length());
                for(auto c : s)
                {
                    if(c != 'a')
                    {
                        flagB = true;
                        break;
                    }
                }
            }
            if(flagB)
                cout << "YES\n";
            else if(!flagA)
                cout << (a < b ? "YES" : "NO") << endl;
            else
                cout << "NO\n";
        }
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值