google code jam (Qualification Round 2009)

問1だけやった。
問題文は公式のやつを見ておくれやす。http://code.google.com/codejam/contest/dashboard?c=90101#s=p0


ほかの方は、言語はC++とかJavaだが、かなり短いコード量で答えを出している。
優秀な人はいるものだな。普段どれだけ自分で「書いているか」でスピードや効率が決まるのだと実感させられる。
既存のコードの「直し」だとか「ポーティング」では、スピードや効率、「得られない」。


去年の使用したコードの統計http://www.go-hero.net/jam/08/languagesをみたら、最終戦で、Cを使っている人は1名しかいなかった。
あまり有利な言語ではない。


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

#define L 15
#define D 5000
#define N 500
#define RFILE "./D.txt"
#undef DEBUG
#define CATCH \
      j++; \
      d = c; \
      c = words[i][j];

char tokens[D][256];
char words[N][256];
int  answer[N];

char a[L][256];
int  b[L];
int  g[L];

int main(void){
  int i,j,k, parts, chars,fl;
  char c,d,s[256],t[256];
  FILE *fp;
  char *e;
#ifdef _DEBUG
  int z;
#endif

  d="";
  c="";

  if(( fp = fopen(RFILE,"r"))==NULL){
    printf("open error:%s\n",strerror(errno));
    return -1;
  }

  i=0;
  while(fgets(s, 256, fp) != NULL){
    if( i < D ){
      strcpy(tokens[i], s);
    }
    if( i >= D && i < (D+N) ){
      k=i-D;
      strcpy(words[k], s);
    }
    i++;
  }

  fclose(fp);

#ifdef _DEBUG
  printf("=====\n");
  z=0;
  for(z=0;z<D;z++){
    printf("%s",tokens[z]);
  }
  for(z=0;z<N;z++){
    printf("%s",words[z]);
  }
#endif

  i=0;
lunch:
  j=0;k=0;
  c = words[i][j];
  parts=0;
  chars=0;
  fl=0;

  while(c != '\n'){
    if ( c =='(' ){
      fl=1;
      if( j!=0 && d!=')'){
        g[k]=1;b[k++]=chars;chars=0;
      }
      parts++;
      CATCH
      continue;
    }
    if ( c ==')'){
      if(!fl){g[k]=1;}else{g[k]=0;}b[k++]=chars;chars=0;
      fl=0;
      CATCH
      continue;
    }
    if( j==0 )parts++;
    if( j!=0 && d==')'){ parts++;}
    a[parts-1][chars] = c;
    chars++;
    CATCH
  }
  if( d!=')' ){ g[k]=1;b[k++]=chars;chars=0;}

#ifdef DEBUG
  printf("=====\n");
  z=0;
  int y=0;
  printf("parts:%d\n",parts);
  for(z=0;z<parts;z++){
    for(y=0; y<b[z];y++){
      printf("%c",a[z][y]);
    }
    printf(",%d\n",b[z]);
    printf(",%d\n",g[z]);
  }
#endif

//chk------------
  int l,m,cnt;
  cnt=0;fl=0;
  for(l=0;l<D;l++){
      fl=0;
dinner:
      j=0;
      for(m=0;m<L;m++){
dinner2:
      while(j<parts){
dinner3:
      for(k=0;k<b[j];k++){
        if( tokens[l][m] == a[j][k]){
          fl=1;
          if(m == (L-1)){
            cnt++;
            goto breakfast;
          }
          m++;
          if(!g[j]){
          j++;
          if(j<parts)
            goto dinner3;
          else
            goto dinner2;
          }
        }else{
          if(!fl && m>0)
            goto breakfast;
        }
      }j++;}}
breakfast:
;
#ifdef DEBUG
   printf("%d,",fl);
   printf("%s",tokens[l]);
#endif
  }

  answer[i++]=cnt;

#ifdef _DEBUG
  printf("=====\n");
  printf("ans%d:%d\n",i-1,answer[i-1]);
#endif
  printf("Case #%d: %d\n",i,answer[i-1]);

  if(i < N)
  goto lunch;

#ifndef DEBUG
//output-----


#endif
  return 0;
}