http://codility.com/demo/take-sample-test/genomicrangequery

这题有点意思。
一开始以为是RMQ或者线段树,但这样要O(n*logn)。考虑到只有四种字符,可以用数组记录每个字符i之前出现过几次。
二,查询区间是闭区间,所以要处理off by one的问题。

// you can also use includes, for example:
// #include <algorithm>
vector<int> solution(string &S, vector<int> &P, vector<int> &Q) {
// write your code in C++98
int len = S.length();
vector<int> A(len+1);
vector<int> C(len+1);
vector<int> G(len+1);
vector<int> T(len+1);
A[0] = 0;
C[0] = 0;
G[0] = 0;
T[0] = 0;
for (int i = 1; i <= len; i++) {
A[i] = A[i-1];
C[i] = C[i-1];
G[i] = G[i-1];
T[i] = T[i-1];
if (S[i-1] == 'A') {
A[i]++;
}
else if (S[i-1] == 'C') {
C[i]++;
}
else if (S[i-1] == 'G') {
G[i]++;
}
else if (S[i-1] == 'T') {
T[i]++;
}
}
vector<int> ans;
for (int i = 0; i < P.size(); i++) {
int p = P[i];
int q = Q[i] + 1;
if (A[q] - A[p] > 0) ans.push_back(1);
else if (C[q] - C[p] > 0) ans.push_back(2);
else if (G[q] - G[p] > 0) ans.push_back(3);
else if (T[q] - T[p] > 0) ans.push_back(4);
}
return ans;
}

  

*[codility]GenomicRangeQuery的更多相关文章

  1. GenomicRangeQuery /codility/ preFix sums

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

  2. Codility NumberSolitaire Solution

    1.题目: A game for one player is played on a board consisting of N consecutive squares, numbered from ...

  3. codility flags solution

    How to solve this HARD issue 1. Problem: A non-empty zero-indexed array A consisting of N integers i ...

  4. *[codility]Peaks

    https://codility.com/demo/take-sample-test/peaks http://blog.csdn.net/caopengcs/article/details/1749 ...

  5. *[codility]Country network

    https://codility.com/programmers/challenges/fluorum2014 http://www.51nod.com/onlineJudge/questionCod ...

  6. *[codility]AscendingPaths

    https://codility.com/programmers/challenges/magnesium2014 图形上的DP,先按照路径长度排序,然后依次遍历,状态是使用到当前路径为止的情况:每个 ...

  7. *[codility]MaxDoubleSliceSum

    https://codility.com/demo/take-sample-test/max_double_slice_sum 两个最大子段和相拼接,从前和从后都扫一遍.注意其中一段可以为0.还有最后 ...

  8. *[codility]Fish

    https://codility.com/demo/take-sample-test/fish 一开始习惯性使用单调栈,后来发现一个普通栈就可以了. #include <stack> us ...

  9. *[codility]CartesianSequence

    https://codility.com/programmers/challenges/upsilon2012 求笛卡尔树的高度,可以用单调栈来做. 维持一个单调递减的栈,每次进栈的时候记录下它之后有 ...

随机推荐

  1. OPENSSL中RSA私钥文件(PEM格式)解析【一】

    http://blog.sina.com.cn/s/blog_4fcd1ea30100yh4s.html 在PKCS#1 RSA算法标准中定义RSA私钥语法为: RSAPrivateKey ::= S ...

  2. 树莓派配置AP模式

    所需硬件:树莓派.无线网卡 1.查看无线网卡是否被识别 pi@raspberrypi ~ $ sudo lsusb Bus Device : ID : Standard Microsystems Co ...

  3. ASP.NET MVC模型绑定的6个建议(转载)

    ASP.NET MVC模型绑定的6个建议 发表于2011-08-03 10:25| 来源博客园| 31 条评论| 作者冠军 validationasp.netmvc.netasp 摘要:ASP.NET ...

  4. __nonnull 和 __nullable (Swift 和 Objective-C混编)

    苹果在 Xcode 6.3 以后,为了解决 Swift 与 OC 混编时的问题,引入了一个 Objective-C 的新特性:nullability annotations. 这一新特性的核心是两个新 ...

  5. table表格中加入<a>标签,使内容上下居中的方法。

    主要思路:定义好表格单元格的width和height,再加入<a>后,设置<a>的width=100%,height=100%填充整个单元格.那么此时设置上下左右居中样式就只需 ...

  6. struts2 package元素配置(转载)

    package 元素的所有属性及对应功能: Attribute Required Description name yes key to for other packages to reference ...

  7. Config spec rules for elements in subbranches

    Quote from:  Config spec rules for elements in subbranches The following is an example of a config s ...

  8. Markdown:纯文本进行网页排版的简单标记语言

    Markdown http://daringfireball.net/projects/markdown/ 2016-08-03 Markdown是一种标记语言,对纯文本使用简单的标记符号进行网页格式 ...

  9. opensuse13.1 双屏幕扩展

    最近搞一项j2eeweb工程,想要使用Linux平台编写程序.对比Ubuntu 14.04.Redhat.openSUSE,选择了最后这个. 安装的时候将/boot只分了100M,后来空间不足软件都安 ...

  10. mysql中的load命令使用方法

    使用mysql 中的load 命令,可以将txt 文件中的内容加载到数据库表中 使用mysql 中的load 命令,讲txt 文件中的内容加载到数据库表中,例如,创建table,名称是user,一个字 ...