Skip to content

All Your Base

My solution for the "All Your Base" task of Google Code Jam 2009 Round1C (in C language):


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <regex.h>

int d[256];
int c[256];

main() {
  char str[4096];
  int t, i, numd, base, pc, code;
  size_t len, T;
  long long otv, k;

  fgets(str, sizeof(str), stdin);
  sscanf(str, "%d", &T);

  for (t = 0; t < T; t++) {
    fgets(str, sizeof(str), stdin);

    bzero(d, sizeof(d));
    bzero(c, sizeof(c));
    len = strlen(str);

    numd = 0;
    for (i = 0; i < len; i++) {
      if ((str[i] < '0' || str[i] > '9') && (str[i] < 'a' || str[i] > 'z')) continue;
      if (d[str[i]]) continue;
      switch(numd) {
      case 0: code = 1; break;
      case 1: code = 0; break;
      default: code = numd; break;
      }
      c[str[i]] = code;
      d[str[i]]++;
      numd++;
    }
    if (numd == 1) numd++;

    otv = 0;
    for(i = 0; i < len; i++) {
      if ((str[i] < '0' || str[i] > '9') && (str[i] < 'a' || str[i] > 'z')) continue;
      otv *= (long long)numd;
      otv += (long long)c[str[i]];
    }

    printf("Case #%d: %lld\n", t + 1, otv);
  }

  return 0;
}

Leave a Reply

Your email address will not be published.