USACO 1月 2022-2023 January Contest Gold金组 题解

首先声明一下,暴力完全是过不了的

Problem 1. Find and Replace

Bessie is using the latest and greatest innovation in text-editing software, miV! Its powerful find-and-replace feature allows her to find all occurrences of a lowercase English letter c and replace each with a nonempty string of lowercase letters s. For example, given the string "𝚋𝚊𝚕𝚕ball", if Bessie selects c to be 'l' and s to be "𝚗𝚊na", the given string transforms into "𝚋𝚊𝚗𝚊𝚗𝚊banana".

Bessie starts with the string "𝚊a" and transforms it using a number of these find-and-replace operations, resulting in a final string S. Since S could be massive, she wants to know, given l and r with 1≤l≤r≤min(|S|,1018)1≤≤≤min(||,1018), what Sl…r… (the substring of S from the l-th to the r-th character inclusive) is.

It is guaranteed that the sum of |s||| over all operations is at most 2⋅1052⋅105, and that r−l+1≤2⋅105+1≤2⋅105.

INPUT FORMAT (input arrives from the terminal / stdin):

The first line contains l, r, and the number of operations.

Each subsequent line describes one operation and contains c and s for that operation. All characters are in the range 'a' through 'z'.

OUTPUT FORMAT (print output to the terminal / stdout):

Output the string Sl…r on a single line.

SAMPLE INPUT:

3 8 4
a ab
a bc
c de
b bbb

SAMPLE OUTPUT:

bdebbb

The string is transformed as follows:

𝚊→𝚊𝚋→𝚋𝚌𝚋→𝚋𝚍𝚎𝚋→𝚋𝚋𝚋𝚍𝚎𝚋𝚋𝚋a→ab→bcb→bdeb→bbbdebbb

SCORING:

  • Inputs 2-7: ∑|s|,r−l+1≤2000∑|+1≤2000
  • Inputs 8-15: No additional constraints.

翻译:

Bessie 正在使用文本编辑软件 miV 中最新和最伟大的创新。其强大的查找和替换功能使她能够找到所有出现的小写英文字母 c,并将每个字母替换为非空的小写字母 s 字符串。例如,给定字符串“𝚋𝚊𝚕𝚕”,如果 Bessie 选择 c 为“l”,s 为“𝚗𝚊”,则给定的字符串将转换为“𝚋𝚊𝚗𝚊𝚗𝚊”。

Bessie 从字符串“𝚊”开始,并使用许多这样的查找和替换操作对其进行转换,从而得到最终的字符串 S。由于 S 可能很大,她想知道,给定 l 和 r,其中 1≤l≤ r≤min(|S|,1018),什么是 Sl…r…(S 从第 l 个字符到第 r 个字符的子串)是什么。

保证 |s| 的总和在所有操作中至多为 2⋅105,并且 r−l+1≤2⋅105。

思路:

我最开始的思路就是暴力出所有的子串

0分代码:

我起初是这样想的,但是单一个replace函数所用的时间太多,而且效率不够高,主函数又要挨个暴力出0~n的子串个数,所以导致所有数据超时。

#include <bits/stdc++.h>
using namespace std;
 
string replace(string str, const string& from, const string& to) {
    stringstream ss;
    size_t start_pos = 0;
    while ((start_pos = str.find(from, start_pos)) != string::npos) {
        ss << str.substr(0, start_pos) << to;
        start_pos += from.length();
        str = str.substr(start_pos);
    }
    ss << str;
    return ss.str();
}
 
int main() {
    string str = " a";  //在指定串里查找
    long long l,r,n;
    cin>>l>>r>>n;
    //后面为n行的operation行为
    for(long long i=0;i<n;i++){
        string from;        //要查找的
        string to;          //要替换成的
        cin>>from>>to;
        str = replace(str, from, to);
    }
    //截取
    string final = str.substr(l,r);
    cout << final << endl;// 输出 
    return 0;
}

正确思路:

表示字符串 S 的自然数据结构是一棵二叉树,其中每个叶节点包含一个单独的字符,并且操作涉及用子树替换一些叶节点。例如输入样例数据的树将像这样变化

满分代码

#include <algorithm>
#include <iostream>
#include <string>
#include <utility>
typedef long long ll;
using namespace std;

const ll INF = 1e18;

struct Node {
    char value;
    ll size;
    Node *l, *r;

    void print_substring(ll start, ll end) {
        start = max(start, 1ll);
        end = min(end, size);
        if (start > end) {
            return;
        }
        if (value != '.') {
            cout << value;
        } else {
            l->print_substring(start, end);
            r->print_substring(start - l->size, end - l->size);
        }
    }
};

Node* current[26];
pair<char, string> operations[200000];

int main() {
    cin.tie(0)->sync_with_stdio(0);
    ll l, r;
    int n;
    cin >> l >> r >> n;
    for (int i = n - 1; i >= 0; i--) {
        cin >> operations[i].first >> operations[i].second;
    }

    for (char c = 'a'; c <= 'z'; c++) {
        current[c - 'a'] = new Node{c, 1};
    }
    for (int i = 0; i < n; i++) {
        Node* result = nullptr;
        for (char c : operations[i].second) {
            Node* to_merge = current[c - 'a'];
            if (result == nullptr) {
                result = to_merge;
            } else {
                result = new Node{
                    '.',
                    min(INF, result->size + to_merge->size),
                    result,
                    to_merge
                };
            }
        }
        current[operations[i].first - 'a'] = result;
    }

    current[0]->print_substring(l, r);
    cout << '\n';
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值