[比赛题解] 牛客小白月赛91 2024.4.19

Bingbong的算法挑战:字符串操作、哈希与子串计数
文章讲述了在编程竞赛中,如何通过动态规划、字符串匹配和哈希技术解决涉及子串查找、子序列判断以及回文路径等问题,包括使用dp数组计算符合条件的子串数量,以及利用哈希和LCA快速检查字符串特性。

比赛链接:牛客小白月赛91

E Bingbong的字符串世界

枚举子串的右端点 i i i,考虑以 i i i为右端点的子串,综合考虑能包含子序列ACCEPT的最近左端点、能包含子序列WA的最近左端点、子串的最小长度 k k k,即可计算以 i i i为右端点的符合条件的子串数量。

#include <bits/stdc++.h>
typedef long long ll;
#define INF 1000000000

const int MAXN = 200000+5;
int n, k;
char s[MAXN];
int dp1[10], dp2[10];
char s1[10] = "ACCEPT", s2[10] = "WA";
ll ans;

int solve(int l, int r, int x){
    if(l >= r || l >= x)  return 0;
    r = std::min(x, r);
    return r-l;
}

int main(){
    scanf("%d%d", &n, &k);
    scanf("%s", s+1);
    for(int i = 0; i < 6; i++)
        dp1[i] = INF;
    for(int i = 0; i < 2; i++)
        dp2[i] = 0;
    for(int i = 1; i <= n; i++){
        for(int j = 5; j >= 0; j--){
            if(s[i] == s1[j]){
                if(j)
                    dp1[j] = dp1[j-1];
                else
                    dp1[j] = i;
            }
        }
        for(int j = 1; j >= 0; j--){
            if(s[i] == s2[j]){
                if(j)
                    dp2[j] = dp2[j-1];
                else
                    dp2[j] = i;
            }
        }
//         printf("dp %d %d\n", dp1[5], dp2[1]);
        if(i >= k && dp1[5] < INF)
            ans += solve(dp2[1], dp1[5], i-k+1);
    }
    printf("%lld\n", ans);
    return 0;
}

F Bingbong的幻想世界

不同的位之间互不影响,可按位计算答案,则每个数只需考虑1和0。每一对 a i ⊕ a j = 1 ( i < j ) a_i\oplus a_j=1(i<j) aiaj=1(i<j)会在所有区间中计算 i × ( n − j + 1 ) i\times (n-j+1) i×(nj+1)次,枚举 j j j,用前缀和计算即可。

#include <bits/stdc++.h>
typedef long long ll;

const int MAXN = 200000+5;
const ll mod = 1000000000+7;
int n;
int a[MAXN], x[MAXN];
ll ans;

int main(){
    scanf("%d", &n);
    for(int i = 1; i <= n; i++){
        scanf("%d", &a[i]);
    }
    for(int d = 0; d <= 19; d++){
        for(int i = 1; i <= n; i++){
            x[i] = (a[i] >> d) & 1;
        }
        ll res = 0, s0 = 0, s1 = 0;
        for(int i = n; i >= 1; i--){
            if(x[i]){
                res = (res + s0*i) % mod;
                s1 += n-i+1;
            }
            else{
                res = (res + s1*i) % mod;
                s0 += n-i+1;
            }
        }
        // printf("d %d res %lld\n", d, res);
        ans = (ans + (res << d)) % mod;
    }
    printf("%lld\n", ans*2%mod);
    return 0;
}

G Bingbong的回文路径

LCA+字符串哈希做法

预处理出从根节点到每个节点的哈希值 h 1 [ u ] h1[u] h1[u]和从每个节点到根节点的哈希值 h 2 [ u ] h2[u] h2[u]后,结合逆元即可计算路径 ( u , v ) (u,v) (u,v)的哈希值,如果这个哈希值和 ( v , u ) (v,u) (v,u)的哈希值相等,则为回文串。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define fi first
#define se second
#define mp make_pair

const int MAXN = 100000+5;
const int mod1 = 1e9+7;
const int mod2 = 1e9+9;
int n, q;
int fa[MAXN][17], dep[MAXN];
char s[MAXN];
vector<int> G[MAXN];
pii h1[MAXN], h2[MAXN], pw[MAXN], ipw[MAXN], base, ibase;

ll fpow(ll a, ll p, ll mod){
    ll res = 1;
    while(p){
        if(p & 1)  res = res*a%mod;
        a = a*a%mod;
        p >>= 1;
    }
    return res;
}

pii operator + (const pii &a, const pii &b){
    int c1 = a.fi + b.fi;
    int c2 = a.se + b.se;
    if(c1 >= mod1)  c1 -= mod1;
    if(c2 >= mod2)  c2 -= mod2;
    return mp(c1, c2);
}

pii operator - (const pii &a, const pii &b){
    int c1 = a.fi - b.fi;
    int c2 = a.se - b.se;
    if(c1 < 0)  c1 += mod1;
    if(c2 < 0)  c2 += mod2;
    return mp(c1, c2);
}

pii operator * (const pii &a, const pii &b){
    return mp(1ll * a.fi * b.fi % mod1, 1ll * a.se * b.se % mod2);
}

