-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringTale.kt
76 lines (58 loc) · 1.55 KB
/
StringTale.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
import java.util.Arrays
fun main(args: Array<String>) {
val cnt = readLine()!!.toInt();
val src = readLine()!!;
val dst = readLine()!!;
val p = prefixFunc(src+(0.toChar())+dst);
val charMap:MutableMap<Char,Int> = mutableMapOf();
for (i in 0..src.length-1) {
charMap[ src[i] ] = i;
}
var lastPrefix = p[p.size-1];
for (j in lastPrefix downTo 1) {
var found = true;
val shift = src.length - j;
var i = j;
var k = 0;
for (c in charMap.keys) {
if (dst[ (shift + charMap[c]!!) % dst.length ] != c) {
found = false;
break;
}
}
if (!found)
continue;
while (i < src.length && k < dst.length - j) {
if (src[i] != dst[k]) {
found = false;
break;
}
i++;
k++;
}
if (found == true) {
println(shift.toString());
return;
}
}
println("-1");
}
fun prefixFunc(pattern:String):Array<Int> {
val prefix = Array<Int>(pattern.length, {0});
//0123456789
//nanannbanana
//001231001234
//[0, 0, 1, 2, 3, 1, 0, 0, 1, 2, 3, 4]
for (i in 1..prefix.size-1) {
var j = i-1 ;
while (pattern[ prefix[j] ] != pattern[i]) {
if (prefix[j] == 0)
break;
j = prefix[j]-1;
}
if (pattern[ prefix[j] ] == pattern[i]) {
prefix[i]=prefix[j]+1;
}
}
return prefix;
}