Guest User

USACO_BEADS_C++

a guest
Jan 20th, 2025
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.14 KB | Software | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. string coloredNecklace (string necklace) {
  5.     char currentColor = necklace[0];
  6.     int maxBeadCount = 0;
  7.     for (int i = 0; i < necklace.size(); i++) {
  8.         if (necklace[i] != currentColor && necklace[i] != 'w') {
  9.             currentColor = necklace[i];
  10.         }
  11.         if (necklace[i] == 'w') {
  12.             necklace[i] = currentColor;
  13.         }
  14.     }
  15.     return necklace;
  16. }
  17.  
  18. void maxBeadCount (string necklace, int &max) {
  19.     int d = 1;
  20.     for (int i = 0; i < necklace.size() - 1; i++) {
  21.         while (i < necklace.size() && (necklace[i] == necklace[i + 1] || necklace[i] == 'w')) {
  22.             d++;
  23.             i++;
  24.         }
  25.         if (d > max) {
  26.             max = d;
  27.         }
  28.         d = 1;
  29.     }
  30. }
  31.  
  32. int main () {
  33.     freopen ("beads.in", "r", stdin);
  34.     freopen ("beads.out", "w", stdout);
  35.     int max = 0, n;
  36.     string s;
  37.     cin >> n >> s;
  38.     s += s;
  39.     maxBeadCount(coloredNecklace(s), max);
  40.     reverse(s.begin(), s.end());
  41.     maxBeadCount(coloredNecklace(s), max);
  42.     if (max > n) {
  43.         max /= 2;
  44.     }
  45.     cout << max << endl;
  46.     return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment