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. Asp.net MVC 4 Attributes特性

    Attributes特性 ActionFilterAttribute Represents the base class for filter attributes. 代表筛选器属性的基类. Acti ...

  2. 再也不要看到Eclipse万恶的arg0,arg1提示

    不知道大家跟我是否一下,遇到arg的提示. @Override public void onDateChanged(DatePicker arg0, int arg1, int arg2, int a ...

  3. 7zip self-extracted executable: To extract file to specific directory

    1) Install 7-zip (7zS.sfx will be installed to C:\Program Files\7-Zip): http://7zsfx.solta.ru/en/ 2) ...

  4. STL容器与配接器

    STL容器包括顺序容器.关联容器.无序关联容器 STL配接器包括容器配接器.函数配接器 顺序容器: vector                             行为类似于数组,但可以根据要求 ...

  5. php的运行环境介绍

    php软件已下载在我的百度云:页面底部有地址,如有需要欢迎下载! 一:如何让php环境运行php代码? 直接使用php软件直接运行代码文件中的php代码 在B/S结构中让Apache使用php软件运行 ...

  6. shell awk

    #!/bin/bash # ;i<=;i++)); # do # c1=`shuf -i - -n ` #生成随机数 # c2=`shuf -i - -n ` # c3=`shuf -i - - ...

  7. 深入理解javascript中的闭包!(转)

    1.闭包的经典错误 假如页面上有若干个div,我们想给它每个绑定一个onclick方法,于是有了下面的代码. function A(){ var divs=document.getElementsBy ...

  8. java Object类

    常用的共性内容 1,实现任何对象的比较,一般比较同一种对象的比较 Object1.equals(Object obj);等同于Object1 == obj: 只有当两个引用指向同一个对象时方法返回tr ...

  9. 自定义弹出div对话框

    <style type="text/css"> html,body{height:100%;overflow:hidden;} body,div,h2{margin:0 ...

  10. Javascript的websocket的使用方法

    javascript websocket接口 web实现客户端和服务端双向发送消息的方法有: 轮询,客户端定期向服务端请求: 长轮询,客户端定期向服务端请求,服务端只有有信息发送的时候才返回respo ...