【题解】【数组】【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 ...
随机推荐
- 转:Linux 安装 Mysql
前段时间安装了Mysql,但是有些问题,就想把他卸载了,重新安装一个,但是没想到在Linux卸载软件是一个很痛苦的事情. 我的Mysql是用命令的方式安装的,就是上一篇文章用到的那个命令(sudo ...
- 让Windows下的Tomcat将控制台信息记录到日志
在开发的过程中经常出现包冲突,却不知道怎么回事,可以在 catalina.bat 里面设置查看class加载日志 set CATALINA_OPTS=-server -Xdebug -Xnoage ...
- 进制转换器(十进制转n进制)
#include<stdio.h> #include<stdlib.h> #define MAXSIZE 100 /*链栈类型定义*/ typedef struct node ...
- 【干货来了】2014年K2房地产IT分享峰会
2014年K2房地产IT分享峰会已圆满落幕,嘉宾们纷纷出招,分享干货,现场妙语连珠不断,高潮迭起. 主题:流程驱动的地产业务管控平台 嘉宾:王寿欣(卓越地产战略与运营管理部 副总经理) 卓越地产应用K ...
- POJ 2559 Program C
Submit Status Practice POJ 2559 Description A histogram is a polygon composed of a sequence of recta ...
- linux下的powershell,pash试用手记
------1 概述------ 1.1 简单来说linux,unix是非常依赖脚本的,而win不是.win中有很多图形程序+c库,效率不比脚本差.点几下鼠标照样能完成需求.当 然,图形和字符是两码事 ...
- ProcessOn:功能强大的在线作图工具(HTML5)
ProcessOn是一款专业作图人员的社交网络,这里汇聚很多业界专家.学者,同时他们分享的作品又形成一个庞大的知识图库,你在学习专业知识的同时还可以结交一些志同道合的新朋友. ProcessOn核心设 ...
- matlab 画框(三) 画框并保存图像
initstate = [x y w h];%-----------------------------------------Show the tracking resultimshow(uint8 ...
- sql 语句中使用条件判断case then else end
sql 语句中使用条件判断case then else end范例: SELECT les.[nLessonNo] FROM BS_Lesson AS les WHERE les.[sClassCod ...
- Visual Studio 2015 预览版 - 支持跨平台开发Android/iOS应用程序(内置安卓模拟器)
微软最近的惊人举动真多,对普通消费者Office 移动版宣布免费,对开发者也发布了完全免费的 VS2013 社区版! 不仅如此,就连 .Net 开发框架环境也竟然「开源」并且跨平台支持 Mac 及 L ...