Question

ANSWER IN C++ ONLY A string of characters including only alphabets (lowercase letters) is provided as...

ANSWER IN C++ ONLY

A string of characters including only alphabets (lowercase letters) is provided as an input.

  • The first task is to compute the frequency of each character appearing in the string. In the output, the characters have to be arranged in the same order as they appear in the input string.
  • Then characters have to be rearranged, such that all the characters having a specific frequency, say xx, come together.

Let the frequency of a character, lying in between the two characters having same frequency xx, be yy. The steps to be followed for getting the desired arrangement are as follows:

  • If y>xy>x, then shift the character at the end.
  • If y<xy<x, then shift the character at the beginning.

Input:

  • Line 1 contains the string of characters without spaces.
  • Line 2 contains an integer N.
  • Line 3 contains N integers separated by space, N1 N2 … NN. These are the specific frequency values whose corresponding characters should come together.

Output:

  • Line 1 has characters and their frequency as computed originally. Each output value is separated using a space.
  • Each of the following N lines contains the rearranged sequence of characters and their frequency for the given N specific frequencies. Each line in the output has output values separated using a space.

Constraints:

  • Number of characters in the string ranges in between 1 and 1000.

Sample Input:

nomatterhowbusyyoumaythinkyouareyoumustfindtimeforreadingorsurrenderyourself
toselfchosenignorance
3
3 1 6

Sample Output:

n 8 o 11 m 4 a 5 t 6 e 10 r 10 h 3 w 1 b 1 u 7 s 6 y 6 i 5 k 1 f 4 d 3 g 2 l 2 c 2
k 1 b 1 w 1 n 8 o 11 m 4 a 5 t 6 e 10 r 10 h 3 d 3 g 2 l 2 c 2 u 7 s 6 y 6 i 5 f 4
k 1 b 1 w 1 n 8 o 11 m 4 a 5 t 6 e 10 r 10 h 3 d 3 g 2 l 2 c 2 u 7 s 6 y 6 i 5 f 4
c 2 l 2 g 2 d 3 h 3 k 1 b 1 w 1 n 8 o 11 m 4 a 5 t 6 s 6 y 6 i 5 f 4 e 10 r 10 u 7

EXPLANATION:

First line has characters and their frequency as computed using the given input string. The characters are arranged in the same order as per their appearance in the original string. Second line is obtained after rearranging characters appearing thrice together. Similarly, Third line is generated after placing characters that are coming only once together. The Third line output is similar to the Second line output as targeted characters (having frequency one) automatically comes in continuation during the previous rearrangement corresponding to frequency three. Lastly, the Fourth line presents the final character and frequency sequence after putting characters with frequency 6 together.

PLS HELP

Homework Answers

Answer #1

First part

using namespace std;

  

#define SIZE 26

  

// function to print the character and its frequency

// in order of its occurrence

void printCharWithFreq(string str)

{

// size of the string 'str'

int n = str.size();

  

// 'freq[]' implemented as hash table

int freq[SIZE];

  

// initialize all elements of freq[] to 0

memset(freq, 0, sizeof(freq));

  

// accumulate freqeuncy of each character in 'str'

for (int i = 0; i < n; i++)

freq[str[i] - 'a']++;

  

// traverse 'str' from left to right

for (int i = 0; i < n; i++) {

  

// if frequency of character str[i] is not

// equal to 0

if (freq[str[i] - 'a'] != 0) {

  

// print the character along with its

// frequency

cout << str[i] << freq[str[i] - 'a'] << " ";

  

// update frequency of str[i] to 0 so  

// that the same character is not printed  

// again

freq[str[i] - 'a'] = 0;

}

}

}

  

// Driver program to test above

int main()

{

string str = "nomatterhowbusyyoumaythinkyouareyoumustfindtimeforreadingorsurrenderyourselftoselfchosenignorance";

printCharWithFreq(str);

return 0;

}

