Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- struct myMapNode { // create our own structure map like type
- char letter;
- int count;
- std::string bitRepr; // binary representation of number in array
- };
- void addLetter(std::vector<myMapNode> &vec, char letterToAdd) { // create map like array and append elem if arr has no
- // or update count to +1 if element is exists
- int len = vec.size();
- for (int i = 0; i < len; i++) {
- if (letterToAdd == vec[i].letter) {
- vec[i].count++;
- return;
- }
- }
- vec.push_back(myMapNode{letterToAdd, 1});
- }
- std::string convertToBin(int numCount, int n) { // bitRepr to bitRepr convert int 0 or 1 to char and add to our result string
- std::string output;
- for (int i = 0; i < numCount; i++) {
- output += ((char) n % 2) + 48; // %2 is 0 or 1 and is last num in bitRepr representation
- // +48 because 48 is position 0 in ascii
- n >>= 1; // bitRepr shift to new int
- }
- return output;
- }
- int bitsCount(int n) { // function for find max count of bits in out last index in array
- int count = 0;
- while (n) {
- count++;
- n >>= 1;
- }
- return count;
- }
- int main() {
- using namespace std;
- string str;
- getline(cin, str);
- vector<myMapNode> vec;
- for (auto letter: str) {
- addLetter(vec, letter);
- }
- int bitCount = bitsCount(vec.size() - 1); // find max count of bits in out last index in array
- for (int i = 0; i < vec.size(); ++i) {
- vec[i].bitRepr = convertToBin(bitCount, i);
- cout << vec[i].letter << " - " << vec[i].count << " - " << vec[i].bitRepr << '\n';
- }
- for (auto letter: str) { // print our string by finding in array equal letters
- for (int j = 0; j < vec.size(); j++) {
- if (vec[j].letter == letter) {
- cout << vec[j].bitRepr;
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement