//set all points to true that would be in circle of radius r from a point that was originally true void expand_object(array_2d<unsigned char >& obj, double r) { const unsigned int width(obj.width); const unsigned int height(obj.height); const unsigned int size(obj.size); array_2d<edist > dists(width, height); edist max_dist = edist(-(width + height), -(width + height)); edist zero = edist(0,0); for(unsigned int i = 0; i < size; i++) { dists[i] = (obj[i] ? zero : max_dist); } expand_dist_map(dists); double r_squared = r * r; for(unsigned int i = 0; i < size; i++) { obj[i] = dists[i] <= r_squared; } }
static int cdist(int r, int c, int alt, struct xyz target, const struct plane *p, int srow, int scol) { struct xy sxy = { .row = srow, .col = scol }; if (p->target_airport && in_airport_excl(sxy, 1, p->target_num)) { // Use an airport's secondary targets. const struct airport *ap = get_airport(p->target_num); int dist1 = edist(r, c, alt, ap->strow1, ap->stcol1, 2); int dist2 = edist(r, c, alt, ap->strow2, ap->stcol2, 2); return (dist1 < dist2) ? dist1 : dist2; } return edist(r, c, alt, target.row, target.col, target.alt); }
int main() { int c; scanf("%d", &c); while(c--) { scanf(" %s %s", w1, w2); printf("%d\n", edist(strlen(w1), strlen(w2))); } return 0; }
void Center_set::updateDistance(int e, Sample_point * sample) { if(usingatoms) { Array1 <doublevar> R(5); sample->updateEIDist(); for(int i=0; i< ncenters; i++ ) { sample->getEIDist(e, i, R); for(int d=0; d< 5; d++) { edist(e,i,d)=R(d); } } } else if(usingsampcenters) { Array1 <doublevar> R(5); sample->updateECDist(); for(int i=0; i< ncenters; i++ ) { sample->getECDist(e, i, R); for(int d=0; d< 5; d++) { edist(e,i,d)=R(d); } } } else { Array1 <doublevar> r(3); sample->getElectronPos(e, r); for(int i=0; i< ncenters; i++) { edist(e,i,1)=0; for(int d=0; d< 3; d++) { edist(e,i,d+2)=r(d)-position(i,d); //cout << "positions " << position(i,d) << " dist " << edist(e,i,d+2) << endl; edist(e,i,1)+=edist(e,i,d+2)*edist(e,i,d+2); } } for(int i=0; i< ncenters; i++) { edist(e,i,0)=sqrt(edist(e,i,1)); } } }
void Neighborhood::calcEdistMatrix(double* distMatrix){ for(int i = 0; i < ninst; i++) for(int j = 0; j < ninst; j++){ if(i==j) distMatrix[i*ninst+j] = .0; else if(i>j) distMatrix[i*ninst+j] = distMatrix[j*ninst+i]; else{ distMatrix[i*ninst+j] = edist(i, j); } } }
int vq(double *x, double *cb, const int l, const int cbsize) { int i, index = 0; double min = 1e23, dist; for (i = 0; i < cbsize; i++) { dist = edist(x, cb, l); if (dist < min) { index = i; min = dist; } cb += l; } return (index); }
int main(int argc, char *argv[]) { string sd(D), sc(C); int la = strlen(A), lb = strlen(B), lc = strlen(C), ld = strlen(D); int ed = edist(A,la,B,lb); printf("Levenshtein distance (%s, %s): %d (should be 3)\n", A, B, ed); int ls = lcs(A,la,B,lb); printf("Longest common subsequence (%s, %s): %d (should be 4)\n", A, B, ls); strcpy(pattern, C); preffix_suffix(pattern, array); strcpy(text, D); printf("%s\n",pattern); show(array, strlen(pattern)); printf("dopasowanie na m = %d\n",kmp_search(text, pattern)); printf("rabi_karp, dopasowanie na m = %d\n", rabin_karp(sc, sd)); return 0; }
void lbg(double *x, const int l, const int tnum, double *icb, int icbsize, double *cb, const int ecbsize, const int iter, const int mintnum, const int seed, const int centup, const double delta, const double end) { int i, j, k, it, maxindex, tnum1, tnum2; static int *cntcb, *tindex, size, sizex, sizecb; unsigned long next = SEED; double d0, d1, dl, err, tmp, rand; static double *cb1 = NULL; double *p, *q, *r; if (cb1 == NULL) { cb1 = dgetmem(ecbsize * l); tindex = (int *) dgetmem(tnum); cntcb = (int *) dgetmem(ecbsize); size = l; sizex = tnum; sizecb = ecbsize; } if (l > size) { free(cb1); cb1 = dgetmem(ecbsize * l); size = l; } if (tnum > sizex) { free(tindex); tindex = (int *) dgetmem(tnum); sizex = tnum; } if (sizecb > ecbsize) { free(cb1); free(cntcb); cb1 = dgetmem(ecbsize * l); cntcb = (int *) dgetmem(ecbsize); } movem(icb, cb, sizeof(*icb), icbsize * l); if (seed != 1) next = srnd((unsigned int) seed); for (; icbsize * 2 <= ecbsize;) { q = cb; r = cb + icbsize * l; for (i = 0; i < icbsize; i++) { for (j = 0; j < l; j++) { dl = delta * nrandom(&next); *r = *q - dl; r++; *q = *q + dl; q++; } } icbsize *= 2; d0 = MAXVALUE; for (it = 1; it <= iter; it++) { fillz((double *) cntcb, sizeof(*cntcb), icbsize); d1 = 0.0; p = x; for (i = 0; i < tnum; i++, p += l) { tindex[i] = vq(p, cb, l, icbsize); cntcb[tindex[i]]++; q = cb + tindex[i] * l; d1 += edist(p, q, l); } d1 /= tnum; err = abs((d0 - d1) / d1); if (err < end) break; d0 = d1; fillz(cb1, sizeof(*cb), icbsize * l); p = x; for (i = 0; i < tnum; i++) { q = cb1 + tindex[i] * l; for (j = 0; j < l; j++) *q++ += *p++; } k = maxindex = 0; for (i = 0; i < icbsize; i++) if (cntcb[i] > k) { k = cntcb[i]; maxindex = i; } q = cb; r = cb1; for (i = 0; i < icbsize; i++, r += l, q += l) if (cntcb[i] >= mintnum) for (j = 0; j < l; j++) q[j] = r[j] / (double) cntcb[i]; else { if (centup == 1) { p = cb + maxindex * l; for (j = 0; j < l; j++) { rand = nrandom(&next); q[j] = p[j] + delta * rand; p[j] = p[j] - delta * rand; } } else if (centup == 2) { if (i < icbsize / 2) { p = q + icbsize / 2 * l; tnum1 = cntcb[i]; tnum2 = cntcb[i + icbsize / 2]; for (j = 0; j < l; j++) { tmp = (tnum2 * q[j] + tnum1 * p[j]) / (tnum1 + tnum2); rand = nrandom(&next); q[j] = tmp + delta * rand; p[j] = tmp - delta * rand; } } else { p = q - icbsize / 2 * l; tnum1 = cntcb[i]; tnum2 = cntcb[i - icbsize / 2]; for (j = 0; j < l; j++) { tmp = (tnum2 * q[j] + tnum1 * p[j]) / (tnum1 + tnum2); rand = nrandom(&next); q[j] = tmp + delta * rand; p[j] = tmp - delta * rand; } } } } } if (icbsize == ecbsize) break; } return; }
edist right() { return edist(dx + 1, dy);}
edist left() { return edist(dx - 1, dy);}
edist below() { return edist(dx, dy + 1);}
edist above() { return edist(dx, dy - 1);} //get edist that would be from the cell above this one