Codeforces-Round-827-Div-4-F-Smaller


title: Codeforces Round 827 (Div. 4) F. Smaller
date: 2023-04-06 20:39:36
categories:

  • Algorithm
  • Codeforces
    tags:
  • codeforces
  • 字典序
  • 字符串
  • 贪心
  • 1500

F. Smaller

Alperen has two strings, 𝑠 and 𝑡 which are both initially equal to “a”.

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

1𝑘𝑥 — Append the string 𝑥 exactly 𝑘 times at the end of string 𝑠 . In other words, 𝑠:=𝑠+𝑥+⋯+𝑥𝑘 times . 2𝑘𝑥 — Append the string 𝑥 exactly 𝑘 times at the end of string 𝑡 . In other words, 𝑡:=𝑡+𝑥+⋯+𝑥𝑘 times . After each operation, determine if it is possible to rearrange the characters of 𝑠 and 𝑡 such that 𝑠 is lexicographically smaller† than 𝑡 .

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 𝑝 is lexicographically smaller than string 𝑞 if there exists a position 𝑖 such that 𝑝𝑖<𝑞𝑖 , and for all 𝑗<𝑖 , 𝑝𝑗=𝑞𝑗 . If no such 𝑖 exists, then 𝑝 is lexicographically smaller than 𝑞 if the length of 𝑝 is less than the length of 𝑞 . For example, 𝚊𝚋𝚍𝚌<𝚊𝚋𝚎 and 𝚊𝚋𝚌<𝚊𝚋𝚌𝚍 , where we write 𝑝<𝑞 if 𝑝 is lexicographically smaller than 𝑞 .

Input

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

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

Then 𝑞 lines follow, each containing two positive integers 𝑑 and 𝑘 (1≤𝑑≤2 ; 1≤𝑘≤105 ) and a non-empty string 𝑥 consisting of lowercase English letters — the type of the operation, the number of times we will append string 𝑥 and the string we need to append respectively.

It is guaranteed that the sum of 𝑞 over all test cases doesn’t exceed 105 and that the sum of lengths of all strings 𝑥 in the input doesn’t exceed 5⋅105 .

Output

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

题目大意

现在有两个字符串s和t,初始的时候他们都是a,即s=“a”,t=“a”

每组测试有q个操作,对于每个操作,收入d,k,x;

  • d=1,s加上k个x
  • d=2,t加上k个x

问能否重新排列s和t,使得s的字典序小于t。

思路

因为一开始都是a,所以一旦b中出现了一个大于a,那么肯定可以把这个大于a的放在第一位,此时t的字典序一定会小于s。如果b中没有出现过一个大于a的字符,那么说明b一定全是"a",此时如果s的长度>=t的长度,那么 s的字典序一定会大于t,因为b全是“a”,一定已经是最小的了,如果s的长度<t的长度,那么就去看s中有没有出现过大于"a"的字符,如果有的话,那么s的字典序也一定是大于b的,否则s的字典序就小于t

代码

#include <bits/stdc++.h>
#define int long long
using namespace std;

void solve() {
    int q;
    cin >> q;
    bool flag1 = false, flag2 = false;
    int len1 = 1, len2 = 1;
    while (q--) {
        int d, k;
        string x;
        cin >> d >> k >> x;
        if (d == 1) {
            len1 += k * x.size();
            if (!flag1) {
                for (auto item: x) {
                    if (item > 'a')flag1 = true;
                }
            }
        } else {
            len2 += k * x.size();
            if (!flag2) {
                for (auto item: x) {
                    if (item > 'a') flag2 = true;
                }
            }
        }
        if (flag2) {
            cout << "YES" << endl;
        } else {
            if (len1>=len2){
                cout<<"NO"<<endl;
            }else{
                if (flag1){
                    cout<<"NO"<<endl;
                }else{
                    cout<<"YES"<<endl;
                }
            }
        }
    }
}

signed main() {
#ifndef ONLINE_JUDGE
    freopen("../test.in", "r", stdin);
    freopen("../test.out", "w", stdout);
#endif
    int _;
    cin >> _;
    while (_--) solve();

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

重生之我是cxk

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

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

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

打赏作者

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

抵扣说明:

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

余额充值