-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day12p2.java
67 lines (55 loc) · 2.26 KB
/
Day12p2.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static Scanner sc = new Scanner(System.in);
public static PrintWriter out = new PrintWriter(System.out);
public static HashMap<HashMap<Integer, Integer>, Long> memo = new HashMap<>();
public static String s;
public static ArrayList<Integer> nums;
public static void solve() throws IOException {
String line;
int n = 1;
long ans = 0;
int idx = 0;
while ((line = br.readLine()) != null) {
memo = new HashMap<>();
String[] temp = line.split(" ")[1].split(",");
s = line.split(" ")[0]+'?'+line.split(" ")[0]+'?'+line.split(" ")[0]+'?'+line.split(" ")[0]+'?'+line.split(" ")[0];
nums = new ArrayList<>();
int sum = 0;
for(int i = 0; i < temp.length*5; i++) {
nums.add(Integer.parseInt(temp[i%temp.length]));
sum += nums.get(i);
}
ans += dfs(0, 0, sum);
}
out.println(ans);
}
public static long dfs(int sIdx, int numsIdx, int sum) {
HashMap<Integer, Integer> temp = new HashMap<>();
temp.put(sIdx, numsIdx);
if(memo.containsKey(temp)) return memo.get(temp);
long ans = 0;
if(numsIdx == nums.size()) {
if(s.substring(sIdx).contains("#")) {
return 0;
} else {
return 1;
}
}
if(sIdx == s.length()) return 0;
if(sum + nums.size()-numsIdx-1 > s.length()-sIdx) return 0;
if(numsIdx == nums.size()-1 && nums.get(numsIdx) == s.length()-sIdx && !s.substring(sIdx).contains(".")) ans++;
if(!s.substring(sIdx, sIdx + nums.get(numsIdx)).contains(".") && sIdx+nums.get(numsIdx) < s.length() && s.charAt(sIdx + nums.get(numsIdx)) != '#') ans += dfs(sIdx+nums.get(numsIdx)+1, numsIdx+1, sum-nums.get(numsIdx));
if(s.charAt(sIdx) != '#') ans += dfs(sIdx+1, numsIdx, sum);
memo.put(temp, ans);
return ans;
}
public static void main (String[] args) throws IOException {
solve();
out.close();
}
}