E. DNA Evolution

题目连接:

http://codeforces.com/contest/828/problem/E

Description

Everyone knows that DNA strands consist of nucleotides. There are four types of nucleotides: "A", "T", "G", "C". A DNA strand is a sequence of nucleotides. Scientists decided to track evolution of a rare species, which DNA strand was string s initially.

Evolution of the species is described as a sequence of changes in the DNA. Every change is a change of some nucleotide, for example, the following change can happen in DNA strand "AAGC": the second nucleotide can change to "T" so that the resulting DNA strand is "ATGC".

Scientists know that some segments of the DNA strand can be affected by some unknown infections. They can represent an infection as a sequence of nucleotides. Scientists are interested if there are any changes caused by some infections. Thus they sometimes want to know the value of impact of some infection to some segment of the DNA. This value is computed as follows:

Let the infection be represented as a string e, and let scientists be interested in DNA strand segment starting from position l to position r, inclusive.

Prefix of the string eee... (i.e. the string that consists of infinitely many repeats of string e) is written under the string s from position l to position r, inclusive.

The value of impact is the number of positions where letter of string s coincided with the letter written under it.

Being a developer, Innokenty is interested in bioinformatics also, so the scientists asked him for help. Innokenty is busy preparing VK Cup, so he decided to delegate the problem to the competitors. Help the scientists!

Input

The first line contains the string s (1 ≤ |s| ≤ 105) that describes the initial DNA strand. It consists only of capital English letters "A", "T", "G" and "C".

The next line contains single integer q (1 ≤ q ≤ 105) — the number of events.

After that, q lines follow, each describes one event. Each of the lines has one of two formats:

1 x c, where x is an integer (1 ≤ x ≤ |s|), and c is a letter "A", "T", "G" or "C", which means that there is a change in the DNA: the nucleotide at position x is now c.

2 l r e, where l, r are integers (1 ≤ l ≤ r ≤ |s|), and e is a string of letters "A", "T", "G" and "C" (1 ≤ |e| ≤ 10), which means that scientists are interested in the value of impact of infection e to the segment of DNA strand from position l to position r, inclusive.

Output

For each scientists' query (second type query) print a single integer in a new line — the value of impact of the infection on the DNA.

Sample Input

ATGCATGC

4

2 1 8 ATGC

2 2 6 TTT

1 4 T

2 2 6 TA

Sample Output

8

2

4

Hint

题意

给你一个字符串,然后你有一些操作:

1.单点更新

2.查询区间[l,r]中,有多少个字符能和所给的字符串匹配(字符串循环匹配)

题解:

树状数组,由于查询的字符串最长10,且字符集直邮4,所以我们直接暴力对T[10][10][4]建树,T[i][j][k]表示这个为长度为i,该字符的位置是j%i,该字符是k,然后去讨论即可。

代码

