C, 775 771 chars
char*e[]={"h","he","li","be","b","c","n","o","f","ne","na","mg","al","si","p","s","cl","ar","k","ca","sc","ti","v","cr","mn","fe","co","ni","cu","zn","ga","ge","as","se","br","kr","rb","sr","y","zr","nb","mo","tc","ru","rh","pd","ag","cd","in","sn","sb","te","i","xe","cs","ba","la","ce","pr","nd","pm","sm","eu","gd","tb","dy","ho","er","tm","yb","lu","hf","ta","w","re","os","ir","pt","au","hg","tl","pb","bi","po","at","rn","fr","ra","ac","th","pa","u","np","pu","am","cm","bk","cf","es","fm","md","no","lr","rf","db","sg","bh","hs","mt","ds","rg","cn","uut","fl","uup","lv","uus","uu",0};
b[99],n;
c(w,o,l)char*w,*o,**l;{
return!*w||!strncmp(*l,w,n=strlen(*l))&&c(w+n,o+sprintf(o,",%d",l-e+1),e)||*++l&&c(w,o,l);
}
main(){
while(gets(b))c(b,b+9,e)&&printf("%s%s\n",b,b+9);
}
Input: Word per line, must be lowercase. usr/share/dict/words
is fine.
Output: Word and numbers, e.g.: acceptances,89,58,15,73,7,6,99
Logic:
c(w,o,l)
checks the word w
, starting with element l
.
Two-way recursion is used - if the first element matches the head of the element list, check the remainer of w
against the full element list. If this match fails, check the word against the tail of the list.
The buffer o
accumulates the element numbers along the successful path. After a match, it will contain the list of numbers, and is printed.
Issues:
The list isn't encoded efficiently - too much "
and ,
". But this way it's easy to use. I'm sure it can be much improved, without too much cost in code.