Pagini recente » Cod sursa (job #2299658) | Cod sursa (job #664906) | Cod sursa (job #1921349) | Cod sursa (job #2890070) | Cod sursa (job #587847)
Cod sursa(job #587847)
# include <cstdio>
# include <cstring>
const char *FIN = "perb.in", *FOU = "perb.out", *adn = { "ACGT" } ;
const size_t MAX = 605 ;
char S[MAX] ;
size_t A[MAX][MAX], B[MAX][MAX] ;
size_t N, M ;
# define verf ++poz == hg ? fread ( ch, 1, hg, stdin ), poz = 0 : 0
inline size_t max ( size_t a, size_t b ) {
return (a > b ? a : b) ;
}
inline size_t min ( size_t a, size_t b ) {
return (a < b ? a : b) ;
}
const size_t hg = 1 << 13 ;
char ch[hg];
size_t poz ;
inline void cit ( size_t &x ) {
if ( ch[0] == '\0' ) fread ( ch, 1, hg, stdin ) ;
else for ( ; ch[poz] < '0' || ch[poz] > '9' ; verf ) ;
for ( x = 0 ; ch[poz] >= '0' && ch[poz] <= '9' ; x = x * 10 + ch[poz] - '0', verf ) ;
}
inline void solve ( size_t K ) {
size_t aux, auxdf ;
for ( size_t i = N; i ; --i ) {
for ( size_t j = 0, see = i + K > N; j < 4; ++j ) {
B[i][j] = (S[i] == adn[j]) + (see ? 0 : B[i + K][j]) ;
}
}
for ( size_t i = 1; i <= N; ++i ) {
for ( size_t j = i + 2 * K - 1; j <= N; j += K ) {
aux = 0;
for ( size_t k = 0; k < K; ++k ) {
auxdf = 0 ;
for ( size_t l = 0, see = j + k + 1 <= N; l < 4; ++l ) {
auxdf = max (auxdf, B[i + k][l] - (see ? B[j + k + 1][l] : 0)) ;
}
aux += (j - i + 1) / K - auxdf ;
}
A[i][j] = min (A[i][j], aux) ;
}
}
}
int main (void) {
freopen (FIN, "r", stdin) ;
freopen (FOU, "w", stdout) ;
scanf ( "%u %u %s", &N, &M, S + 1 ) ;
for ( size_t i = 1; i <= N; ++i ) {
for ( size_t j = i + 1; j <= N; ++j ) {
A[i][j] = j - i ;
}
}
for ( size_t i = 1, j = N >> 1; i <= j; ++i ) {
solve (i) ;
}
cit(N) ;
for ( size_t i = 1, x, y; i <= M; ++i ) {
cit (x), cit(y) ;
printf ( "%d\n", A[x][y] ) ;
}
}