void change_char_at_pos(const std::string & str, const s_vector & N) { RESULT_BY_CHANGE_CHAR.push_back(""); // If it is empty I can start so add empty string. std::string set[] = {"0","1","2","3","4","5","6","7","8","9", "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z", "a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"}; int idx = 0; s_vector::const_iterator pos = N.begin(); while(pos != N.end()) // During iterations, when we confront pos change it from 0 to z. { if ((int)str.length() - *pos > idx) //Maintain character positions that vector N doesn't mention. { for (str_vector::iterator it = RESULT_BY_CHANGE_CHAR.begin(); it != RESULT_BY_CHANGE_CHAR.end(); ++it) *it += str[idx]; idx++; } else //Change charactor positions that vector N mention. { str_vector tempV; for (str_vector::iterator it = RESULT_BY_CHANGE_CHAR.begin(); it != RESULT_BY_CHANGE_CHAR.end(); ++it) { for (int i=0;i<62;i++) tempV.push_back(*it + set[i]); } RESULT_BY_CHANGE_CHAR = tempV; //already modify all the elements in RESULT_BY_CHANGE_CHAR, and give them back to itself. tempV.clear(); ++pos; idx++; } } while (idx < (int)str.length()) // If while loop ends because we changed all, add left part of string. { for (str_vector::iterator it = RESULT_BY_CHANGE_CHAR.begin(); it != RESULT_BY_CHANGE_CHAR.end(); ++it) *it += str[idx]; idx++; } return; }
void account_table::recommend(const std::string & str, str_vector & result, const unsigned int & numbers) { int score = 1; while (1) { str_vector temp; int s_len = 0; int s_mod; // score - s_len unsigned int p_cnt = 0; while ( s_len <= score ) { s_mod = score - s_len; get_pos_set(s_mod); //calculate score(s_mod) permutation set // First step cut first and modify if (str.length() > p_cnt) { std::string temp_str = str.substr(0, (int)str.length() - p_cnt); //cut length if (s_mod == 0) temp.push_back(temp_str); // Add string, which is only cut else //Add string; there is modification { for (m_vector::iterator it = TABLE[s_mod-1].begin(); it != TABLE[s_mod-1].end(); ++it) { if (it->front() <= (int)temp_str.length()) //Need to check, whether the modification target position is within string. { change_char_at_pos(temp_str, *it); std::copy (RESULT_BY_CHANGE_CHAR.begin(),RESULT_BY_CHANGE_CHAR.end(),back_inserter(temp)); RESULT_BY_CHANGE_CHAR.clear(); } } } } // Second step modify first and add permutation str_vector pmt = permute(p_cnt); if (pmt.size() != 0) { if ((int)str.length() + p_cnt <= 100) // Length of ID should be less than 100 { if (s_mod == 0) // s_mod is 0 for (str_vector::iterator it = pmt.begin(); it != pmt.end(); ++it) temp.push_back(str + *it); else { for (m_vector::iterator it = TABLE[s_mod-1].begin(); it != TABLE[s_mod-1].end(); ++it) { if (it->front() <= (int)str.length()) { change_char_at_pos(str, *it); for (str_vector::iterator i_it = RESULT_BY_CHANGE_CHAR.begin(); i_it != RESULT_BY_CHANGE_CHAR.end(); ++i_it) for (std::vector<std::string>::iterator ii_it = pmt.begin(); ii_it != pmt.end(); ++ii_it) temp.push_back(*i_it + *ii_it); } RESULT_BY_CHANGE_CHAR.clear(); } } } } p_cnt++; s_len += p_cnt; } std::sort( temp.begin(), temp.end() ); str_vector::iterator it = temp.begin(); while( result.size() < numbers && it != temp.end() ) { iter_m i = this->account_list.find(*it); if (*it != str && i==this->account_list.end()) //not equal to original string, and it can't already exist { if (result.size() != 0 && *it != result.back()) result.push_back(*it); else if (result.size() == 0) result.push_back(*it); } ++it; } if (result.size() == numbers) break; score++; temp.clear(); } return; }