Розбір
Let's consider the input string — the resulting string after Huseyn has performed all operations. Initialize two pointers: at the start of the string and at the end.
If , then the characters at the ends of the string are different: either on the left and on the right, or on the left and on the right. This means the previous string could have been extended by applying one of the specified operations. In this case, move both pointers and one position towards each other and check again whether the substring could have been obtained through Huseyn's operations.
As soon as the current substring satisfies , it can no longer be produced by the given operations. Print its length — this is the original string that Huseyn had.
Example
Let's perform Huseyn's operations in reverse order.
The string was originally the one Huseyn had.
Algorithm realization
Read the input string and calculate its length, storing it in the variable .
cin >> s; res = s.size();
Initialize two pointers: at the start and at the end of the string.
i = 0; j = s.size() - 1;
If , then the characters at the ends of the string are different. Huseyn could have obtained the substring from the substring .
while ((i < j) && (s[i] != s[j])) {
Move both pointers and one position towards each other and decrease the current size of the substring .
i++; j--; res -= 2; }
Print the answer.
cout << res << endl;