USACO Section 1.1: Broken Necklace

 1 /*

 2 ID: leetcod3

 3 PROG: beads

 4 LANG: C++

 5 */

 6 #include <iostream>

 7 #include <fstream>

 8 #include <string>

 9 #include <map>

10 #include <vector>

11 #include <set>

12 #include <algorithm>

13 #include <stdio.h>

14 #include <queue>

15 #include <cstring>

16 #include <cmath>

17 #include <list>

18 #include <cstdio>

19 #include <cstdlib>

20 #include <limits>

21 #include <stack>

22 

23 using namespace std;

24 

25 ofstream fout ("beads.out");

26 ifstream fin ("beads.in");

27 

28 int main() {

29     int N;

30     fin >> N;

31     string s;

32     fin >> s;

33     s = s + s;

34     int lastb = 0;

35     int lastr = 0;

36     char cur = '#';

37     int ans = 0;

38     //cout << s << endl;

39     for (int i = 0; i < s.size(); i++) {

40         if (s[i] == 'r') {

41             if (cur == 'b') {

42                 int pre = lastr - 1;

43                 while (s[pre] == 'w') pre--;

44                 pre++;

45                 if (ans < i - pre) {

46                     ans = max(ans, i - pre);

47                     //cout << "ans : " << ans << endl;

48                 }

49                 lastr = i;

50                 //cout << "lastr: " << i << endl;

51             }

52             cur = 'r';

53         }

54         else if (s[i] == 'b') {

55             if (cur == 'r') {

56                 int pre = lastb - 1;

57                 while (s[pre] == 'w') pre--;

58                 pre++;

59                 if (ans < i - pre) {

60                     ans = max(ans, i - pre);

61                     //cout << "ans : " << ans << endl;

62                 }

63                 lastb = i;

64                 //cout << "lastb: " << i << endl;

65             }

66             cur = 'b';

67         }

68     }

69     if (ans == 0 || ans > s.size() / 2) ans = s.size() / 2;

70     fout << ans << endl;

71     return 0;

72 }

 

你可能感兴趣的:(USACO)