-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLexicography.java
More file actions
69 lines (68 loc) · 1.44 KB
/
Lexicography.java
File metadata and controls
69 lines (68 loc) · 1.44 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
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.Scanner;
public class Lexicography {
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
String[] nlk = scan.nextLine().split(" ");
int n = Integer.parseInt(nlk[0]); //number of words
int l = Integer.parseInt(nlk[1]); //length of each word
int k = Integer.parseInt(nlk[2]) - 1; //index of smallest word
PriorityQueue<Character> data = new PriorityQueue<Character>();
for (char c : scan.nextLine().toCharArray())
data.add(c);
String[] output = new String[n];
Arrays.fill(output, "");
boolean diff = false;
char in;
char prev = '-';
int same = 0;
int x = 0;
while (diff == false && !data.isEmpty())
{
for(int i = x; i <= k; i++)
{
if (output[i].length() >= l)
{
same = 0;
break;
}
in = data.poll();
if (prev == in)
same++;
else
same = 0;
output[i] = output[i] + in;
prev = in;
}
if (same > 0)
{
x = k-same;
same = 0;
prev = '-';
}
else
diff = true;
}
while (output[k].length() < l)
{
output[k] = output[k] + data.poll();
}
int point = k + 1;
while (!data.isEmpty())
{
point%=output.length;
if (output[point].length() < l)
{
output[point] = output[point] + data.poll();
}
point++;
}
for (String s: output)
System.out.println(s);
scan.close();
}
}