-
Notifications
You must be signed in to change notification settings - Fork 55
/
Java.java
58 lines (45 loc) · 1.76 KB
/
Java.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
/****************************************/
/* */
/* CodinGame.com Solutions by pathosDev */
/* */
/* Puzzle: Bulk Email Generator */
/* Difficulty: Easy */
/* Date solved: 12.11.2018 */
/* */
/****************************************/
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Solution
{
public static void main(String[] args)
{
//Read inputs.
Scanner scanner = new Scanner(System.in);
int N = Integer.parseInt(scanner.nextLine());
String[] emailTemplateLines = new String[N];
for (int i = 0; i < N; i++)
{
emailTemplateLines[i] = scanner.nextLine();
}
String emailTemplate = String.join("\n", emailTemplateLines);
//Create regex objects.
Pattern regexPattern = Pattern.compile("\\([^)]*\\)");
Matcher regexMatcher = regexPattern.matcher(emailTemplate);
int choicesCounter = -1;
String email = "";
int emailTemplateIndex = 0;
//Replacing choices.
while (regexMatcher.find())
{
choicesCounter++;
email += emailTemplate.substring(emailTemplateIndex, regexMatcher.start());
emailTemplateIndex = regexMatcher.end();
String[] choices = emailTemplate.substring(regexMatcher.start() + 1, regexMatcher.end() - 1).split("\\|", -1);
email += choices[choicesCounter % choices.length];
}
email += emailTemplate.substring(emailTemplateIndex);
//Output email.
System.out.println(email);
}
}