Codeforces Round #401 (Div. 2) D. Cloud of Hashtags【模拟、贪心】

本文介绍了一种通过删除最少字符使一系列以#开头的字符串达到字典序不递减的方法。采用从后向前的贪心策略,确保尽可能多地保留原始字符串内容。通过比较和模拟实现最优化解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

D. Cloud of Hashtags

题意:

1.给你 n 串字符串,都是以#开头。
2.让你删除掉最少的一些字符,从而形成 n 串字典序不递减的字符串。

思路:

1.贪心策略:从后往前推,即是形成字典序不递增的字符串,这样可以使得保留最多,即删除最少。
2.每次计算,只与后一列有关系。
3.直接比对模拟取子串就行了。

代码:

#include <bits/stdc++.h>
using namespace std;
char s[500500];
string buf[500500];
char minn[500500];
string scf() {
    scanf("%s", &s);
    return s;
}
int main() {
    int n;
    scanf("%d", &n);
    for(int i = 0; i < n; i++) buf[i] = scf();
    for(int i = 0; i < buf[n-1].size(); i++) minn[i] = buf[n-1][i];
    int idx = buf[n-1].size();
    for(int i = n-2; i >= 0; i--) {
        string tp = buf[i];
        tp = tp.substr(0, idx);
        int to = min(idx, (int)tp.size());
        for(int j = 0; j < to; j++) {
            if(tp[j] < minn[j]) {
                minn[j] = tp[j];
                tp = buf[i];
                for(int k = j; k < tp.size(); k++) minn[k] = tp[k];
                break;
            } else if(tp[j] > minn[j]) {
                tp = tp.substr(0, j);
                break;
            }
        }
        idx = tp.size();
        buf[i] = tp;
    }
    for(int i = 0; i < n; i++) cout << buf[i] << endl;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值