void init_hash(int lim = 0){
    mt19937 rnd(time(0));
    base = {rnd() % mod1, rnd() % mod2};
    ibase = {fpow(base.fi, mod1-2, mod1), fpow(base.se, mod2-2, mod2)};
    pw[0] = ipw[0] = {1, 1};
    for(int i = 1; i <= lim; i++){
        pw[i] = pw[i-1] * base;
        ipw[i] = ipw[i-1] * ibase;
    }
}

void dfs(int u, int f){
    h1[u] = h1[fa[u][0]] * base + mp(s[u], s[u]);
    h2[u] = h2[fa[u][0]] + mp(s[u], s[u]) * pw[dep[u]];
    for(int v : G[u]){
        dep[v] = dep[u]+1;
        dfs(v, u);
    }
}

int getlca(int u, int v){
    if(dep[u] < dep[v])  swap(u, v);
    for(int i = 16; i >= 0; i--){
        if(((dep[u] - dep[v]) >> i) & 1){
            u = fa[u][i];
        }
    }
    if(u == v)  return u;
    for(int i = 16; i >= 0; i--){
        if(fa[u][i] != fa[v][i]){
            u = fa[u][i];
            v = fa[v][i];
        }
    }
    return fa[u][0];
}

pii solve(int u, int w, int v){
    int dv = dep[v] - dep[w];
    pii t1 = h1[v] - h1[w] * pw[dv];
    pii t2 = (h2[u] - h2[fa[w][0]]) * ipw[dep[w]];
    return t2*pw[dv] + t1;
}

int main(){
    scanf("%d", &n);
    scanf("%s", s+1);
    for(int i = 1; i <= n; i++){
        scanf("%d", &fa[i][0]);
        G[fa[i][0]].push_back(i);
    }
    init_hash(n);
    dfs(1, 0);
    for(int j = 1; j <= 16; j++){
        for(int i = 1; i <= n; i++){
            fa[i][j] = fa[fa[i][j-1]][j-1];
        }
    }
    scanf("%d", &q);
    while(q--){
        int u, v;
        scanf("%d%d", &u, &v);
        int lca = getlca(u, v);
        pii x = solve(u, lca, v);
        pii y = solve(v, lca, u);
        puts(x == y ? "YES" : "NO");
    }
    return 0;
}
### 关于牛客小白109的信息 目前并未找到关于牛客小白109的具体比赛信息或题解内容[^5]。然而,可以推测该事可能属于牛客网举办的系列算法之一,通常这类比赛会涉及数据结构、动态规划、图论等经典算法问题。 如果要准备类似的事,可以通过分析其他场次的比赛题目来提升自己的能力。例如,在牛客小白13中,有一道与二叉树相关的题目,其核心在于处理树的操作以及统计最终的结果[^3]。通过研究此类问题的解决方法,能够帮助理解如何高效地设计算法并优化时间复杂度。 以下是基于已有经验的一个通用解决方案框架用于应对类似场景下的批量更新操作: ```python class TreeNode: def __init__(self, id): self.id = id self.weight = 0 self.children = [] def build_tree(n): nodes = [TreeNode(i) for i in range(1, n + 1)] for node in nodes: if 2 * node.id <= n: node.children.append(nodes[2 * node.id - 1]) if 2 * node.id + 1 <= n: node.children.append(nodes[2 * node.id]) return nodes[0] def apply_operations(root, operations, m): from collections import defaultdict counts = defaultdict(int) def update_subtree(node, delta): stack = [node] while stack: current = stack.pop() current.weight += delta counts[current.weight] += 1 for child in current.children: stack.append(child) def exclude_subtree(node, total_nodes, delta): nonlocal root stack = [(root, False)] # (current_node, visited) subtree_size = set() while stack: current, visited = stack.pop() if not visited and current != node: stack.append((current, True)) for child in current.children: stack.append((child, False)) elif visited or current == node: if current != node: subtree_size.add(current.id) all_ids = {i for i in range(1, total_nodes + 1)} outside_ids = all_ids.difference(subtree_size.union({node.id})) for idx in outside_ids: nodes[idx].weight += delta counts[nodes[idx].weight] += 1 global nodes nodes = {} queue = [root] while queue: curr = queue.pop(0) nodes[curr.id] = curr for c in curr.children: queue.append(c) for operation in operations: op_type, x = operation.split(' ') x = int(x) target_node = nodes.get(x, None) if not target_node: continue if op_type == '1': update_subtree(target_node, 1) elif op_type == '2' and target_node is not None: exclude_subtree(target_node, n, 1) elif op_type == '3': path_to_root = [] temp = target_node while temp: path_to_root.append(temp) if temp.id % 2 == 0: parent_id = temp.id // 2 else: parent_id = (temp.id - 1) // 2 if parent_id >= 1: temp = nodes[parent_id] else: break for p in path_to_root: p.weight += 1 counts[p.weight] += 1 elif op_type == '4': pass # Implement similarly to other cases. result = [counts[i] for i in range(m + 1)] return result ``` 上述代码片段展示了针对特定类型的树形结构及其操作的一种实现方式。尽管它并非直接对应小白109中的具体题目,但它提供了一个可借鉴的设计思路。 ####
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值