java codility training Genomic-range-query

后端 未结 30 2260
悲哀的现实
悲哀的现实 2021-02-01 12:47

The task is:

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.

<
30条回答
  •  北恋
    北恋 (楼主)
    2021-02-01 13:30

    In case anyone cares about C:

    #include 
    
    struct Results solution(char *S, int P[], int Q[], int M) {    
        int i, a, b, N, *pA, *pC, *pG;
        struct Results result;
    
        result.A = malloc(sizeof(int) * M);
        result.M = M;
    
        // calculate prefix sums
        N = strlen(S);
        pA = malloc(sizeof(int) * N);
        pC = malloc(sizeof(int) * N);
        pG = malloc(sizeof(int) * N);
        pA[0] = S[0] == 'A' ? 1 : 0;
        pC[0] = S[0] == 'C' ? 1 : 0;
        pG[0] = S[0] == 'G' ? 1 : 0;
        for (i = 1; i < N; i++) {
            pA[i] = pA[i - 1] + (S[i] == 'A' ? 1 : 0);
            pC[i] = pC[i - 1] + (S[i] == 'C' ? 1 : 0);
            pG[i] = pG[i - 1] + (S[i] == 'G' ? 1 : 0);
        }
    
        for (i = 0; i < M; i++) {
            a = P[i] - 1;
            b = Q[i];
    
            if ((pA[b] - pA[a]) > 0) {
                result.A[i] = 1;
            } else if ((pC[b] - pC[a]) > 0) {
                result.A[i] = 2;
            } else if ((pG[b] - pG[a]) > 0) {
                result.A[i] = 3;
            } else {
                result.A[i] = 4;
            }
        }
    
    
        return result;
    }
    

提交回复
热议问题