A non-empty zero-indexed string S is given. String S consists of N characters from the set of upper-case English letters A, C, G, T.

This string actually represents a DNA sequence, and the upper-case letters represent single nucleotides(核苷).

You are also given non-empty zero-indexed arrays P and Q consisting of M integers. These arrays represent queries about minimal nucleotides. We represent the letters of string S as integers 1, 2, 3, 4 in arrays P and Q, where A = 1, C = 2, G = 3, T = 4, and we assume that A < C < G < T.

Query K requires you to find the minimal nucleotide from the range (P[K], Q[K]), 0 ≤ P[i] ≤ Q[i] < N.

For example, consider string S = GACACCATA and arrays P, Q such that:

 P[0] = 0 Q[0] = 8 P[1] = 0 Q[1] = 2 P[2] = 4 Q[2] = 5 P[3] = 7 Q[3] = 7

The minimal nucleotides from these ranges are as follows:

  • (0, 8) is A identified by 1,
  • (0, 2) is A identified by 1,
  • (4, 5) is C identified by 2,
  • (7, 7) is T identified by 4.

the function should return the values [1, 1, 2, 4], as explained above.

Assume that:

  • N is an integer within the range [1..100,000];
  • M is an integer within the range [1..50,000];
  • each element of array P, Q is an integer within the range [0..N − 1];
  • P[i] ≤ Q[i];
  • string S consists only of upper-case English letters A, C, G, T.

Complexity:

  • expected worst-case time complexity is O(N+M);
  • expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments).

Elements of input arrays can be modified.

思路:

开四个Prefix Sums数组分别用来统计ACGT从m到n的个数,如果A个数为0就看C,如此类推。

如果不是事先知道这题应该用Prefix Sums,可能没那么容易想到。

代码:

 vector<int> solution(string &S, vector<int> &P, vector<int> &Q) {
int n = S.length();
vector<vector<int> > vACGT(, vector<int>(,));
int count[] = {,,,};
for(int i = ; i < n; i++){
switch(S[i]){
case 'A':
count[] += ;
break;
case 'C':
count[] += ;
break;
case 'G':
count[] += ;
break;
case 'T':
count[] += ;
break;
}
for(int k = ; k < ; k++){
vACGT[k].push_back(count[k]);
}
} vector<int> vres;
for(int i = ; i < P.size(); i++){
for(int k = ; k < ; k++){
if(vACGT[k][Q[i]+]-vACGT[k][P[i]] > ){
vres.push_back(k+);
break;
}
}
}
return vres;
}

【题解】【数组】【Prefix Sums】【Codility】Genomic Range Query的更多相关文章

  1. 【题解】【数组】【Prefix Sums】【Codility】Passing Cars

    A non-empty zero-indexed array A consisting of N integers is given. The consecutive elements of arra ...

  2. CF1303G Sum of Prefix Sums

    点分治+李超树 因为题目要求的是树上所有路径,所以用点分治维护 因为在点分治的过程中相当于将树上经过当前$root$的一条路径分成了两段 那么先考虑如何计算两个数组合并后的答案 记数组$a$,$b$, ...

  3. CodeForces 837F - Prefix Sums | Educational Codeforces Round 26

    按tutorial打的我血崩,死活挂第四组- - 思路来自FXXL /* CodeForces 837F - Prefix Sums [ 二分,组合数 ] | Educational Codeforc ...

  4. CodeForces 1204E"Natasha, Sasha and the Prefix Sums"(动态规划 or 组合数学--卡特兰数的应用)

    传送门 •参考资料 [1]:CF1204E Natasha, Sasha and the Prefix Sums(动态规划+组合数) •题意 由 n 个 1 和 m 个 -1 组成的 $C_{n+m} ...

  5. Codeforces 837F Prefix Sums

    Prefix Sums 在 n >= 4时候直接暴力. n <= 4的时候二分加矩阵快速幂去check #include<bits/stdc++.h> #define LL l ...

  6. elasticsearch term 查询二:Range Query

    Range Query 将文档与具有一定范围内字词的字段进行匹配. Lucene查询的类型取决于字段类型,对于字符串字段,TermRangeQuery,对于数字/日期字段,查询是NumericRang ...

  7. SuRF : Practical Range Query Filtering with Fast Succinct Tries

    1. Introduction 在数据库管理系统中查找某些关键字会导致很大的磁盘I/O开销,针对这一问题,通常会使用一个内存开销小并且常驻内存的过滤器来检测该关键字是否存.比如现在常用的bloom过滤 ...

  8. Educational Codeforces Round 26 [ D. Round Subset ] [ E. Vasya's Function ] [ F. Prefix Sums ]

    PROBLEM D - Round Subset 题 OvO http://codeforces.com/contest/837/problem/D 837D 解 DP, dp[i][j]代表已经选择 ...

  9. GenomicRangeQuery /codility/ preFix sums

    首先上题目: A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which ...

随机推荐

  1. kafka技术要点

    转载:http://blog.csdn.net/caisini_vc/article/details/48007297 Kafka是分布式发布-订阅消息系统.它最初由LinkedIn公司开发,之后成为 ...

  2. EFCode First 导航属性

    首先谈谈自己对EF的接触的过程吧,最先接触EF只是因为EF支持从数据库把关系扒下来,可以省掉自己写Select.Update.Insert这些SQL语句,而且修改非常方便,后来在使用的过程中发现导航属 ...

  3. Time, Clocks, and the Ordering of Events in a Distributed System

    作者:Leslie Lamport(非常厉害的老头了) 在使用消息进行通信的分布式系统中,使用物理时钟对不同process进行时间同步与事件排序是非常困难的.一是因为不同process的时钟有差异,另 ...

  4. GCJ 2015-Qualification-C Dijkstra 特殊注意,展开 难度:2

    https://code.google.com/codejam/contest/6224486/dashboard#s=p2 题目中的新运算满足传递性不满足自反性,满足传递性则可以先计算后面的部分再计 ...

  5. Operating System Concepts with java 项目: Shell Unix 和历史特点

    线程间通信,fork(),waitpid(),signal,捕捉信号,用c执行shell命令,共享内存,mmap 实验要求: 1.简单shell: 通过c实现基本的命令行shell操作,实现两个函数, ...

  6. 二模 (5)day1

    第一题: 题目大意:解一元一次方程(只有+-符号): 解题过程:直接处理处两边的x的系数和常数项,字符串的处理即可. 第二题: 题目大意:求逆序对数. 解题过程:直接归并排序. 第三题: 题目大意:多 ...

  7. ie9,10 uploadify cleanUp bug

    起因:ie多次加载uploadify3.2版本这个组件的时候,出现了SCRIPT5007: 缺少对象.  From:http://blog.163.com/xiangfei209@126/blog/s ...

  8. POJ 1777 mason素数

    题目大意: 给定数列 a1 , a2 , ... , an 希望找到一个  N = sigma(ai^ki)  , (0<=ki<10) ,ki可随自己定为什么 只要保证N的因子和可以表示 ...

  9. 后台获取不规则排列RadioButton组的值

    获取多个RadioButton的值,我们一般会使用服务器控件RadioButtonList: <asp:RadioButtonList ID="rbl" runat=&quo ...

  10. 解决json跨域时错误:SyntaxError: invalid label

    将数据做以下返回: $callback = $_GET['callback']; echo $callback.'('.json_encode(array('html'=>$html)).')' ...