/****************************************************************************** * Sample solution for the Word Encoding problem in NWERC'04 * Author: Andreas Björklund * Solution: Dynamic programming counting the number of strings of a given * length starting with a given pair of symbols. ******************************************************************************/ #include #include #define MAXN (1000) int cnt; char forb[27][27][27]; unsigned int Mx[21][27][27],CMx[4000][4]; void findString(int id,char *ans) { int i=0,l,a,b,j,k,stop; unsigned int sum; while (id>CMx[i][0]) i++; if (i) id-=CMx[i-1][0]; a=CMx[i][2]; b=CMx[i][3]; for (l=CMx[i][1];l>0;l--) { sum=stop=0; for (j=0;j<27 && !stop;j++) if (!forb[a][b][j]) { sum+=Mx[l-1][b][j]; stop=sum>=id; if (stop) { id-=sum-Mx[l-1][b][j]; } } ans[CMx[i][1]-l]=a+'a'-1; a=b; b=j-1; } ans[CMx[i][1]]=0; } int findId(char *query,unsigned int low,unsigned int high) { char cmp[30]; unsigned int middle=(low+high)/2; if (low>high) return -1; findString(middle,cmp); if (strcmp(query,cmp)==0) return middle; if (strlen(query)2000000000); } for (i=0;i