File tree Expand file tree Collapse file tree 1 file changed +66
-0
lines changed
Expand file tree Collapse file tree 1 file changed +66
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+
3+ import java.util.* ;
4+ import java.io.* ;
5+
6+ class Main {
7+
8+ // IO field
9+ static BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
10+ static BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (System . out));
11+ static StringTokenizer st;
12+
13+ static void nextLine () throws Exception {st = new StringTokenizer (br .readLine ());}
14+ static int nextInt() {return Integer . parseInt(st. nextToken());}
15+ static long nextLong() {return Long . parseLong(st. nextToken());}
16+ static void bwEnd() throws Exception {bw. flush();bw. close();}
17+
18+ // Additional field
19+ static String str;
20+ static int [] left, right, R_cnt ;
21+ static int N ;
22+
23+ public static void main(String [] args) throws Exception {
24+
25+ ready();
26+ solve();
27+
28+ bwEnd();
29+
30+ }
31+
32+ static void ready() throws Exception {
33+
34+ str = new String (" 0" ); // padding for prefix sum
35+ str += br. readLine();
36+ str += " 0" ;
37+ N = str. length()- 2 ;
38+
39+ left = new int [N + 2 ];
40+ right = new int [N + 2 ];
41+ R_cnt = new int [N + 2 ];
42+
43+ }
44+
45+ static void solve() throws Exception {
46+
47+ for (int i= 1 ;i<= N ;i++ ) left[i] += left[i- 1 ] + (str. charAt(i) == ' K' ? 1 : 0 );
48+ for (int i= N ;i>= 1 ;i-- ) right[i] += right[i+ 1 ] + (str. charAt(i) == ' K' ? 1 : 0 );
49+ for (int i= 1 ;i<= N ;i++ ) R_cnt [i] += R_cnt [i- 1 ] + (str. charAt(i) == ' R' ? 1 : 0 );
50+
51+ int ans = 0 ;
52+ int s = 0 , e = N + 1 , K_cnt = 0 ;
53+ while (s< e){
54+ if (R_cnt [e- 1 ] - R_cnt [s] == 0 ) break ;
55+ ans = Math . max(ans, R_cnt [e- 1 ] - R_cnt [s] + K_cnt * 2 );
56+ K_cnt ++ ;
57+ while (s<= N && left[s] < K_cnt ) s++ ;
58+ while (e>= 1 && right[e] < K_cnt ) e-- ;
59+ }
60+ bw. write(ans+ " \n " );
61+
62+ }
63+
64+ }
65+
66+ ```
You can’t perform that action at this time.
0 commit comments