Magicka Problem - CodeJam'2011
- #include<stdio.h>
- #include<string.h>
- char combo[36][4],opps[28][3];
- int c,d;
- char base[]="QWERASDF";
- int search_combo(char buff[],char *tmp);
- int search_opps(char inp[]);
- char* strReverse(char* str)
- {
- int i=0,j=0;
- while(str[j] != '\0')
- j++;
- j--;
- while(i<=j)
- {
- char t = str[i];
- str[i++] = str[j];
- str[j--] = t;
- }
- return str;
- }
- void code_it(char inp[],char op[])
- {
- int i,j;
- op[0]='[';op[1]='\0';
- for(i=0,j=1;i<strlen(inp);i++)
- {
- if(i) strcat(op,(char *)", ");
- sprintf(op,"%s%c",op,inp[i]);
- }
- strcat(op,(char *)"]");
- }
- int main()
- {
- int t,cnt=0,n,i,j,k,l;
- char elm_list[101],op[101],otpt[700],buffer[3],tmp,tm_op[101];
- //freopen("B-large-practice.in", "r", stdin);
- //freopen("test.out", "w", stdout);
- scanf("%d",&t);
- while(t--)
- {
- scanf("%d",&c);
- for(i=0;i<c;i++)
- {
- scanf("%s",combo[i]);
- }
- scanf("%d",&d);
- for(i=0;i<d;i++)
- {
- scanf("%s",opps[i]);
- }
- scanf("%d %s",&n,elm_list);
- for(i=0,j=0;i<n;i++)
- {
- op[j]=elm_list[i];
- if(j>0)
- {
- buffer[0]=op[j-1];buffer[1]=op[j];buffer[2]='\0';
- if(search_combo(buffer,&tmp)==1)
- op[--j]=tmp;
- }
- op[++j]='\0';
- if(search_opps(op)==1)
- {
- j=0;
- op[j]='\0';
- }
- }
- code_it(op,otpt);
- printf("Case #%d: %s\n",++cnt,otpt);
- }
- }
- int search_combo(char buff[],char *tmp)
- {
- int i,j,fl=0;
- char tm[3];
- tm[2]='\0';
- for(i=0;i<c;i++)
- {
- for(j=0;j<2;j++) tm[j]=combo[i][j];
- if(strcmp(tm,buff)==0||strcmp(strReverse(tm),buff)==0)
- {
- *tmp=combo[i][2];
- fl=1;
- break;
- }
- }
- return fl;
- }
- int search_opps(char inp[])
- {
- int k,l,i,j,fl=0;
- char tmp[100],buff[3];
- buff[2]='\0';tmp[0]='\0';
- for(k=0,l=0;k<strlen(inp);k++)
- {
- if(strchr(tmp,inp[k])==NULL)
- if(strchr(base,inp[k])!=NULL)
- tmp[l++]=inp[k];
- tmp[l]='\0';
- }
- if(strlen(tmp)<1) return 0;
- for (i=0;i<strlen(tmp)-1;i++)
- {
- for (j=i+1;j<strlen(tmp);j++)
- {
- buff[0]=tmp[i];buff[1]=tmp[j];
- for(k=0;k<d;k++)
- {
- if(strcmp(opps[k],buff)==0||strcmp(opps[k],strReverse(buff))==0)
- {
- fl=1;
- break;
- }
- }
- if(fl==1) break;
- }
- if(fl==1) break;
- }
- return fl;
- }
It’s slightly big.I think I can reduce that.Your suggestions are highly solicited.
Discussion will be carried out if needed.
Discussion will be carried out if needed.
0 Responses to Google CodeJam 2011–Magicka Problem Solved–C Programming