-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdobra.java
More file actions
160 lines (128 loc) · 4.03 KB
/
dobra.java
File metadata and controls
160 lines (128 loc) · 4.03 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/* Nathan Hicks
* COP 3503
* RP2 Dobra
* Read input of string with blanks, and print number of words that can be created
* following certain rules
*/
//Imports
import java.util.ArrayList;
import java.util.Scanner;
public class dobra {
//Global variables
public static char[] arr; //Character array formed from input string
public static int n; //Length of character array
public static long res = 0; //Total number of pleasant words
public static boolean usedL = false; //If L was found in initial string
public static ArrayList<Integer> weights = new ArrayList<>(); //Weight array for how much each type of character contributes to total
public static void main(String[] args) {
//Scan in initial string and convert to character array
Scanner in = new Scanner(System.in);
arr = in.nextLine().toCharArray();
n = arr.length;
in.close();
//Check if input is valid and look for L, then find pleasant words
if(valid()) go(0, usedL);
//Print result
System.out.println(res);
}
//Recursive permutation function for filling in blanks
public static void go(int k, boolean putL) {
boolean temp = putL; //True if L is currently contained in word
//Reached end of word
if (k == n) {
//Check if final word is pleasant
if (valid(k - 2) && (putL)) {
//Calculate permutations based on weight and add to result
long weight = 1;
for (int j = 0; j < weights.size(); j++) weight *= weights.get(j);
res += weight;
}
return;
}
//Only permute for blanks
if (arr[k] != '_') {
go(k+1, putL);
return;
}
//Try L
putL = true;
arr[k] = 'L';
///////// BACKTRACKING \\\\\\\\\ Only try next blank if current word up to this point is pleasant
if (valid(k - 2))
go(k+1, putL);
putL = temp;
//Try a vowel and update weight to number of vowels
arr[k] = 'A';
weights.set(k, 5);
///////// BACKTRACKING \\\\\\\\
if (valid(k - 2))
go(k+1, putL);
//Try a consonant and update weight to number of consonants not counting L
arr[k] = 'B';
weights.set(k, 20);
///////// BACKTRACKING \\\\\\\\
if (valid(k - 2))
go(k+1, putL);
//Reset to blank with weight of 1
arr[k] = '_';
weights.set(k, 1);
//End function
return;
}
//Check if full string is valid and contains letter L
public static boolean valid() {
//Vowel and consonant counters
int vowels = 0, cons = 0;
//Loop through input string array
for (int i = 0; i < arr.length; i++) {
//Create weights of 1 for each character
weights.add(1);
char c = arr[i];
//If vowel increment vowel counter and set consonant to 0
if(c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U' ) {
vowels++;
cons = 0;
//If L treat as consonant but set global usedL to true
} else if (c == 'L') {
cons++;
vowels = 0;
usedL = true;
//If consonant increment cons counter and set vowel to 0
} else if (c != '_') {
cons++;
vowels = 0;
//if blank set both counters to 0
} else {
vowels = 0;
cons = 0;
}
//If 3 vowels or consonants in a row then not valid
if (vowels == 3) return false;
if (cons == 3) return false;
}
//String is valid
return true;
}
//Check if string is valid from starting point j
public static boolean valid(int j) {
//If starting point is less than 0 then set to 0
if (j < 0) j = 0;
//Same check as valid() but starting in array at point j and no looking for L
int vowels = 0, cons = 0;
for (int i = j; i < n; i++) {
if(arr[i] == 'A' || arr[i] == 'E' || arr[i] == 'I' || arr[i] == 'O' || arr[i] == 'U' ) {
vowels++;
cons = 0;
} else if (arr[i] != '_') {
cons++;
vowels = 0;
} else {
vowels = 0;
cons = 0;
}
if (vowels == 3) return false;
if (cons == 3) return false;
}
return true;
}
}