In: Computer Science
Consider the set of strings A = {c,cc,ccc}.
What is the shortest string the set of strings A5.
Here, we have A = {c,cc,ccc}
We want to find the shortest string in the set of A^5.
Algorithm/Steps to follow:
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.