GenomicRangeQuery /codility/ preFix sums
首先上题目:
A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which correspond to the types of successive nucleotides in the sequence. Each nucleotide has an impact factor, which is an integer. Nucleotides of types A, C, G and T have impact factors of 1, 2, 3 and 4, respectively. You are going to answer several queries of the form: What is the minimal impact factor of nucleotides contained in a particular part of the given DNA sequence?
The DNA sequence is given as a non-empty string S =S[0]S[1]...S[N-1] consisting of N characters. There are M queries, which are given in non-empty arrays P and Q, each consisting of M integers. The K-th query (0 ≤ K < M) requires you to find the minimal impact factor of nucleotides contained in the DNA sequence between positions P[K] and Q[K] (inclusive).
For example, consider string S = CAGCCTA and arrays P, Q such that:
P[0] = 2 Q[0] = 4
P[1] = 5 Q[1] = 5
P[2] = 0 Q[2] = 6
The answers to these M = 3 queries are as follows:
- The part of the DNA between positions 2 and 4 contains nucleotides G and C (twice), whose impact factors are 3 and 2 respectively, so the answer is 2.
- The part between positions 5 and 5 contains a single nucleotide T, whose impact factor is 4, so the answer is 4.
- The part between positions 0 and 6 (the whole string) contains all nucleotides, in particular nucleotide Awhose impact factor is 1, so the answer is 1.
Assume that the following declarations are given:
struct Results {
int * A;
int M;
};
Write a function:
struct Results solution(char *S, int P[], int Q[], int M);
that, given a non-empty zero-indexed string S consisting of N characters and two non-empty zero-indexed arrays P and Q consisting of M integers, returns an array consisting of M integers specifying the consecutive answers to all queries.
The sequence should be returned as:
- a Results structure (in C), or
- a vector of integers (in C++), or
- a Results record (in Pascal), or
- an array of integers (in any other programming language).
For example, given the string S = CAGCCTA and arrays P, Q such that:
P[0] = 2 Q[0] = 4
P[1] = 5 Q[1] = 5
P[2] = 0 Q[2] = 6
the function should return the values [2, 4, 1], 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 arrays P, Q is an integer within the range [0..N − 1];
- P[K] ≤ Q[K], where 0 ≤ K < M;
- 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.
Copyright 2009–2015 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
2.题目分析
这个题目看上去蛮简单,就是在一个序列中的任意一段slice中,找出出现过的最小值。
不过要求时间复杂度为O(N),便费了一些周折。
1. 首先是将str转换为int 数组,为后面的处理加速,免去每次都要遍历。
先用strlen函数获得str的长度,再通过temp 指针遍历这个str,将A转为1,C转为2,G转为3,T转为4。
获得了这个数组之后就比较好操作了~
2. 获得任何段的最小值看似很简单,遍历就可以了,不过这样时间复杂度就会变为O(N*M)。
如何减少时间复杂度呢?答案是用空间换时间。
通过prefix的想法,我先统计出了在每一个位置之前包括这个位置,1所出现的次数,2所出现的次数,3所出现的次数,4所出现的次数。
分别存入数组A,B,C,D;
每次查询时,只要判断A[end of slice]-A[Begin of Slice]是否大于零,即可判断是否1出现过。这一步的时间复杂度就从O(N)降为了O(1);
注意,需要检查str[begin of slice]是否为1,因为当这个位置是1时,是不会表现出来的。
其余查询依然,通过增加空间复杂度,降低了时间的复杂度。
找出最小的出现值即可。
3.代码如下:
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n"); struct Results solution(char *S, int P[], int Q[], int M) {
struct Results result;
// write your code in C99
int len = strlen(S);
// printf("len is %d",len); int arr[len];
int i;
char* temp = S;
for(i=;i<len;i++)
{
if(*temp=='C')
{
arr[i]=;
}
else if(*temp=='A')
{
arr[i]=;
}
else if(*temp=='G')
{
arr[i]=;
}
else if(*temp=='T')
{
arr[i]=;
}
temp++;
} int A[len];
int B[len];
int C[len];
int D[len]; if(arr[]==)
{
A[]=;
B[]=;
C[]=;
D[]=;
} if(arr[]==)
{
A[]=;
B[]=;
C[]=;
D[]=;
} if(arr[]==)
{
A[]=;
B[]=;
C[]=;
D[]=;
} if(arr[]==)
{
A[]=;
B[]=;
C[]=;
D[]=;
} // printf("%d %d %d %d \n",A[0],B[0],C[0],D[0]);
for(i=;i<len;i++)
{
// printf("%d\n",arr[i]);
if(arr[i]==)
{
A[i]=A[i-]+;
B[i]=B[i-];
C[i]=C[i-];
D[i]=D[i-];
} if(arr[i]==)
{
A[i]=A[i-];
B[i]=B[i-]+;
C[i]=C[i-];
D[i]=D[i-];
} if(arr[i]==)
{
A[i]=A[i-];
B[i]=B[i-];
C[i]=C[i-]+;
D[i]=D[i-];
} if(arr[i]==)
{
A[i]=A[i-];
B[i]=B[i-];
C[i]=C[i-];
D[i]=D[i-]+;
} // printf("%d %d %d %d \n",A[i],B[i],C[i],D[i]);
}
result.A = malloc(sizeof(int)*M);
result.M = M; for(i=;i<M;i++)
{
int tempP = P[i];
int tempQ = Q[i];
if(arr[tempP]== || (A[tempQ]-A[tempP]) > )
{
result.A[i]=;
}
else if(arr[tempP]== || (B[tempQ]-B[tempP]) > )
{
result.A[i]=;
}
else if(arr[tempP]== || (C[tempQ]-C[tempP]) > )
{
result.A[i]=;
}
else
{
result.A[i]=;
}
} return result;
}
GenomicRangeQuery /codility/ preFix sums的更多相关文章
- 【题解】【数组】【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- ...
- 【题解】【数组】【Prefix Sums】【Codility】Passing Cars
A non-empty zero-indexed array A consisting of N integers is given. The consecutive elements of arra ...
- Codeforces 837F Prefix Sums
Prefix Sums 在 n >= 4时候直接暴力. n <= 4的时候二分加矩阵快速幂去check #include<bits/stdc++.h> #define LL l ...
- CodeForces 837F - Prefix Sums | Educational Codeforces Round 26
按tutorial打的我血崩,死活挂第四组- - 思路来自FXXL /* CodeForces 837F - Prefix Sums [ 二分,组合数 ] | Educational Codeforc ...
- 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]代表已经选择 ...
- CodeForces 1204E"Natasha, Sasha and the Prefix Sums"(动态规划 or 组合数学--卡特兰数的应用)
传送门 •参考资料 [1]:CF1204E Natasha, Sasha and the Prefix Sums(动态规划+组合数) •题意 由 n 个 1 和 m 个 -1 组成的 $C_{n+m} ...
- CF1303G Sum of Prefix Sums
点分治+李超树 因为题目要求的是树上所有路径,所以用点分治维护 因为在点分治的过程中相当于将树上经过当前$root$的一条路径分成了两段 那么先考虑如何计算两个数组合并后的答案 记数组$a$,$b$, ...
- codeforces:Prefix Sums分析和实现
题目大意: 给出一个函数P,P接受一个数组A作为参数,并返回一个新的数组B,且B.length = A.length + 1,B[i] = SUM(A[0], ..., A[i]).有一个无穷数组序列 ...
- [CF1204E]Natasha,Sasha and the Prefix Sums 题解
前言 本文中的排列指由n个1, m个-1构成的序列中的一种. 题目这么长不吐槽了,但是这确实是一道好题. 题解 DP题话不多说,直接状态/变量/转移. 状态 我们定义f表示"最大prefix ...
随机推荐
- Android APP 读取 AndroidManifest.xml 中的版本信息详解
APP都会涉及到版本的问题,Android APP的版本信息保存在AndroidManifest.xml文件的顶部.如下图: 有2个属性表示,“android:versionCode”和“androi ...
- Neutron 理解(14):Neutron ML2 + Linux bridge + VxLAN 组网
学习 Neutron 系列文章: (1)Neutron 所实现的虚拟化网络 (2)Neutron OpenvSwitch + VLAN 虚拟网络 (3)Neutron OpenvSwitch + GR ...
- HTML 学习笔记 JavaScript (prototype)
原博地址:http://www.cnblogs.com/dolphinX/p/3286177.html 原博客的作者是一个非常牛逼的前端大神,我作为一个初学者,在此借助大神的博客进行自己的学习.在这里 ...
- 关于Advanced Installer 11.0打包软件过程一些记录
1.想要安装后在默认网站下为一个虚拟目录,IIs,Web应用程序,选中虚拟目录是一个Web应用程序.同时修改常规>文件夹,重新指向网站文件所在目录,不然会少一级目录 2.如果想单独的应用程序池, ...
- ubuntu12.04中shell脚本无法使用source的原因及解决方法
现象: shell脚本中source aaa.sh时提示 source: not found 原因: ls -l `which sh` 提示/bin/sh -> dash 这说明是用dash来进 ...
- [转有改动]vi
转自http://www.51testing.com/html/86/427686-247344.html 多按几次[ESC],系统会发出滴滴声以确定进入命令模式.就进入了命令模式,所有在键盘上打的字 ...
- Qt——动态库的创建和使用
一.动态库是什么 很多人写程序的人都见过.lib和.dll文件,对动态库也略有耳闻. 生成动态库后可以得到两个文件,后缀名分别是.lib以及.dll. 简而言之,.lib称为导入库,相当于头文件:.d ...
- TCP/IP、Http、Socket、XMPP-从入门到深入
TCP/IP.Http.Socket.XMPP-从入门到深入 终极iOS程序猿 2016-12-29 18:27 为了便于大家理解和记忆,我们先对这几个概念进行的介绍,然后分析他们的不同,再进行详细的 ...
- 极光推送JPush的快速集成
首先到极光推送的官网上创建一个应用,填写对应的应用名和包名. 创建好之后下载Demo 提取Sdk里面的图片和xml等资源文件放自己项目的相应位置,然后要注意的是.so文件的放置位置: 在main目录下 ...
- springmvc请求接收参数的几种方法
一.通过@PathVariable获取路径中的参数 @RequestMapping(value="user/{id}/{name}",method=RequestMethod.GE ...