algo

Just a bunch of algorithm implementations
Log | Files | Refs | README

grq.c (1074B)


      1 #include <stdio.h>
      2 #include <string.h>
      3 #include <stdlib.h>
      4 /* GenomiRangeQuery */
      5 typedef struct Results Results;
      6 struct Results {
      7 	int *A;
      8 	int M;
      9 };
     10 
     11 Results solution(char *, int *, int *, int);
     12 
     13 int
     14 main(void)
     15 {
     16 	char s[] = "CAGCCTA";
     17 	int P[] = {2, 5, 0};
     18 	int Q[] = {4, 5, 6};
     19 	int M = 3;
     20 
     21 	Results res = solution(s, P, Q, M);
     22 	for (int i = 0; i < res.M; i++)
     23 		printf("%d", res.A[i]);
     24 
     25 	free(res.A);
     26 	
     27 	return 0;
     28 }
     29 
     30 Results
     31 solution(char *S, int P[], int Q[], int M)
     32 {
     33 	Results r;
     34 
     35 	r.A = malloc(M * sizeof(int));
     36 	for (int i = 0; i < M; i++)
     37 		r.A[i] = 4;
     38 	r.M = M;
     39 	
     40 	for (int i = 0; i < M; i++) {
     41 		int p = P[i];
     42 		int q = Q[i];
     43 		int t;
     44 		
     45 		for (int j = p; j <= q; j++) {
     46 			switch (S[j]) {
     47 			case 'A':
     48 				t = 1;
     49 				if (r.A[i] > t)
     50 					r.A[i] = t;
     51 				break;
     52 			case 'C':
     53 				t = 2;
     54 				if (r.A[i] > t)
     55 					r.A[i] = t;
     56 				break;
     57 			case 'G':
     58 				t = 3;
     59 				if (r.A[i] > t)
     60 					r.A[i] = t;
     61 				break;
     62 			case 'T':
     63 				t = 4;
     64 				if (r.A[i] > t)
     65 					r.A[i] = t;
     66 				break;
     67 			default:
     68 				printf("wrong sequence\n");
     69 				return r;
     70 			}
     71 		}
     72 	}
     73 
     74 	return r;
     75 }