-
Notifications
You must be signed in to change notification settings - Fork 0
/
TheFairNutAndString.java
51 lines (49 loc) · 1.18 KB
/
TheFairNutAndString.java
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
/**
* https://codeforces.com/problemset/problem/1084/C #dynamic-programming chien thuat: 1. liet ke cac
* cach 2. sap xep lai cac cach de tim cthuc (ket thuc tai cung vi tri...) (cung la de kiem chung
* lai xem cach hieu co dung k)
*
* <p>p: position
*
* <p>s[p_i] = ki tu tai vi tri p_i
*
* <p>p_i 'b' p_i1
*
* <p>abbaa
*
* <p>result = 0; pre_b = 0; * * * * a b b a a b b a result 1 1 1 3 5 5 5 1+5+5=11 pre_b 0 1 2 2 2 5
* 5 => 3 + 2 = 5.
*
* <p>a b b a a b b a
*
* <p>*
*
* <p>*
*
* <p>* * * * * * *
*/
import java.util.Scanner;
public class TheFairNutAndString {
private static int MOD = 1000000007;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = "*" + sc.next(); // a trick
int n = s.length();
int[] result = new int[n];
int preB = 0;
for (int i = 1; i < n; i++) {
if (s.charAt(i) == 'a') {
result[i] = (result[i - 1] + 1) % MOD;
if (preB > 0) {
result[i] = (result[i] + result[preB - 1]) % MOD;
}
} else {
if (s.charAt(i) == 'b') {
preB = i;
}
result[i] = result[i - 1];
}
}
System.out.println(result[n - 1]);
}
}