1. 程式人生 > >Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) E. DNA Evolution 樹狀數組

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

數組 start let ted posit printf stat output limit

E. DNA Evolution time limit per test 2 seconds memory limit per test 512 megabytes input standard input output standard output

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.

Examples input
ATGCATGC
4
2 1 8 ATGC
2 2 6 TTT
1 4 T
2 2 6 TA
output
8
2
4
input
GAGTTGTTAA
6
2 3 4 TATGGTG
1 1 T
1 6 G
2 5 9 AGTAATA
1 10 G
2 2 6 TTGT
output
0
3
1
Note

Consider the first example. In the first query of second type all characters coincide, so the answer is 8. In the second query we compare string "TTTTT..." and the substring "TGCAT". There are two matches. In the third query, after the DNA change, we compare string "TATAT..."‘ with substring "TGTAT". There are 4 matches.

題意:給你一個字符串只有ATCG四個字符

   q個操作,

   1操作,將x位置修改成字符c

   2操作,求區間[l,r]去匹配e,e一直循環,求所有匹配的數目;

思路:數組數組,註意e的長度為[1,10];

   //表示 長度為i 以j為起點的循環節為i 字符為c的位置標記,詳見代碼;

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define LL long long
#define pi (4*atan(1.0))
#define eps 1e-14
#define bug(x)  cout<<"bug"<<x<<endl;
const int N=1e5+10,M=2e6+10,inf=1e9+10;
const LL INF=1e18+10,mod=1e9+7;

struct AYT
{
    int tree[N];
    int lowbit(int x)
    {
        return x&-x;
    }
    void update(int x,int c)
    {
        while(x<N)
        {
            tree[x]+=c;
            x+=lowbit(x);
        }
    }
    int query(int x)
    {
        int ans=0;
        while(x)
        {
            ans+=tree[x];
            x-=lowbit(x);
        }
        return ans;
    }
};
AYT tree[11][11][4];//表示 長度為i 以j為起點的循環節為i 字符為c的位置標記
int gc(char a)
{
    if(a==A)return 0;
    if(a==T)return 1;
    if(a==C)return 2;
    return 3;
}
char a[N],ch[10]="ATCG";
int main()
{
    scanf("%s",a+1);
    int n=strlen(a+1);
    for(int i=1; i<=10; i++)
    {
        for(int j=1; j<=i; j++)
        {
            for(int k=0; k<4; k++)
            {
                for(int l=j; l<=n; l+=i)
                    if(ch[k]==a[l])
                        tree[i][j][k].update(l,1);
            }
        }
    }
    int q;
    scanf("%d",&q);
    while(q--)
    {
        int t;
        scanf("%d",&t);
        if(t==1)
        {
            int x;
            char f[2];
            scanf("%d%s",&x,f);
            if(a[x]==f[0])continue;
            for(int i=1; i<=10; i++)
            {
                for(int j=1; j<=i; j++)
                {
                    if(x%i==j%i)
                    for(int k=0; k<4; k++)
                    {
                        if(a[x]==ch[k])
                            tree[i][j][k].update(x,-1);
                        if(f[0]==ch[k])
                            tree[i][j][k].update(x,1);
                    }
                }
            }
            a[x]=f[0];
        }
        else
        {
            int l,r,ans=0;
            char f[12];
            scanf("%d%d%s",&l,&r,f+1);
            int len=strlen(f+1);
            for(int i=1; i<=len; i++)
            {
                int x=gc(f[i]);
                int s=(l+i-1)%len?(l+i-1)%len:len;
                ans+=tree[len][s][x].query(r)-tree[len][s][x].query(l-1);
            }
            printf("%d\n",ans);
        }
    }
    return 0;
}

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