Repository navigation
Expand file tree
/
Copy pathCountConstruct.java
More file actions
67 lines (55 loc) · 1.94 KB
/
Copy pathCountConstruct.java
File metadata and controls
67 lines (55 loc) · 1.94 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
import java.util.*;
public class CountConstruct {
public static long countConstructRec(String str, String[] arr){
if(str.equals("")) return 1;
long count=0;
for(String s:arr){
if(str.startsWith(s)){
count+=countConstructRec(str.substring(s.length()),arr);
}
}
return count;
}
public static long countConstructMemo(String str, String[] arr, HashMap<String, Long> memo){
if(memo.containsKey(str)) return memo.get(str);
if(str.equals("")) return 1;
long count=0;
for(String s:arr){
if(str.startsWith(s)){
count+=countConstructRec(str.substring(s.length()),arr);
}
}
memo.put(str, count);
return count;
}
public static long countConstructTab(String str, String[] arr){
long[] tab = new long[(int)str.length()+1];
Arrays.fill(tab, 0);
tab[0]=1;
for(int i=0;i<=str.length();i++){
if(tab[i]!=0){
for(String sub:arr){
if(i+sub.length()<=str.length()){
if(str.substring(i,i+sub.length()).equals(sub)){
tab[i+sub.length()]+=tab[i];
}
}
}
}
}
return tab[str.length()];
}
public static void main(String[] args){
Scanner s = new Scanner(System.in);
String str = s.next();
int n= s.nextInt();
String[] arr = new String[n];
for(int i=0;i<n;i++) arr[i]=s.next();
//---Recursion---
//System.out.println(countConstructRec(str,arr));
//---Memoisation---
//System.out.println(countConstructMemo(str,arr,new HashMap<>()));
//---Tabulation---
System.out.println(countConstructTab(str,arr));
}
}