#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std; const int maxn = 1e5+7;
struct bit{
int a[maxn];
int lowbit(int x){
return x&(-x);
}
void update(int x,int v){
for(int i=x;i<maxn;i+=lowbit(i)){
a[i]+=v;
}
}
int get(int x){
int sum=0;
for(int i=x;i;i-=lowbit(i)){
sum+=a[i];
}
return sum;
}
int get(int l,int r){
return get(r)-get(l-1);
}
}T[11][11][4];
char s[maxn];
int q;
int getid(char x){
if(x=='A')return 0;
if(x=='T')return 1;
if(x=='C')return 2;
if(x=='G')return 3;
}
int main(){
scanf("%s",s+1);
int len = strlen(s+1);
for(int j=1;j<=10;j++)
for(int i=1;i<=len;i++)
T[j][i%j][getid(s[i])].update(i,1);
scanf("%d",&q);
while(q--){
int op;
scanf("%d",&op);
if(op==1){
int l;
char t[10];
scanf("%d%s",&l,t);
for(int i=1;i<=10;i++){
T[i][l%i][getid(t[0])].update(l,1);
T[i][l%i][getid(s[l])].update(l,-1);
}
s[l]=t[0];
}else{
int l,r;
char t[10];
scanf("%d%d%s",&l,&r,t);
int len2=strlen(t);
int ans = 0;
for(int i=0;i<len2;i++){
ans+=T[len2][(l+i)%len2][getid(t[i])].get(l,r);
}
printf("%d\n",ans);
}
}
}

Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) E. DNA Evolution 树状数组的更多相关文章

  1. 【树状数组】Codeforces Round #423 (Div. 1, rated, based on VK Cup Finals) C. DNA Evolution

    题意跟某道我出的等差子序列求最值非常像…… 反正询问的长度只有10种,你就建立10批树状数组,每组的公差是确定的,首项不同. 然后询问的时候只需要枚举询问串的每一位,找找这一位对应哪棵树状数组即可. ...

  2. Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) E DNA Evolution

    DNA Evolution 题目让我们联想到树状数组或者线段树,但是如果像普通那样子统计一段的和,空间会爆炸. 所以我们想怎样可以表示一段区间的字符串. 学习一发大佬的解法. 开一个C[10][10] ...

  3. Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) E. Cards Sorting 树状数组

    E. Cards Sorting time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  4. Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Cards Sorting(树状数组)

    Cards Sorting time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  5. Codeforces Round #423 (Div. 1, rated, based on VK Cup Finals)

    Codeforces Round #423 (Div. 1, rated, based on VK Cup Finals) A.String Reconstruction B. High Load C ...

  6. Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem E (Codeforces 828E) - 分块

    Everyone knows that DNA strands consist of nucleotides. There are four types of nucleotides: "A ...

  7. Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) D. High Load 构造

    D. High Load 题目连接: http://codeforces.com/contest/828/problem/D Description Arkady needs your help ag ...

  8. Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) C. String Reconstruction 并查集

    C. String Reconstruction 题目连接: http://codeforces.com/contest/828/problem/C Description Ivan had stri ...

  9. Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) A,B,C

    A.题目链接:http://codeforces.com/contest/828/problem/A 解题思路: 直接暴力模拟 #include<bits/stdc++.h> using ...

随机推荐

  1. VS2017使用文档

    参考链接:https://docs.microsoft.com/zh-cn/visualstudio/debugger/?view=vs-2017

  2. mybatis_generator_逆向工程的使用笔记

    1:解压mybatis_generator_1.3.1.zip文件. 2:把features,pougins文件夹copy到D:\java\eclipse\eclipse目录下(D:\java\ecl ...

  3. Windows 7下java SDK下载、安装及环境变量设置

    第一步:下载Java JDK   1 登录官网站下载正版JDK   2 点击"SDK Download"进入Java JDK下载页面 注明: Java JDK和Java JRE区别 ...

  4. 【Android】Android 多个APK数据共享

    Android给每个APK进程分配一个单独的用户空间,其manifest中的userid就是对应一个Linux用户(Android 系统是基于Linux)的.所以不同APK(用户)间互相访问数据默认是 ...

  5. IIS:另一个程序正在使用此文件进程无法访问。

    启动网站时,遇到这个错误,一般是端口已经被占用,更换一个空闲端口即可. 通过以下命令可查询 根据最后一列的数字在任务管理器中可查看被哪个程序占用了

  6. Python学习(十六)—— 数据库

    一.数据库介绍 数据库(Database,DB)是按照数据结构来组织.存储和管理数据的,并且是建立在计算机存储设备上的仓库. 数据库指的是以一定方式存储在一起.能为多个用户共享.具有尽可能小的冗余度. ...

  7. HDU5470 Typewriter SAM 动态规划 单调队列

    原文链接https://www.cnblogs.com/zhouzhendong/p/HDU5470.html 题目传送门 - HDU5470 题意 你需要写一个只包含小写字母的字符串 $s$ . 你 ...

  8. POJ1330Nearest Common Ancestors

    去博客园看该题解 题意 第一行输入T,有T组数据. 对于每组数据,给出一棵树,先输入n,然后n-1行,每行两个数a,b,表示a是b的父亲:第n行输入两个数A,B表示询问A和B的最近公共祖先. 题解 L ...

  9. java读取配置文件方法以及工具类

    第一种方式 : java工具类读取配置文件工具类 只是案例代码  抓取异常以后的代码自己处理 import java.io.FileNotFoundException; import java.io. ...

  10. Selenium3详解(基本操作,定位方法)

    如果想使用selenium驱动不同的浏览器,必须单独下载并设置不同的浏览器驱动. 基本操作: 刷新:refresh, 获取浏览器窗口大小:get_window_size 设置浏览器窗口大小:set_w ...