-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathC.kt
85 lines (79 loc) · 2.12 KB
/
C.kt
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
fun main() {
val input = System.`in`.bufferedReader()
val output = StringBuilder()
val n = input.readLine()!!.toInt()
var s = mutableListOf<Pair<String, Int>>()
for(i in 0 until 2 * n - 2) {
val si = input.readLine()!!
s.add(Pair(si, i))
}
var ok = true
var ans = CharArray(2 * n - 2){'*'}
var used = BooleanArray(2 * n - 2){false}
var largestSuffix = ""
for(i in s) {
if(i.first.length > largestSuffix.length) {
largestSuffix = i.first
}
}
for(i in 0..n-2) {
var target = largestSuffix.subSequence(0 + i, n - 1)
var found = false
for (j in s) {
if(j.first == target) {
ans[j.second] = 'S'
used[j.second] = true
found = true
break
}
}
if(!found) {
ok = false
break
}
}
if(ok) {
var largestPrefix = ""
for(i in s) {
if(i.first.length > largestPrefix.length && !used[i.second]) {
largestPrefix = i.first
}
}
for(i in 0..n-2) {
var target = largestPrefix.subSequence(0, n - 1 - i)
var found = false
for (j in s) {
if(j.first == target && ans[j.second] == '*') {
ans[j.second] = 'P'
found = true
break
}
}
if(!found) {
ok = false
break
}
}
}
if(!ok) {
ans = CharArray(2 * n - 2){'S'}
var largestPrefix = largestSuffix
for(i in 0..n-2) {
var target = largestPrefix.subSequence(0, n - 1 - i)
var found = false
for (j in s) {
if(j.first == target) {
ans[j.second] = 'P'
found = true
break
}
}
if(!found) {
ok = false
break
}
}
}
output.append("${ans.joinToString("")}\n")
print(output)
}