commit 90546fc1d5fcd810c4fa0564d031940f024a17ae
parent 63fde8eb73cb91ee0d42edddcab0474e18b22f7d
Author: zerous Naveen Narayanan <zerous@nocebo.space>
Date: Tue, 26 Feb 2019 15:38:18 +0100
Codility - Strontium 2019 solve
Diffstat:
strontium.c | | | 63 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 63 insertions(+), 0 deletions(-)
diff --git a/strontium.c b/strontium.c
@@ -0,0 +1,63 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+int ch, b;
+
+void
+swap(char **a, int m, int n)
+{
+ char *t;
+
+ t = a[m];
+ a[m] = a[n];
+ a[n] = t;
+}
+
+void
+heapperm(char **a, int size, int n)
+{
+ int c, nc;
+
+ c = '\0';
+ nc = 0;
+
+ if (size == 1) {
+ for (int i = 0; i < n; i++) {
+ for (int j = 0; a[i][j] != '\0'; j++) {
+ if (a[i][j] != c) {
+ if (nc > b) {
+ b = nc;
+ ch = c;
+ }
+ c = a[i][j];
+ nc = 1;
+ }
+ else
+ nc++;
+ }
+ }
+ return;
+ }
+
+ for (int i = 0; i < size; i++) {
+ heapperm(a, size-1, n);
+
+ if (size % 2 == 1)
+ swap(a, 0, size-1);
+ else
+ swap(a, i, size-1);
+ }
+}
+
+int
+main(int argc, char **argv)
+{
+ ch = '\0';
+ b = 0;
+
+ heapperm(++argv, argc-1, argc-1);
+
+ printf("%c: %d\n", ch, b);
+
+ return 0;
+}