Codeforces 828 E DNA Evolution(树状数组方法)

本文解析了CodeForces上的一道题目E,通过使用树状数组进行预处理,实现高效查询DNA序列中与另一序列碱基相同的数量。具体介绍了算法思路及C++实现代码。

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

题目地址:http://codeforces.com/contest/828/problem/E
题意:给你个DNA序列,有两个操作,一个是改变DNA序列里的一个碱基,另一个是查询l~r中有多少是和另一个序列(给出的序列不断重复形成的)碱基相同的,求出相同碱基的数量。
思路:因为操作的次数太多了,所以可以想到用树状数组去存,预处理好,那样查询就特别方便了,

TreeArray T[后面要查询的序列长度][在原序列的位置%序列长度(PS:把它化成一个一个的块)][碱基];

#include <iostream>
#include <cstring>
#include <string>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <cstdio>
#include <algorithm>
#define LL long long 
#define N 100010
#define M 50010
#define inf 0x3f3f3f3f
using namespace std;
const LL mod = 1e9 + 7;
const double eps = 1e-9;
struct TreeArray {
    int d[N];
    void add(int x, int y) {
        while (x<N) {
            d[x] += y;
            x += x&-x;
        }
    }
    int get(int x) {
        int ans = 0;
        while (x) {
            ans += d[x];
            x -= x&-x;
        }
        return ans;
    }
    int get(int l, int r) {
        return get(r) - get(l - 1);
    }
}T[11][11][4];
int solve(char s) {
    if (s == 'A') return 0;
    if (s == 'T') return 1;
    if (s == 'C') return 2;
    if (s == 'G') return 3;
}
int main() {
    cin.sync_with_stdio(false);
    int op, q, num, l, r;
    string str, s;
    while (cin >> str) {
        for (int i = 1; i <= 10; i++) {
            for (int j = 0; j < str.length(); j++) {
                T[i][(j + 1) % i][solve(str[j])].add(j + 1, 1);
            }
        }
        cin >> q;
        while (q--) {
            cin >> op;
            if (op == 1) {
                cin >> num >> s;
                for (int i = 1; i <= 10; i++) {
                    T[i][num%i][solve(str[num - 1])].add(num, -1);
                    T[i][num%i][solve(s[0])].add(num, 1);
                }
                str[num - 1] = s[0];
            }
            else {
                cin >> l >> r >> s;
                int len = s.length();
                int ans = 0;
                for (int i = 0; i < len; i++) {
                    ans += T[len][(l + i) % len][solve(s[i])].get(l, r);
                }
                cout << ans << endl;
            }
        }

    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值