// This code written by B.Tserenchimed // Source name: "shortest_code_judge.cpp" /*********************** Code Restrictions ************************************* 0. All source code submitted must be in C, C++ 1. "define"s are not allowed. 2. "typedef"s are not allowed. 3. "asm"s are not allowed 4. Only specific header files may be included in your solution. These will be specified separately per problem. *********************** Score Calculation *************************************** Maximize SCORE=50000/score; (minimize) score=10*S+NW+15*K+3*O+10000*D+10000*A S is the number of semicolons, NW is the number of non-white spaces, K is the number of C,C++ keywords, O is the number of commas, D is the number of multiply or divide symbols, A is the number of string "<<" or ">>". **********************************************************************************/ #include "spoj.h" #include <cstdio> #include <string> #include <cstdlib> #include <map> #include<vector> #include<cctype> #include<algorithm> using namespace std; char keywords[][10]={"asm","define","typedef","auto","break","case","class", "char","const","continue","default","do","double","else","enum","extern", "float","for","goto","if","int","long","register","return","short","signed", "static","sizeof","struct","switch","union","unsigned","void","volatile","while", "inline",""}; int count_non_white; int count_keyword; int count_char[128]; int leftshift; int rightshift; map<string,bool> keyword; vector<string> header; void read_file(FILE *fp){ int i,k,ch,prev_ch; char word[1000]; bool is_include; //Initiating variables count_non_white=0; count_keyword=0; leftshift=0; rightshift=0; for (i=0;i<128;i++) count_char[i]=0; header.clear(); keyword.clear(); for (i=0;keywords[i][0];i++) keyword[keywords[i]]=false; //Read source file k=0; is_include=false; prev_ch=-1; while((ch=fgetc(fp))!=EOF){ if (!isspace(ch)) count_non_white++; if (isalpha(ch)) word[k++]=(char)ch; else{ if (k>0){ word[k]=0; if (is_include){ header.push_back(word); is_include=false; } if (strcmp(word,"include")==0){ is_include=true; } if (keyword.find(word)!=keyword.end()){ count_keyword++; keyword[word]=true; } k=0; }//if k>0 }//else isalpha if (0<=ch&&ch<128) count_char[ch]++; if (prev_ch=='<'&&ch=='<') leftshift++; if (prev_ch=='>'&&ch=='>') rightshift++; prev_ch=ch; }//while } int make_score(){ int i,j,result=0; if (keyword["define"] ==true){ // Restriction 1. fprintf(stderr,"#define is not allowed\n"); return -1; } if (keyword["typedef"] ==true){ // Restriction 2. fprintf(stderr,"typedef is not allowed\n"); return -2; } if (keyword["asm"] ==true){ // Restriction 3. fprintf(stderr,"asm is not allowed\n"); return -3; } int n=2; char allowed[][40]={"stdio","cstdio"};//Can use stdio.h, cstdio for (j=0;j<header.size();j++){ for (i=0;i<n;i++) if (header[j]==allowed[i]) break; if (i==n) { // Restriction 4. fprintf(stderr,"#include<%s.h> is not allowed\n",header[j].c_str()); return -4; } } // Minimize the score=10*S+NW+15*K+3*O+10000*D+10000*A; return(10*count_char[';']+1*count_non_white+15*count_keyword+3*count_char[','] +10000*(count_char['*']+count_char['/']) +10000*(leftshift+rightshift)); } void show_result(int score){ fprintf(stderr,"Included files are\n"); for (int i=0;i<header.size();i++) fprintf(stderr,"\"%s\" ",header[i].c_str()); fprintf(stderr,"\n"); fprintf(stderr,"Used keywords are\n"); for (map<string,bool>::iterator it=keyword.begin();it!=keyword.end();it++) if (it->second){ fprintf(stderr,"\"%s\" ",it->first.c_str()); } fprintf(stderr,"\n"); fprintf(stderr,"Number of ';' is =%d\n",count_char[';']); fprintf(stderr,"Number of '*' is =%d\n",count_char['*']); fprintf(stderr,"Number of '/' is =%d\n",count_char['/']); fprintf(stderr,"Number of \"<<\" is =%d\n",leftshift); fprintf(stderr,"Number of \">>\" is =%d\n",rightshift); fprintf(stderr,"Number of non-white spaces =%d\n",count_non_white); fprintf(stderr,"Total number of keywords =%d\n",count_keyword); fprintf(stderr,"score=10*S+NW+15*K+3*O+10000*D+10000*A=%d\n",score<0?0:score); } int main(){ spoj_init(); /* spoj_p_out: correct output spoj_p_in : correct input spoj_t_out: output of the program spoj_score: score board spoj_t_src: source code of the problem. */ long long A,B; while(fscanf(spoj_p_out,"%lld",&A)==1){ if (fscanf(spoj_t_out,"%lld",&B)!=1) return 1; if (A!=B) return 1;//wrong answer } read_file(spoj_t_src); int score=make_score(); double onoo; if (score<=0) onoo=0.0; else{ onoo=500.0/score; if (onoo>1.0) onoo=1.0; } show_result(score); fprintf(spoj_score,"%lf\n",onoo); return 0; }

inserted by FC2 system