fork download
  1. #include<iostream>
  2. #include<cmath>
  3. #include<algorithm>
  4. #include<iomanip>
  5. #include<climits>
  6. #include<numeric>
  7. #include<set>
  8. #include<sstream>
  9. #include<map>
  10. #include<vector>
  11. #include<string>
  12. #include<regex>
  13.  
  14. using namespace std;
  15.  
  16. const long long MOD = (long long)(1e9 + 7);
  17.  
  18. vector<string> split(const string& haystack, const string& needle) {
  19. vector<string> result;
  20. size_t start_pos = 0;
  21. size_t found_pos = haystack.find(needle, start_pos);
  22. while (found_pos != string::npos) {
  23. size_t count = found_pos - start_pos;
  24. string token = haystack.substr(start_pos, count);
  25. if (!token.empty()) {
  26. result.push_back(token);
  27. }
  28. start_pos = found_pos + needle.length();
  29. found_pos = haystack.find(needle, start_pos);
  30. }
  31.  
  32. string last_token = haystack.substr(start_pos);
  33. if (!last_token.empty()) {
  34. result.push_back(last_token);
  35. }
  36. return result;
  37. }
  38.  
  39. int main() {
  40. string s;cin >> s;
  41. int max_length = 0;
  42. map<int, int> prefix;
  43. int sum = 0;
  44. for (int i = 0;i < s.length();i++) {
  45. if (s[i] == '1') {
  46. sum++;
  47. }
  48. else {
  49. sum--;
  50. }
  51.  
  52. if (prefix.find(sum) != prefix.end()) {
  53. max_length = max(max_length, i - prefix[sum]);
  54. }
  55. else {
  56. prefix[sum] = i;
  57. }
  58.  
  59. if (sum == 0) {
  60. max_length = max(max_length, i + 1);
  61. }
  62. }
  63. cout << max_length;
  64. return 0;
  65. }
  66.  
  67.  
Success #stdin #stdout 0.01s 5324KB
stdin
Standard input is empty
stdout
Standard output is empty