Untitled
unknown
plain_text
21 days ago
1.6 kB
2
Indexable
Never
#include <iostream> #include <string> #include <algorithm> // Function to reverse a string std::string reverse(const std::string& s) { std::string reversed(s); std::reverse(reversed.begin(), reversed.end()); return reversed; } // Function to find the longest alternating substring length int findLongestAlternatingSubstring(const std::string& s) { int maxLength = 0; int currentLength = 1; for (size_t i = 1; i < s.length(); ++i) { if (s[i] != s[i - 1]) { ++currentLength; } else { maxLength = std::max(maxLength, currentLength); currentLength = 1; } } maxLength = std::max(maxLength, currentLength); return maxLength; } // Function to perform the operation and check possible strings int performOperation(const std::string& s) { int n = s.length(); int maxLength = 0; for (int i = 1; i < n; ++i) { std::string part1 = s.substr(0, i); std::string part2 = s.substr(i); std::string reversedPart1 = reverse(part1); std::string reversedPart2 = reverse(part2); std::string combined1 = reversedPart1 + reversedPart2; std::string combined2 = part1 + part2; // Original string to consider if needed maxLength = std::max(maxLength, findLongestAlternatingSubstring(combined1)); maxLength = std::max(maxLength, findLongestAlternatingSubstring(combined2)); } return maxLength; } int main() { std::string s; std::cin >> s; std::cout << performOperation(s) << std::endl; return 0; }
Leave a Comment