Platinum Partner

C - Permutation Multiplication

This code multiplies the permutations given as imput via in. This uses the normal human search and write algorithm. It goes through the input many times.

Example:
In: (acfg)(bcd)(aed)(fade)(bgfae)
Out: (a d g) (c e b) (f)


typedef char str[64];

void pmul_c(str in, str out) {
  int n = strlen(in);
  char *m = (char*)malloc(sizeof(char) * n + 1);
  memset(m, 0, n);
  m[n] = 0;

  int k = 0;
  int i = 0;
  int c;
  for (; i < n; ++i)
    if (in[i] == '(') {
      m[i] = 1;
      c = in[i + 1];
    } else if (in[i] == ')') {
      m[i] = 1;
      in[i] = c;
    }

  char start;
  char curent;
A2:
  i = 0;
  while (m[i] == 1)
    ++i;
  if (i == n)
    return;
  start = in[i];
  out[k++] = '(';
  out[k++] = start;
  m[i] = 1;

A3:
  curent = in[++i];
  ++i;

A4:
  while ((i < n) && (in[i] != curent))
    ++i;
  if (i < n) {
    m[i] = 1;
    goto A3;
  }

  if (curent != start) {
    out[k++] = curent;
    i = 0;
    goto A4;
  }

  out[k++] = ')';
  goto A2;

  out[k] = 0;
}
{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}