Cod sursa(job #586629)

Utilizator vladtarniceruVlad Tarniceru vladtarniceru Data 2 mai 2011 17:45:16
Problema Perb Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.28 kb
# include <cstdio>
# include <cstring>
using namespace std;

int n, m, i, j, div, nr, l, sol, _max, nprim;
int prim[] = {0, 1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47};
int x, y, a[610][610];
char s[610];
int ap[100];
int main (){
	freopen ("perb.in", "r", stdin);
	freopen ("perb.out", "w", stdout);
	
	scanf ("%d%d", &n, &m);
	scanf (" %s", s + 1); l = strlen (s) - 1;
	
	for (; m > 0; --m){
		scanf ("%d%d", &x, &y);
		if (a[x][y]) printf ("%d", a[x][y]);
		else{
			int num;
            num = y - x + 1;
			sol = 2000000000;
			for (nr = 1; prim[nr] <= num; ++nr){
				int mxt = 0;
				nprim = num / prim[nr];
				if (num % prim[nr] == 0){
					int lim = x + prim[nr] - 1;
					for (j = x; j <= lim; ++j){
						ap['A' - 'A'] = 0;
						ap['C' - 'A'] = 0;
						ap['G' - 'A'] = 0;
						ap['T' - 'A'] = 0;
						for (i = 0; i * prim[nr] < num; ++i)
							++ap[s[j + i * prim[nr]] - 'A'];
						_max = ap['A' - 'A'];
						if (_max < ap['C' - 'A']) _max = ap['C' - 'A'];
						if (_max < ap['G' - 'A']) _max = ap['G' - 'A'];
						if (_max < ap['T' - 'A']) _max = ap['T' - 'A'];
						mxt += nprim - _max;
					}
					if (sol > mxt && mxt) sol = mxt;
					mxt = 0;
				}
			}
			
			printf ("%d\n", sol);
			a[x][y] = sol;
		}
	}
	return 0;
}