Trying to figure out others too if you like then pls upvote, meanwhile I am working on other subparts of your question

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
(C++) Write a program whose input is two characters and a string, and whose output indicates...
(C++) Write a program whose input is two characters and a string, and whose output indicates the number of times each character appears in the string. Ex: If the input is: n M Monday the output is: 1 1 Ex: If the input is: z y Today is Monday the output is: 0 2 Ex: If the input is: n y It's a sunny day the output is: 2 2 Case matters. Ex: If the input is: n N Nobody...
This is for C++ A string is a palindrome if reverse of the string is same...
This is for C++ A string is a palindrome if reverse of the string is same as the original string. For example, “abba” is palindrome, but “abbc” is not palindrome. Basically a string with a plane of symmetry in the middle of it is considered a palindrome. For example, the following strings are palindromic: "abbbbba", "abba", "abbcbba", "a", etc. For this problem, you have an input string consisting of both lowercase or uppercase characters. You are allowed to pick and...
Write a program that takes a string of characters (including spaces) as input, computes the frequency...
Write a program that takes a string of characters (including spaces) as input, computes the frequency of each character, sorts them by frequency, and outputs the Huffman code for each character.   When you are writing your program, you should first test it on a string of 7 characters, so you can check it. PLEASE NOTE: Your program must work for any text that has upper and lower case letters digits 0 - 9, commas, periods, and spaces. Please submit the...
pseudocode please!! Assignment6C: P0\/\/|\|3D. In the early 80s, hackers used to write in an obfuscated, but...
pseudocode please!! Assignment6C: P0\/\/|\|3D. In the early 80s, hackers used to write in an obfuscated, but mostly readable way called “leet” – short for “elite”. In essence, it was a simple character replacement algorithm, where a single “regular” character was replaced by one or more “leet” characters; numbers remained the same. Here’s one of the most readable versions: a 4 g 9 m /\\/\\ s $ y ‘/ b B h |-| n |\\| t 7 z Z c (...
MIPS ASSEMBLY Have the user input a string and then be able to make changes to...
MIPS ASSEMBLY Have the user input a string and then be able to make changes to the characters that are in the string until they are ready to stop. We will need to read in several pieces of data from the user, including a string and multiple characters. You can set a maximum size for the user string, however, the specific size of the string can change and you can’t ask them how long the string is (see the sample...
Implement a singly linked list having all unique elements with the following operations.I 0 x –...
Implement a singly linked list having all unique elements with the following operations.I 0 x – Inserts element x at the end. I 1 y x – If the element y exists, then insert element x after the element y, else insert element y before the existing element x. Assuming either the element x or the element y exists. I 2 z y x – Inserts element x in the middle of the elements z and y. The element z...
3. Write function, leastChar(inputString) that takes as input a string of one or more letters (and...
3. Write function, leastChar(inputString) that takes as input a string of one or more letters (and no other characters) and prints 1) the "least" character in the string, where one character is less than another if it occurs earlier in the alphabet (thus 'a' is less than 'C' and both are less than 'y') and 2) the index of the first occurrence of that character. When comparing letters in this problem, ignore case - i.e. 'e' and 'E' should be...
C++ only. Must use loops. Please do not use sequence of if statements. Given as input...
C++ only. Must use loops. Please do not use sequence of if statements. Given as input string and an int N, set result a string made of N repetitions of the last N characters of the string. You may assume that N is between 0 and the length of the string, inclusive.   • for input of "Hello", 3 → "llollollo" • for input of "Hello", 2 → "lolo" • for input of "Hello", 1 → "o"
Write a program of wordSearch puzzle that use the following text file as an input. The...
Write a program of wordSearch puzzle that use the following text file as an input. The output should be like this: PIXEL found (left) at (0,9). ( Use JAVA Array ) .Please do not use arrylist and the likes! Hints • The puzzle can be represented as a right-sized two-dimensional array of characters (char). • A String can be converted into a right-sized array of characters via the String method toCharArray. . A word can occur in any of 8...
This is C. Please write it C. 1) Prompt the user to enter a string of...
This is C. Please write it C. 1) Prompt the user to enter a string of their choosing. Store the text in a string. Output the string. (1 pt) Ex: Enter a sample text: we'll continue our quest in space. there will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. nothing ends here; our hopes and our journeys continue! You entered: we'll continue our quest in space. there will be...