size_t smallest_abs_nonzero(size_t n, const num *x) { size_t i, j = (size_t)(-1); for (i = 0; i != n; ++i) { if (x[i] && (j == (size_t)(-1) || abs_num(x[i]) < abs_num(x[j]))) { j = i; } } return j; }
void remove_common_factors(size_t n, num *x) { const size_t imin = smallest_abs_nonzero(n, x); size_t i; num m; if (imin == (size_t)(-1)) { return; } /* not the most efficient algorithm */ for (m = 2; m <= abs_num(x[imin]); ++m) { while (all_divisible_by(n, x, m)) { size_t i; for (i = 0; i != n; ++i) { x[i] /= m; } } } m = 0; for (i = n - 1; i < n; --i) { if (x[i] && !m) { m = x[i] > 0 ? 1 : -1; } x[i] *= m; } }
int BankUMap::computeScoring(const string& str1, const string& str2) { int score = 0, min_len = min_num(str1.length(), str2.length()); for (int i = 0; i < min_len; i++) { if(str1[i] != str2[i]) score += (min_len - i); } int delta = abs_num((int)(str1.length() - str2.length())); score += delta * (delta + 1) / 2; return score; }
void BankRBTree::runRecommend(string id, string oid, int len, RecommendId& rid, int degree_c, int degree_a) { if (degree_c == 0 && degree_a == 0) { string now_id = id; DataNode la = DataNode(&now_id, NULL); DataNode* res = (DataNode*)rb_find(rb_tree, &la); if (res == NULL) { // it means that the id isn't exist in the bank rid.push_back(id); return; } } string tmpid; int delta = abs_num((int)oid.length() - (int)id.length()); if (degree_a > delta) { if (id.length() >= oid.length() && id.length() < MAX_STRING_SIZE) { for (char c = '0'; ; c = next_char(c)) { runRecommend(id + c, oid, id.length() + 1, rid, degree_c, degree_a - delta - 1); if (c == 'z') break; } } if (id.length() <= oid.length() && id.length() > len && id.length() > 1) { tmpid = id; tmpid.pop_back(); runRecommend(tmpid, oid, len, rid, degree_c, degree_a - delta - 1); } } if (degree_c > 0) { for (int i = max_num(min_num(id.length(), oid.length()) - degree_c, len); i < min_num(oid.length(), id.length()); i++) { if (degree_c >= min_num(oid.length(), id.length()) - i) { for (char c = '0'; c <= 'z'; c = next_char(c)) { if (c != oid[i]) { tmpid = id; tmpid[i] = c; runRecommend(tmpid, oid, i + 1, rid, degree_c - (min_num(oid.length(), id.length()) - i), degree_a); } if (c == 'z') break; } } } } }
void BankUMap::runRecommend(string id, string oid, int len, RecommendId& rid, int degree_c, int degree_a) { if (degree_c == 0 && degree_a == 0) { UMap::iterator it = umap.find(id); if (it == umap.end()) { // it means that the id isn't exist in the bank rid.push_back(id); return; } } string tmpid; int delta = abs_num((int)oid.length() - (int)id.length()); if (degree_a > delta) { if (id.length() >= oid.length() && id.length() < MAX_STRING_SIZE) { for (char c = '0'; ; c = next_char(c)) { runRecommend(id + c, oid, id.length() + 1, rid, degree_c, degree_a - delta - 1); if (c == 'z') break; } } if (id.length() <= oid.length() && id.length() > len && id.length() > 1) { tmpid = id; tmpid.pop_back(); runRecommend(tmpid, oid, len, rid, degree_c, degree_a - delta - 1); } } if (degree_c > 0) { for (int i = max_num(min_num(id.length(), oid.length()) - degree_c, len); i < min_num(oid.length(), id.length()); i++) { if (degree_c >= min_num(oid.length(), id.length()) - i) { for (char c = '0'; c <= 'z'; c = next_char(c)) { if (c != oid[i]) { tmpid = id; tmpid[i] = c; runRecommend(tmpid, oid, i + 1, rid, degree_c - (min_num(oid.length(), id.length()) - i), degree_a); } if (c == 'z') break; } } } } }