I've mentioned some examples to explain the problem below,I've written a code to solve the first two exercises ,is there more optimal solution, and could you please help me with the the third exercises
Heads or tails If an unbiased coin is tossed, then the value of the coin (Tails) has the same probability of showing after falling as the other side that contains the coat of arms (Heads). Because of this decision by "tossing a coin" is used frequently, for example this method is used to decide which team starts a football match. Your task is the simulation of the tossing of a coin and the analysis of sequences acquired by tossing a coin. In the exercises T stands for tails and H stands for heads. For example a sequence of 5 tosses can be: TTHTH. Create a program named Heads_Or_Tails to solve the following exercises. The input for the program is a so-called trial, i.e. the outcome of a sequence of tosses. This input must be read from the standard input. The first line of the input contains an integer, N , indicating the number of tosses.
The second line of the input line contains exactly N characters, each character is either H or T. For example, the input can be as follows.
8
HHHTHHTT
The output of the program should be written to the standard output. The output contains 3 lines: the ith line in the output is the solution to exercise i.
Exercise 1
How many times did it occur in the trial that exactly two heads were tossed after each other? For example, in the sequence THHHHTTHHTHHHTHH it occurred twice that exactly two heads were thrown after each other.
Exercise 2
Find the length of the longest subsequence that contained only heads, and the longest subsequence that contained only tails. Print both lengths as integer numbers separated by a space. (Any of the two numbers can be zero, but not both.)
Exercise 3
We shall call a sequence of heads in the trial a heads series, if both of the following two conditions hold:
the first item in the series is the first item in the trial, or the item preceding the first item of the series is tails;
the last item in the series is the last item in the trial, or the item following the last item of the series is tails.
Find out which is the most frequent length for heads series. If there are more than one "most frequent" heads series lengths, print the longest. If there are no heads in the trial, print zero.
Examples
We provide three simple examples to clarify the exercises. Test your solution with other, larger examples as well!
Example 1
Input:
10
HHTTHHTHHH
Output:
2
3 2
2
Example 2
Input:
14
HHTTHHTHHHTHHH
Output:
2
3 2
3
Example 3
Input:
1
H
Output:
0
1 0
1
What I have tried:
int main()
{
char TossesSequence[50];
int N;
int TwoHeadsN =0;
int LongestH = 0;
int LongestT = 0;
int HeadsC = 0;
int TailsC = 0;
cout << "Please enter Number of tosses: ";
cin >> N;
cout << "Please enter outcome of sequence of the tosses: ";
cin >> TossesSequence;
for (int i = 0; i < N; i++)
{
if (TossesSequence[i] == 'H')
{
HeadsC++;
if (HeadsC > LongestH)
LongestH = HeadsC;
TailsC = 0;
}
else if (TossesSequence[i] == 'T')
{
TailsC++;
if (TailsC > LongestT)
LongestT = TailsC;
if (HeadsC == 2)
TwoHeadsN++;
HeadsC = 0;
}
else
{
cout << "wrong input" << endl;
return -1;
}
}
if (HeadsC == 2)
TwoHeadsN++;
cout << TwoHeadsN << "\n";
cout << LongestH << "\t" << LongestT << endl;
return 0;
}