CodeForces 828E DNA Evolution(树状数组)题解
题意:给你一个串k,进行两个操作:
“1 a b”:把a位置的字母换成b
“2 l r s”:求l到r有多少个字母和s匹配,匹配的条件是这样:从l开始无限循环s形成一个串ss,然后匹配ss和指定区间的匹配个数,如图。

思路:用树状数组预处理。因为模板串是不断重复循环的,所以我们可以一个位置一个位置求。对于长len的模板串来说,如果位置i,j满足 i%len == j%len,那么i和j匹配时对模板串来说是一样的(匹配同一个字符)。所以我们定义node[字母][模板串长度][相对位置][位置]来遍历某个位置字符在所有可能的相对位置的情况。
参考:Codeforces - 828E DNA Evolution —— 很多棵树状数组
代码:
#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<cstring>
#include<string>
#include<sstream>
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#define ll long long
#define ull unsigned long long
using namespace std;
const int maxn = + ;
const int seed = ;
const int MOD = ;
const int INF = 0x3f3f3f3f;
char s[maxn];
int h[];
int node[][][][maxn];
int lowbit(int x){
return x&(-x);
}
void update(int letter, int x, int val){
for(int i = x; i < maxn; i += lowbit(i)){
for(int len = ; len <= ; len++){
node[letter][len][x % len][i] += val;
}
}
}
int sum(int letter, int x, int len, int pos){
int ans = ;
for(int i = x; i > ; i -= lowbit(i)){
ans += node[letter][len][pos][i];
}
return ans;
}
int query(int l, int r, int len, int letter, int pos){
return sum(letter, r, len, pos) - sum(letter, l - , len, pos);
}
int main(){
memset(node, , sizeof(node));
h['A'] = , h['G'] = , h['C'] = , h['T'] = ;
scanf("%s", s + );
int len = strlen(s + );
for(int i = ; i <= len; i++){
update(h[s[i]], i, );
}
int q;
scanf("%d", &q);
while(q--){
int o,u,v;
char ss[];
scanf("%d", &o);
if(o == ){
scanf("%d%s", &u, ss);
update(h[s[u]], u, -);
update(h[ss[]], u, );
s[u] = ss[];
}
else{
scanf("%d%d%s", &u, &v, ss);
len = strlen(ss);
int ans = ;
for(int i = ; i < len; i++){
ans += query(u, v, len, h[ss[i]], (u + i) % len);
}
printf("%d\n", ans);
}
}
return ;
}
/*
ATGCATGC
4
2 1 8 ATGC
2 2 6 TTT
1 4 T
2 2 6 TA
*/
CodeForces 828E DNA Evolution(树状数组)题解的更多相关文章
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) E. DNA Evolution 树状数组
E. DNA Evolution 题目连接: http://codeforces.com/contest/828/problem/E Description Everyone knows that D ...
- [Codeforces 1208D]Restore Permutation (树状数组)
[Codeforces 1208D]Restore Permutation (树状数组) 题面 有一个长度为n的排列a.对于每个元素i,\(s_i\)表示\(\sum_{j=1,a_j<a_i} ...
- Codeforces - 828E DNA Evolution —— 很多棵树状数组
题目链接:http://codeforces.com/contest/828/problem/E E. DNA Evolution time limit per test 2 seconds memo ...
- Codeforces 650D - Zip-line(树状数组)
Codeforces 题目传送门 & 洛谷题目传送门 我怕不是个 nt--一开始忽略了"询问独立"这个条件--然后就一直在想有什么办法维护全局 LIS--心态爆炸 首先离散 ...
- Codeforces 1139F Dish Shopping 树状数组套平衡树 || 平衡树
Dish Shopping 将每个物品拆成p 和 s 再加上人排序. 然后问题就变成了, 对于一个线段(L - R), 问有多少个(li, ri)满足 L >= li && R ...
- Codeforces 830B - Cards Sorting 树状数组
B. Cards Sorting time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- CodeForces 522D Closest Equals 树状数组
题意: 给出一个序列\(A\),有若干询问. 每次询问某个区间中值相等且距离最短的两个数,输出该距离,没有则输出-1. 分析: 令\(pre_i = max\{j| A_j = A_i, j < ...
- codeforces 589G G. Hiring(树状数组+二分)
题目链接: G. Hiring time limit per test 4 seconds memory limit per test 512 megabytes input standard inp ...
- CodeForces–830B--模拟,树状数组||线段树
B. Cards Sorting time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
随机推荐
- 【jQuery系列之插件】jQuery插件---exselect实现联动
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title> ...
- LeetCode——Pascal's Triangle II
Description: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3 ...
- Android技巧分享——Android开发超好用工具吐血推荐(转)
内容中包含 base64string 图片造成字符过多,拒绝显示
- 1.执行环境判断 window 或 self
window or self ? 在 underscore 的判断所处环境的代码中,似乎我们没有看到 window 对象的引用,其实,在浏览器环境下,self 保存的就是当前 window 对象的引用 ...
- asp.net SessionState模式的配置及使用
由于项目dll文件变动比较频繁,而保存登陆的状态又保存在Session中,所以导致用户经常无故掉线(PS:dll变动的时候导致Session).有一种方法可以长期保存session,那就是sessio ...
- Linux下手工卸载11.2 RAC(非MOS的deinstall方法)
思路来自于经典的<How to Proceed From a Failed 10g or 11.1 Oracle Clusterware (CRS) Installation (Doc ID 2 ...
- 程序入口函数和glibc及C++全局构造和析构
分类: CRT Machnasim 2011-06-15 17:45 144人阅读 评论(0) 收藏 举报 c++汇编linuxlist语言编译器 1,程序入口函数和初始化 操作系统在装载可执行文件后 ...
- centos shell编程4【分发系统】 服务器标准化 mkpasswd 生成密码的工具 expect讲解 expect传递参数 expect自动同步文件 expect指定host和要同步的文件 expect文件分发系统 expect自动发送密钥脚本 Linux脚本执行方式 第三十八节课
centos shell编程4[分发系统] 服务器标准化 mkpasswd 生成密码的工具 expect讲解 expect传递参数 expect自动同步文件 expect指定host和要 ...
- 005-java的Annotation
一.概述 Annotation,JDK1.5开始提供 二.基本定义 public @interface HelloWorld { } 1.使用@Interface定义,名称大写 2.使用@Target ...
- Spark中文文本分析建模
实用的朴素贝叶斯模型建模 建模过程主要是把文本转化成向量然后再作分析 数据格式: ,善良 美丽 ,丑陋 阴险 卑鄙 ,温和 ....... 注:前面是给文章贴的标签,后面是文章的分词,分词可以找关于分 ...