Untitled

mail@pastecode.io avatar
unknown
c_cpp
a month ago
1.9 kB
1
Indexable
Never
#include <iostream>
#include <string>
#include <vector>

struct map
{
	std::vector<char> key;
	std::vector<int> value;
};

void print(map letters) {
	for (int i = 0; i < letters.key.size(); i++) {
		std::cout << letters.key[i] << " : " << letters.value[i] << "\n";
	}
}

void sortMapToMax(map &letters) {
	size_t size = letters.key.size();

	for (size_t i = 0; i < size; ++i) {
		for (size_t j = 0; j < size - 1; ++j) {
			if (letters.value[j] > letters.value[j + 1]) {
				int bufferValue = letters.value[j];
				char bufferKey = letters.key[j];

				letters.value[j] = letters.value[j + 1];
				letters.value[j + 1] = bufferValue;

				letters.key[j] = letters.key[j + 1];
				letters.key[j + 1] = bufferKey;
			}
		}
	}

}

void sortMapToMin(map& letters) {
	size_t size = letters.key.size();

	for (size_t i = 0; i < size; ++i) {
		for (size_t j = 0; j < size - 1; ++j) {
			if (letters.value[j] < letters.value[j + 1]) {
				int bufferValue = letters.value[j];
				char bufferKey = letters.key[j];

				letters.value[j] = letters.value[j + 1];
				letters.value[j + 1] = bufferValue;

				letters.key[j] = letters.key[j + 1];
				letters.key[j + 1] = bufferKey;
			}
		}
	}

}
void count(std::string word, map &letters) {
	for (char i : word) {
		if (std::find(letters.key.begin(), letters.key.end(), i) == letters.key.end()) {
			letters.key.push_back(i);
			letters.value.push_back(std::count(word.begin(), word.end(), i));
		}
	}
}

int main() {

	std::string word;
	std::cin >> word;

	map letters;
	count(word, letters);
	sortMapToMax(letters);

	std::cout << "vector after sort from min to max" << "\n";
	print(letters);
	std::cout << "--------------------------------------" << '\n';
	std::cout << "vector after sort from max to min" << "\n";
	sortMapToMin(letters);
	print(letters);
};
Leave a Comment