日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【题解】【数组】【Prefix Sums】【Codility】Genomic Range Query

發布時間:2024/1/17 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【题解】【数组】【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,可能沒那么容易想到。

代碼:

1 vector<int> solution(string &S, vector<int> &P, vector<int> &Q) { 2 int n = S.length(); 3 vector<vector<int> > vACGT(4, vector<int>(1,0)); 4 int count[4] = {0,0,0,0}; 5 for(int i = 0; i < n; i++){ 6 switch(S[i]){ 7 case 'A': 8 count[0] += 1; 9 break; 10 case 'C': 11 count[1] += 1; 12 break; 13 case 'G': 14 count[2] += 1; 15 break; 16 case 'T': 17 count[3] += 1; 18 break; 19 } 20 for(int k = 0; k < 4; k++){ 21 vACGT[k].push_back(count[k]); 22 } 23 } 24 25 vector<int> vres; 26 for(int i = 0; i < P.size(); i++){ 27 for(int k = 0; k < 4; k++){ 28 if(vACGT[k][Q[i]+1]-vACGT[k][P[i]] > 0){ 29 vres.push_back(k+1); 30 break; 31 } 32 } 33 } 34 return vres; 35 }

?

轉載于:https://www.cnblogs.com/wei-li/p/GenomicRangeQuery.html

總結

以上是生活随笔為你收集整理的【题解】【数组】【Prefix Sums】【Codility】Genomic Range Query的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。