mamajonok
BAN USERstd::string guess_word(const std::string& w, const std::vector<std::string>& dict) {
std::map<int, std::string> words;
for (auto&& word : dict) {
if (word.length() > w.length() + 1 || w.length() > word.length() + 1)
continue;
std::vector<std::vector<char>> matrix(word.size(), std::vector<char>(w.size(), 0));
int number = 0;
for (int i = 0; i < matrix.size(); i++) {
for (int j = 0; j < matrix[i].size(); j++) {
if (w[j] == word[i]) {
if (j && i) {
matrix[i][j] = matrix[i - 1][j - 1] + 1;
}
else {
matrix[i][j] = 1;
}
if (matrix[i][j] > number) {
number = matrix[i][j];
}
}
}
}
if (std::abs(number - (int)w.length()) < 2)
words[number] = word;
}
if (words.size())
return words.rbegin()->second;
return std::string();
}
#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
std::vector<std::string> collect(std::unordered_map<std::string, std::list<std::string>> & data, const std::string & part)
{
std::vector<std::string> results;
for (int i = 1; i <= part.length(); i++) {
std::string s = part.substr(0, i);
auto it = data.find(s);
if (it != data.end()) {
if (s.length() == part.length()) {
for (auto && it3 : it->second) {
results.push_back(it3);
}
}
else {
std::string h = part.substr(i, part.length() - i);
std::vector<std::string> sub_results = collect(data, h);
for (auto && it3 : it->second) {
for (auto && it2 : sub_results) {
results.push_back(it3 + it2);
}
}
}
}
}
return results;
}
int main()
{
std::unordered_map<std::string, std::list<std::string>> data;
data["1"].push_back("A");
data["1"].push_back("B");
data["1"].push_back("C");
data["2"].push_back("D");
data["2"].push_back("E");
data["12"].push_back("X");
data["3"].push_back("P");
data["3"].push_back("Q");
std::vector<std::string> results = collect(data, "123");
for (auto && it2 : results) {
std::cout << it2 << " ";
}
return 0;
}
int shuffle(vector<int>& arr) {
int last_value = arr[0];
int unique_index = 0;
int spot_index = -1;
for (int i = 1; i < arr.size(); i++) {
if (arr[unique_index] == arr[i] && (spot_index < 0 || (spot_index > 0 && arr[unique_index] != arr[spot_index]))) {
spot_index = i;
}
else if (arr[unique_index] != arr[i]) {
if (i - unique_index > 1) {
arr[spot_index] = arr[i];
unique_index = spot_index;
spot_index++;
}
else {
unique_index++;
}
}
}
return unique_index;
}
#include <vector>
#include <string>
#include <iostream>
using namespace std;
void printRanges(std::vector<int> arr) {
if (arr[arr.size() - 1] != 100) {
arr.push_back(100);
}
for (int i = 1; i < arr.size(); i++) {
int lowerValue = arr[i - 1];
int higherValue = arr[i];
int delta = higherValue - lowerValue;
if (delta == 2) {
printf("%d; ", lowerValue + 1);
}
else if (delta > 2) {
printf("%d - %d; ", lowerValue + 1, higherValue - 1);
}
}
}
// Driver program to test above function
int main()
{
std::vector<int> arr = { 1,2,3,4,6,8,12,45,65,94 };
printRanges(arr);
return 0;
}
- mamajonok December 22, 2021