Question

In: Computer Science

Consider the set of strings A = {c,cc,ccc}. What is the shortest string the set of...

Consider the set of strings A = {c,cc,ccc}.
What is the shortest string the set of strings A5.

Solutions

Expert Solution

Here, we have A = {c,cc,ccc}
We want to find the shortest string in the set of A^5.

  • There are many ways to do it. Let's understand it using a program.
  • I have used C++ for the program.
  • You can use any other language too according to your requirement as the basic logic will be the same.

Algorithm/Steps to follow:

  1. First of all, find the string with minimum length from A.
  2. For finding the string with minimum length, we take a variable(minimumLength), initialize with the minimumLength with INT_MAX and then compare.
  3. If A[i] is less than the minimumLength then we update the minimumLength.
  4. Now by traversing the entire array, we will find the string having minimum length.
  5. After this concatenate the string power-times(Here, power = 5).
  6. The final string after the concatenation will be the required string.

Please refer to the comments of the program for more clarity.

#include<bits/stdc++.h>
using namespace std;


int main()
{
    int len = 3;  // Length of the given string array
    string A[len] = {"c", "cc", "ccc"};  // The string array
    int power = 5;   // Given power
    int minimumLength = INT_MAX;  // Initialized the minimum length
    string minimumLengthString = A[0];  // Initialized the minimum length string

    // Finding the shortest string and its length for a A
    for(int i=0;i <len; i++)
    {
        if(A[i].length()<minimumLength)
        {
            minimumLength = A[i].length();
            minimumLengthString = A[i];
        }
    }

    // Finding the shortest length string for A^power
    string shortestStringWithTheGivenPower = "";
    for(int i=1; i<=power; i++)
    {
        shortestStringWithTheGivenPower = shortestStringWithTheGivenPower + minimumLengthString; // Concatenation of string
    }

    // Finally printing the values

    // Printing the shortest string for A^5
    cout << "The shortest string the set of strings A^5: " << shortestStringWithTheGivenPower << endl;

    // Printing the length of the above string
    cout << "Length of final string: " << minimumLength*power << endl;

    return 0;
}

Code-run/Output:

Please let me know in the comments in case of any confusion. Also, please upvote if you like.


Related Solutions

In C: Find a string within a string Given two strings S1 & S2, search for...
In C: Find a string within a string Given two strings S1 & S2, search for an occurrence of the second string within a first string. Note: Do not use system library for the implementation. Input: Code Zinger University Zinger where, First line represents string S1. Second line represents string S2. Output: 5 Here 'Zinger' word starts at 5th index within 'Code Zinger University’. Assume that, The length of strings S1 & S2 are within the range [1 to 10000]....
Question 3: C-Strings and pointers (10 pts) [5] The following function appends C-string str2, to C-string...
Question 3: C-Strings and pointers (10 pts) [5] The following function appends C-string str2, to C-string str1. Complete the function using array access, i.e. str1[i], but no pointer access. Note: For both part a) and part b) you may not use any library function calls (e.g. you cannot use strlen, strcat, etc.) // Append strt2 to str1 void my_strcat(char str1[], char str2[]) { //YOUR CODE HERE   } // example of using my_strcat() #include <stdio.h> int main(void) { char my_str1[50] =...
Given the nested collection that maps each term to a set of strings   Return a string...
Given the nested collection that maps each term to a set of strings   Return a string of terms that are repeated in all the nested sets Given : {apple=[apple BALL carrot, ball !carrot! ,!Dog*&]} {apple=[apple BALL carrot, ball !carrot! ,!Dog*&], dog=[ball !carrot! ,!Dog*&]} Return: [ball !carrot! ,!Dog*&] Public static String common(Map<String, Set<Sting>> map) { }
Suppose that you pick a bit string from the set of all bit strings of length...
Suppose that you pick a bit string from the set of all bit strings of length ten. Find the probability that the bit string has exactly two 1s; the bit string begins and ends with 0; the bit string has the sum of its digits equal to seven; the bit string has more 0s than 1s; the bit string has exactly two 1s, given that the string begins with a 1.
Suppose that you pick a bit string from the set of all bit strings of length...
Suppose that you pick a bit string from the set of all bit strings of length ten. Find the probability that the bit string has exactly two 1s; the bit string begins and ends with 0; the bit string has the sum of its digits equal to seven; the bit string has more 0s than 1s; the bit string has exactly two 1s, given that the string begins with a 1.
3. Write a Java program that generates a set of random strings from a given string...
3. Write a Java program that generates a set of random strings from a given string of same length where no character is ever repeated and characters belong to the original string. Examples Input: “Hello World” Output: “World oHlel”
use c++ (1) Prompt the user for a string that contains two strings separated by a...
use c++ (1) Prompt the user for a string that contains two strings separated by a comma. (1 pt) Examples of strings that can be accepted: Jill, Allen Jill , Allen Jill,Allen Ex: Enter input string: Jill, Allen (2) Print an error message if the input string does not contain a comma. Continue to prompt until a valid string is entered. Note: If the input contains a comma, then assume that the input also contains two strings. (2 pts) Ex:...
Code in c# Write a recursive method called isReverse(String s1, String s2) that accepts two strings...
Code in c# Write a recursive method called isReverse(String s1, String s2) that accepts two strings as parameters and returns true if the two strings contain the same sequence of characters as each other but in the opposite order and false otherwise. • The recursive function should ignore capitalization. (For example, the call of isReverse("hello", "eLLoH") would return true.) • The empty string, as well as any one letter string, should be its own reverse. Write a driver program that...
Before C++ and classes, strings were stored in simple arrays of characters. The term C-string refers...
Before C++ and classes, strings were stored in simple arrays of characters. The term C-string refers to the classic implementation of strings in the C programming language. A C-string is a sequence of characters terminated by the null character, '\0'. Since C is part of C++, C-string implementations are valid in C++ as well as C. Recall that an array of characters is the underlying data structure for storing C-strings. For example, this definition creates such an array. char myString[100];...
DEVELOP A C++ PROGRAM IN PROCEDURAL CODE that will recursively alphabetize a set of strings in...
DEVELOP A C++ PROGRAM IN PROCEDURAL CODE that will recursively alphabetize a set of strings in a user-specified file using the tree sort algorithm explained in the lectures while maintaining a balanced binary tree at all times. The following will test your program with an input file containing: Max Hank Jet Frisky Chata Richard Nan Sam Thomas Karen Gerri Ingrid Alan Dana When done print out the contents of the tree with inorder traversal in a tabular format similar to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT