-
Notifications
You must be signed in to change notification settings - Fork 0
/
First
106 lines (80 loc) · 3.01 KB
/
First
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
import java.io.*;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;
public class First {
public static void main(String args[]) throws Exception {
FileReader fr = new FileReader("E:\\Work\\IntelliJ Idea Projects\\src\\InputFirst");
BufferedReader br = new BufferedReader(fr);
int noP = Integer.parseInt(br.readLine());
String sent[] = new String[noP];
for (int i = 0; i < noP; i++)
sent[i] = br.readLine();
String out[] = new String[noP+1];
out[noP]="~";
for (int i = 0; i < noP; i++) {
String s[] = sent[i].split("->");
String right = s[0];
String left = s[1];
//System.out.println("----------------\n"+" s[0] = "+right+" s[1] = "+left+"\n\n");
out[i] = right;
boolean t = true;
one:
while (Character.isUpperCase(left.charAt(0)) == true) {
t = false;
char fchar = left.charAt(0);
//System.out.println("fchar = "+fchar);
String temp[];
for (int j = 0; j < noP; j++) {
if (j != i) {
temp = sent[j].split("->");
if (temp[0].charAt(0) == fchar) {
//System.out.println("temp[0] here is : "+ temp[0]);
left = temp[1];
if (Character.isUpperCase(left.charAt(0))) {
//System.out.println("Jumpinh to while");
continue one;
}
else
out[i] += left.charAt(0);
}
}
}
}
if (t)
out[i] += left.charAt(0);
}
/*Arrays.sort(out);
for (String x : out)
System.out.println(x);
System.out.println("--------------------"); */
boolean f = false; String temp="";
for (int i = 0; i < noP; i++) {
if (f == false) {
System.out.print(out[i].charAt(0) + " ");
f = true;
}
if (out[i].charAt(0) == out[i + 1].charAt(0))
temp += out[i].substring(1) + out[i + 1].substring(1);
else if (out[i].charAt(0) != out[i + 1].charAt(0)) {
temp += out[i].substring(1);
System.out.print(remDup(temp) + "\n");
temp="";
f = false;
}
}
}
static String remDup(String x){
String string = x;
char[] chars = string.toCharArray();
Set<Character> charSet = new LinkedHashSet<Character>();
for (char c : chars) {
charSet.add(c);
}
StringBuilder sb = new StringBuilder();
for (Character character : charSet) {
sb.append(character);
}
return sb.toString();
}
}