-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbeads.java
More file actions
112 lines (102 loc) · 2.43 KB
/
Copy pathbeads.java
File metadata and controls
112 lines (102 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* Use the slash-star style comments or the system won't see your
identification information */
/*
ID: adimagg1
LANG: JAVA
TASK: beads
*/
import java.io.*;
import java.util.*;
class beads {
public static void main (String [] args) throws IOException {
// Use BufferedReader rather than RandomAccessFile; it's much faster
File f = new File("beads.in");
Scanner in = new Scanner(f);
// input file name goes above
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("beads.out")));
// Use StringTokenizer vs. readLine/split -- lots faster
int n = in.nextInt();
String s = in.next();
int max = 0;
int indexer = 0;
for(int i = 0; i<n;i++){
int test = count(s, n, i);
if(test>max){
max = test;
indexer = i;
}
}
out.print(max);
out.println();
out.close(); // close the output file
}
public static int count (String s, int n, int index){
int result = 1;
int top;
if(index == n-1){
top = 0;
}else{
top = index +1;
}
if(s.charAt(index) == 'b'){
for(int i = index-1; i< n+2; i--){
if(i == -1){
i = n-1;
}
if(s.charAt(i) == 'b' || s.charAt(i) == 'w'){
result++;
}else{
break;
}
if(result>n*2){
break;
}
}
}else{for(int i = index-1; i< n; i--){
if(i == -1){
i = n-1;
}
if(s.charAt(i) == 'r' || s.charAt(i) == 'w'){
result++;
}else{
break;
}
if(result>n*2){
break;
}
}
}
if(s.charAt(top) == 'b'){
for(int i = index+1; i< n+2; i++){
if(i == n){
i = 0;
}
if(s.charAt(i) == 'b' || s.charAt(i) == 'w'){
result++;
}else{
break;
}
if(result>n*2){
break;
}
}
}else{for(int i = index+1; i< n+2; i++){
if(i == n){
i = 0;
}
if(s.charAt(i) == 'r' || s.charAt(i) == 'w'){
result++;
}else{
break;
}
if(result>n*2){
break;
}
}
}
if(result>n){
return n;
}
return result;
}
}