【题解】【数组】【Prefix Sums】【Codility】Genomic Range Query
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的更多相关文章
- 【题解】【数组】【Prefix Sums】【Codility】Passing Cars
A non-empty zero-indexed array A consisting of N integers is given. The consecutive elements of arra ...
- CF1303G Sum of Prefix Sums
点分治+李超树 因为题目要求的是树上所有路径,所以用点分治维护 因为在点分治的过程中相当于将树上经过当前$root$的一条路径分成了两段 那么先考虑如何计算两个数组合并后的答案 记数组$a$,$b$, ...
- CodeForces 837F - Prefix Sums | Educational Codeforces Round 26
按tutorial打的我血崩,死活挂第四组- - 思路来自FXXL /* CodeForces 837F - Prefix Sums [ 二分,组合数 ] | Educational Codeforc ...
- CodeForces 1204E"Natasha, Sasha and the Prefix Sums"(动态规划 or 组合数学--卡特兰数的应用)
传送门 •参考资料 [1]:CF1204E Natasha, Sasha and the Prefix Sums(动态规划+组合数) •题意 由 n 个 1 和 m 个 -1 组成的 $C_{n+m} ...
- Codeforces 837F Prefix Sums
Prefix Sums 在 n >= 4时候直接暴力. n <= 4的时候二分加矩阵快速幂去check #include<bits/stdc++.h> #define LL l ...
- elasticsearch term 查询二:Range Query
Range Query 将文档与具有一定范围内字词的字段进行匹配. Lucene查询的类型取决于字段类型,对于字符串字段,TermRangeQuery,对于数字/日期字段,查询是NumericRang ...
- SuRF : Practical Range Query Filtering with Fast Succinct Tries
1. Introduction 在数据库管理系统中查找某些关键字会导致很大的磁盘I/O开销,针对这一问题,通常会使用一个内存开销小并且常驻内存的过滤器来检测该关键字是否存.比如现在常用的bloom过滤 ...
- 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]代表已经选择 ...
- GenomicRangeQuery /codility/ preFix sums
首先上题目: A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which ...
随机推荐
- BZOJ1747 [Usaco2005 open]Expedition 探险
首先我们可以发现如果错过了一个加油站,而继续往前走的时候没有油了,可以再假装之前经过加油站的时候加过油 于是我们维护一个大根堆,表示错过的加油站是哪些,每当没有油的时候从堆顶取出最大值加上去即可 /* ...
- Html5编辑工具
如果你是一名Web开发人员,当你需要开发一个独特的网站时,你就会知道文本编辑器的重要性.小编为大家整理了8款非常前沿的HTML5文本编辑器,简化开发流程,喜欢就转走吧! Mercury Editor ...
- Spring学习(一)——Spring中的依赖注入简介【转】
[前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring. ...
- HDU 3085 Nightmare II 双向bfs 难度:2
http://acm.hdu.edu.cn/showproblem.php?pid=3085 出的很好的双向bfs,卡时间,普通的bfs会超时 题意方面: 1. 可停留 2. ghost无视墙壁 3. ...
- php解压zip文件
<?php header("Content-type:text/html;charset=utf-8"); function get_zip_originalsize($fi ...
- NOIP 2013 提高组 day2 积木大赛
积木大赛 描述 春春幼儿园举办了一年一度的“积木大赛”.今年比赛的内容是搭建一座宽度为 n 的大厦,大厦可以看成由 n 块宽度为1的积木组成,第
- Rhel6-mailsystem配置文档
(postfix+dovecot+mysql+extmail) 理论基础:
- SharePoint加K2,将Portal系统与BPM系统完美整合!
K2 blackPearl与Microsoft Office SharePoint 一起为解决人员和流程相互合作的解决方案而提供一个强大的平台. K2“blackpearl”根据企业的需求提供了设计, ...
- swing LayoutManager 和多态
interface LayoutManager{ void show();}class FlowLayout implements LayoutManager{ public void show(){ ...
- [windows驱动]基本概念
https://msdn.microsoft.com/zh-cn/library/windows/hardware/ff554721 1.设备节点和设备堆栈 在windows中,设备通过即插即用设备树 ...