2023 广东省赛 E. New but Nostalgic Problem

文章介绍了一种在Codeforces平台上遇到的问题,涉及使用字典树(Trie)数据结构来解决字符串集合中的查询。代码展示了如何插入字符串到字典树中,以及如何遍历树以找到第k个特定位置的字符。在构建和遍历字典树的过程中,文章强调了计算节点结束字符串的数量、子树总和等关键步骤。

Problem - E - Codeforces

题面:

官方题解:

 代码(有英文注释):

char s[N];
int son[N][26], ed[N], sum[N], calc[N];
// ed[i] is the num of string end of node i
// sum[i] is the sum of ed in subtree of i
// des[i] is true if there is a subnode j of i ,and ed[j]>0
// calc[i] is the sum of des[j], j is all i direct son
bool des[N];
int tot, slen;
void insert() {
    // insert to the trie
    int p = 0;
    for (int i = 1; i <= slen; i++) {
        if (!son[p][s[i] - 'a'])
            son[p][s[i] - 'a'] = ++tot;
        p = son[p][s[i] - 'a'];
    }
    ed[p]++;
}
void build(int i) {
    // initial the des,sum,calc
    if (ed[i]) {
        des[i] = true;
        sum[i] = ed[i];
    }
    for (int j : son[i]) {
        if (j) {
            build(j);
            des[i] |= des[j];
            sum[i] += sum[j];
            calc[i] += des[j];
        }
    }
}
char ans[N];
// ans string
int anstot, n, k, L, R;
// L is all sum,which node is located left of now
// R is all des and ed ,which node is located right of now
void dfs(int i) {
    if (L + R >= k)
        return;
    // if at begining, the L+R>=k, return
    // int the initial, R is added all des[j],j is direct son of i
    for (int c = 0; c < 26; c++) {
        int j = son[i][c];
        if (j) {
            R -= des[j];
            L += sum[j];
            // j is turn from right node to mid node
            if (L + R >= k) {
                ans[++anstot] = char('a' + c);
                // add ans
                // repeal the change and dfs down
                R += ed[j] + calc[j];
                L -= sum[j];
                dfs(j);
                return;
            }
        }
    }
}
int main() {
    int t_;
    cin >> t_;
    while (t_--) {
        cin >> n >> k;
        for (int i = 1; i <= n; i++) {
            cin >> (s + 1);
            slen = strlen(s + 1);
            insert();
        }
        build(0);
        // build trie and get sum,des,calc
        R = calc[0];
        calc[0] = 0;
        dfs(0);
        // get ans and output ans
        if (!anstot)
            cout << "EMPTY";
        else
            for (int i = 1; i <= anstot; i++)
                cout << ans[i];
        cout << "\n";
        // clear
        for (int i = 0; i <= tot; i++) {
            memset(son[i], 0, sizeof son[i]);
            ed[i] = sum[i] = calc[i] = 0;
            des[i] = false;
        }
        tot = anstot = L = R = 0;
    }
}

本场比赛的全部题解可以参考:2023广东省赛题解(SUA现场讲解)_哔哩哔哩_bilibili

### 排查 `http://0.0.0.0` 网页无法访问或地址变更的解决方案 #### 一、理解 `http://0.0.0.0` `http://0.0.0.0` 是一种特殊的表示方式,通常用于服务器端监听所有可用网络接口上的连接请求。它并不指向具体的 IP 地址,而是意味着服务可以接受来自任何 IP 的请求[^1]。 如果尝试通过浏览器直接访问 `http://0.0.0.0`,会失败,因为这并不是一个实际可路由的地址。需要将其替换为运行服务的实际主机名或 IP 地址。 --- #### 二、排查网页无法访问的原因 以下是可能导致 `http://0.0.0.0` 网页不可访问的主要原因及其对应的解决方法: 1. **服务未启动** 如果服务尚未启动,则不会有任何响应。可以通过 Docker 日志命令确认容器内的服务状态: ```bash sudo docker logs -f <container_name> ``` 上述命令可以帮助验证服务是否成功启动并绑定到指定端口。例如,在引用中提到的服务日志显示其正在运行于 `http://0.0.0.0:5000/`[^2]。 2. **防火墙阻止访问** 防火墙可能阻止外部设备对该服务的访问。需检查本地防火墙设置以及云服务商的安全组配置,确保目标端口(如 5000)已开放。 3. **代理干扰** 外部代理可能会拦截对特定地址的请求。若存在代理环境变量(如 `HTTP_PROXY`, `HTTPS_PROXY`),应考虑将本机地址加入代理忽略列表[^1]。可通过以下方式实现: ```bash export NO_PROXY="localhost,127.0.0.1,<your_host_ip>" ``` 4. **DNS 解析错误** 若使用域名而非 IP 访问,可能是 DNS 配置不正确所致。建议先通过 IP 测试连通性,再逐步排查域名解析问题。 5. **IP 变更** 当前服务绑定的是 `0.0.0.0`,这意味着它可以被映射至任意有效的局域网或公网 IP。若不确定当前机器的具体 IP 地址,可执行如下命令获取: ```bash hostname -I ``` 或者在 Linux 中查看网络适配器详情: ```bash ip addr show ``` --- #### 三、寻找新的访问地址 当发现原地址失效时,可以根据实际情况采取以下措施定位新地址: 1. **查找宿主机 IP** 容器中的服务虽然绑定了 `0.0.0.0`,但实际上对外暴露的地址取决于宿主机的网络配置。对于标准桥接模式下的 Docker,默认情况下可以通过宿主机的 IP 和对应端口号访问容器内部服务。 2. **Docker 映射端口查询** 使用以下命令列出所有容器及其端口映射关系: ```bash sudo docker ps --format "{{.Names}} -> {{.Ports}}" ``` 输出示例: ``` nostalgic_morse -> 0.0.0.0:32768->5000/tcp ``` 这表明可以从宿主机的 `32768` 端口访问容器内运行在 `5000` 端口的服务。 3. **动态分配的新地址** 某些场景下,服务可能因负载均衡或其他机制迁移到不同的实例上。此时需联系运维团队或者查阅相关文档了解最新的部署架构变化。 --- ```python import socket def get_local_ips(): """ 获取本机所有活动的 IPv4 地址 """ local_ips = [] for interface, snics in psutil.net_if_addrs().items(): # 假设安装了psutil库 for snic in snics: if snic.family.name == 'AF_INET': # 仅保留IPv4地址 local_ips.append(snic.address) return local_ips if __name__ == "__main__": print("本机可用IP:", get_local_ips()) ``` 上述脚本可用于快速枚举本机所有的有效 IPv4 地址以便进一步测试。 --- #### 四、总结 针对 `http://0.0.0.0` 网页无法访问的情况,主要从以下几个方面入手:确认服务是否正常启动;排除防火墙和代理的影响;核实正确的访问路径。一旦明确了这些因素之后,就能够顺利解决问题或将流量导向新的合法入口。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值