Language agnostic 代码:决策树

Language agnostic 代码:决策树,language-agnostic,rosetta-stone,Language Agnostic,Rosetta Stone,在Google Code Jam 2009中,有一个称为决策树的问题,它提供了相当有创意的解决方案 张贴你的最短解决方案;我将以半频繁的方式将已接受的答案更新为当前最短条目,假设您不只是为了解决此问题而创建新语言:-P 当前排名: 107 121 132 154 一百六十 170 192 196 199 212 273 280 281 312 314 339 346 381 462 718 759 1554 暂时没有资格 方案(Guile 1.8) 这是我的版本,在去掉所有换行符(当然,除了字

在Google Code Jam 2009中,有一个称为决策树的问题,它提供了相当有创意的解决方案

张贴你的最短解决方案;我将以半频繁的方式将已接受的答案更新为当前最短条目,假设您不只是为了解决此问题而创建新语言:-P

当前排名:

  • 107
  • 121
  • 132
  • 154
  • 一百六十
  • 170
  • 192
  • 196
  • 199
  • 212
  • 273
  • 280
  • 281
  • 312
  • 314
  • 339
  • 346
  • 381
  • 462
  • 718
  • 759
  • 1554
  • 暂时没有资格
方案(Guile 1.8) 这是我的版本,在去掉所有换行符(当然,除了字符串文本中的换行符)之后,它的字节数为278字节(改进后的字节数减少到273字节)。它只适用于Guile1.8(因为在标准方案中,
define
是一种语法,而不是一个对象,但Guile将其表示为一个对象)

后记:170(常规)/160(ASCII85)/121(二进制) 我目前最短的(常规)PostScript解决方案,前提是将输入文件重命名为“r”(170个字符,包括换行符);使用特定于重影脚本的过程(
=only
):

如果不允许使用ASCII可打印范围以外的字符,PS将内置二进制源的ASCII85编码。因此,在所有ASCII可打印字符中,我们有以下160字节的解决方案:

1[([){r]}/r(r)<~OuSUj0-P\*5*Dsn>`q:6@$5JU?'9>YBkCXV1Qkk'Ca"4@Apl(5.=75YP')1:5*?@0>C.bc@<6!&,:Se!4`>4SH!;p_OuQ[/1Herh>;'5D4Bm/:07B"95!G,c3aEmO4aiKGI?I,~>cvx exec
(defun r()(read))(dotimes(i(r))(format t"~&Case #~D:"(1+ i))(r)(set'z(r))(dotime
s(a(r))(r)(print(do((g(mapcar'read(make-list(r))))(p 1(*(pop c)p))(c z(if(find(p
op c)g)(car c)(cadr c))))((not c)p)))))
1[([){r]}/r(r)`q:6@$5JU?'9>YBkCXV1Qkk'Ca”4@Apl(5.=75YP')1:5*?@0>C。bc@4SH!;p_OuQ[/1Herh>;'5D4Bm/:07B“95!G,c3aEmO4aiKGI?,I,~>cvx exec
Common Lisp,199字节 每80个字符包装一次:

1[([){r]}/r(r)<~OuSUj0-P\*5*Dsn>`q:6@$5JU?'9>YBkCXV1Qkk'Ca"4@Apl(5.=75YP')1:5*?@0>C.bc@<6!&,:Se!4`>4SH!;p_OuQ[/1Herh>;'5D4Bm/:07B"95!G,c3aEmO4aiKGI?I,~>cvx exec
(defun r()(read))(dotimes(i(r))(format t"~&Case #~D:"(1+ i))(r)(set'z(r))(dotime
s(a(r))(r)(print(do((g(mapcar'read(make-list(r))))(p 1(*(pop c)p))(c z(if(find(p
op c)g)(car c)(cadr c))))((not c)p)))))
间隔和缩进:

(defun r () (read))
(dotimes (i (r))
  (format t "~&Case #~D:" (1+ i))
  (r)
  (set 'z (r))
  (dotimes (a (r))
    (r)
    (print
      (do ((g (mapcar 'read (make-list (r))))
           (p 1 (* (pop c) p))
           (c z (if (find (pop c) g)
                    (car c)
                    (cadr c))))
          ((not c) p)))))
192年的Python 导入re;S=re.sub;R=raw\u input;I=input;c=0;exec R“c+=1;L=S('\)*\(',')或''S('[a-z]+)','*('''\'\\1'在a和',eval('+R()'*I(''Case.\35;%S:\n'%c))[1:]);exec'a=R()+\';print eval(L);'I()“*I() 132年的红宝石 由leonid改进。换行符是必不可少的

def j
'1
'..gets
end
j.map{|c|s=j.map{gets}*''
puts"Case #%d:"%c,j.map{gets;eval s.gsub(/[a-z]+/,'*(/ \&\b/?').gsub /\)\s*\(/,'):'}}
136年的红宝石 我刚刚了解到*“”相当于.join“”。还意识到map可以在一些地方使用

红宝石150 我只是ruby的一个新手,所以可能还有很大的改进空间

Haskell,514字节(我很烂?)

基于:

import-Control.Monad
导入Text.ParserCombinators.Parsec
数据F=N | F字符串(浮点,F)(浮点,F)
r=返回
f=多个字母>>=\i->w>>d>=\t->d>>=r.f i t
d=char'('>>w>>many1(其中一个“.0123456789”)>>=\g->w>>(fr N)>>=\p->char')”>>w>>r(读取g,p)
w=多个“\n”中的一个
g=getLine
l=readLn
m=复制项
main=l>>=\n->forM[1..n]$\t->putStrLn(“Case#“++show t++:”)>>l>=(`m`g)>>=(\(右q)->l>=(`m`p q))。解析d'。加入
z(p,f)=(p*).yf
y N=1
y(F n t F)x=z(如果n`elem`x那么t else F)x
p q=fmap(drop 2.words)g>>=print.z q
Perl,107个字符
说(“Case#$\”),
$\ux=eval“'.”,
s:[a-z]+:*(/$&\\s/?:g,s/\)\s*\(/):/g,
评估“\$”;说“$\”;X的1。。
换行符的易读性;它们都不是必需的,也不计入

它使用了仅在最新版本的Perl中才能找到的功能,因此请使用
Perl-M5.010
或更高版本运行

我以前也是Perl noob,所以它的工作原理与ruby的差不多。原始版本126个字符,由peutri优化

反向链接:

C(489字节) 代码包装为80个字符,实际上只有3行

保存在a.c中并使用:gcc-wa.c-oa编译

#define S int I,N;scanf("%d\n",&N);for(I=-1;++I<N;)
#define M 1000
char B[M],Z[M],Q[M]={' '},*F[M],*V;float W[M],H;int J,C,L[M],R[M];t(){V=strtok(0
," \n()");}p(){int U=C++;F[U]=0;if(!V)t();sscanf(V,"%f",W+U);t();if(V&&*V>='a')s
trcpy(Q+1,V),V=0,F[U]=strdup(strcat(Q," ")),L[U]=p(),R[U]=p();return U;}main(){S
{printf("Case #%d:\n",I+1);*B=0;{S strcat(B,gets(Z));}V=strtok(B," \n(");C=0,p()
;{S{strcat(gets(B)," ");for(J=0,H=W[0];F[J];J=strstr(B,F[J])?L[J]:R[J],H*=W[J]);
printf("%f\n",H);};}}}
#为(I=-1;++I='a')S定义S int I,N;scanf(“%d\N”,&N)
trcpy(Q+1,V),V=0,F[U]=strdup(strcat(Q,”),L[U]=p(),R[U]=p();返回U;}main(){S
{printf(“Case#%d:\n,I+1);*B=0;{S strcat(B,get(Z));}V=strtok(B,”\n(“);C=0,p()
{S{strcat(get(B),”);for(J=0,H=W[0];F[J];J=strstr(B,F[J])?L[J]:R[J],H*=W[J];
printf(“%f\n”,H);};}}
467字节的Java 这使用Java6中包含的javascript解释器

import java.util.*;class D{static{Scanner c=new
Scanner(System.in);int n=c.nextInt(),i=0,l;while(i++<n){l=c.nextInt();String
s="(";while(l-->=0)s+=c.nextLine();System.out.println("Case #"+i+":");l=c.nextInt();while(l-->0)try{c.next();System.out.println(new
javax.script.ScriptEngineManager().getEngineByName("js").eval(s.replace(")","))").replaceAll("\\) *\\(",":(").replaceAll("[a-z]+","*(/ $0 /.test('"+c.nextLine()+" ')?")));}catch(Exception
x){}}System.exit(0);}}
import java.util.*;类D{static{Scanner c=new
Scanner(System.in);int n=c.nextInt(),i=0,l;而(i++=0)s+=c.nextLine();System.out.println(“Case#“+”+”:”);l=c.nextInt();而(l-->0)尝试{c.next();System.out.println(新
javax.script.ScriptEngineManager()
x) {}System.exit(0);}
感谢Varan、Chris和pfn(间接地)帮助我缩短它。
请参阅我的另一个(甚至更短!)java答案。

C-346字节 用gcc-w编译

#define N{int n=atoi(gets(A));for(;n--;)
T[999];F[99];char*t,*f,*a,A[99];float p(){float
d,m=1;for(;*t++^40;);sscanf(t,"%f %[^ (]",&d,A);if(*A^41){for(f=F;m**f;){for(;*f&&*f++^32;);for(a=A;*a&&*f==*a;f++,a++);m=*a||*f&64;}d*=!m*p()+m*p();}return
d;}main(I)N{printf("Case #%d:\n",I++);t=T;N
for(gets(t);*++t;);}N gets(F),t=T,printf("%f\n",p());}}}
使用1554个字符(纯)/281个字符(带bc) 是的,说真的

用法:
sed-r-f thisfile.sedoutput.out

#load"str.cma";;open List;;open String;;open Str;;let x=length and
y=Printf.printf and e=global_replace and h=float_of_string and b=regexp and
k=index and r=read_line and a=read_int and w s m c=sub s(c+1)(m-c-1);;for i=1to
a()do y"Case #%d:\n"i;let t=let n=a()in let rec g d j=if j>n then d else
g(d^(r()))(j+1)in e(b" ")""(e(b"\\b")"^"(g""1))and n=a()in let rec z j=if j>n
then()else let q=tl(split(b" ")(r()))in let rec g l j s p=let o=k s '('and c=k
s ')'in if j then let f=w s c o in if contains f '('then let m=k s '^'in let
c=index_from s(m+1)'^'in g 0(mem(w s c m)q)(w s(x s)c)(h(w s m o)*.p)else h f*.p
else if o<c then g(l+1)j(w s(x s)o)p else g(l-1)(l=1)(w s(x s)c)p in y"%f\n"(g
0(0=0)t 1.);z(j+1)in z 1done
(有关GNU sed的工作)

此解决方案省略小数点前的前导零,并且不处理答案为1.00的情况。幸运的是,GCJ法官接受没有零的情况,并且没有任何答案为1.00的情况

若要包含前导零,请将最后一行更改为
s/*@/0./
;若要处理1.00大小写,请追加行
s/^$/1/

这里有一个将乘法外包给bc的解决方案:

1d
/ /!{x
s/\n.*//
s/.*/echo 0&+1|bc/e
x
g
s/.*/Case #&:/p
:p
N
/[()]/s/ |\n//g
y/()/JK/
tp
H
d}
G
s/\n[^J]*/ %/
s/[^JK]*$//
:c
s/J([.-9]+)(.*)K/\2*\1/
/%\*/s/.*%.(.*)/echo \1|bc -l/e
:b
/J/s/T//
s/J([^JK]*)K/TC\1B/
tb
/ (.+) .*%\1C/{s/%[^C]*/%/
s/T.*B//
b}
s/%.*T/%/
:
y/CB/JK/
tc
314中的PHP
698字节的C++代码 使用“g++-o test source.cpp-include iostream-include vector-include sstream”编译

#define R(x,f,t) for(int x=f;x<t;x++){
#define S(x) x.size()
#define H string
#define U while
#define I if
#define D cin>>
#define X t.substr(p,S(t))
using namespace std;
int main(){int h,l,n,a,p,Y,W;D h;for(int q=1;q<=h;q++){D l;H s;char c;D c;R(i,0,l)H L;getline(cin,L);R(j,0,S(L))I (L[j]==41||L[j]==40)s+=32;s+=L[j];I(L[j]==40)s+=32;}}D a;printf("Case #%d:\n",q);R(i,0,a)H N;D N;D n;vector<H>f;R(j,0,n)D N;f.push_back(N);}H t=s;float P=1;p=0;U(p<S(t)-1){p=0;U(t[p]!=48&&t[p]!=49)p++;t=X;stringstream T(t);float V;T>>V;H F;T>>F;P*=V;I(F[0]==41)break;Y=0;R(j,0,S(f))if(F==f[j])Y=1;}p=t.find(40)+1;t=X;p=0;I(Y==0){W=1;U (W>0){I(t[p]==40)W++;I(t[p]==41)W--;p++;}t=X;p=0;}}cout<<P<<endl;}}return 0;}
#为(int x=f;x>
#定义X t.substr(p,S(t))
使用名称空间std;
intmain(){inth,l,n,a,p,Y,W;dh;for(intq=1;qV;hf;T>>F;p*=V;I(F[0]=41)break;Y=0;R(j,0,S(F))if(F==F[j])Y=1;}p=T.find(40)+1;T=X;p=0;I(Y==0{W=1;U(T[p]=0){I(T[p]=40)W++;I(T[p]=41);coull=0;}
这是对Haskell的另一个解释。我把脏活留给了Prelude的
lex
。它的包装是
Text.parsercompbinators.ReadP
。导入它本身就需要36个字符

解析器是一个
Features->SExp->Cuteness
函数,它省去了中的大部分类型声明

导入文本.Pars
#define S int I,N;scanf("%d\n",&N);for(I=-1;++I<N;)
#define M 1000
char B[M],Z[M],Q[M]={' '},*F[M],*V;float W[M],H;int J,C,L[M],R[M];t(){V=strtok(0
," \n()");}p(){int U=C++;F[U]=0;if(!V)t();sscanf(V,"%f",W+U);t();if(V&&*V>='a')s
trcpy(Q+1,V),V=0,F[U]=strdup(strcat(Q," ")),L[U]=p(),R[U]=p();return U;}main(){S
{printf("Case #%d:\n",I+1);*B=0;{S strcat(B,gets(Z));}V=strtok(B," \n(");C=0,p()
;{S{strcat(gets(B)," ");for(J=0,H=W[0];F[J];J=strstr(B,F[J])?L[J]:R[J],H*=W[J]);
printf("%f\n",H);};}}}
import java.util.*;class D{static{Scanner c=new
Scanner(System.in);int n=c.nextInt(),i=0,l;while(i++<n){l=c.nextInt();String
s="(";while(l-->=0)s+=c.nextLine();System.out.println("Case #"+i+":");l=c.nextInt();while(l-->0)try{c.next();System.out.println(new
javax.script.ScriptEngineManager().getEngineByName("js").eval(s.replace(")","))").replaceAll("\\) *\\(",":(").replaceAll("[a-z]+","*(/ $0 /.test('"+c.nextLine()+" ')?")));}catch(Exception
x){}}System.exit(0);}}
#define N{int n=atoi(gets(A));for(;n--;)
T[999];F[99];char*t,*f,*a,A[99];float p(){float
d,m=1;for(;*t++^40;);sscanf(t,"%f %[^ (]",&d,A);if(*A^41){for(f=F;m**f;){for(;*f&&*f++^32;);for(a=A;*a&&*f==*a;f++,a++);m=*a||*f&64;}d*=!m*p()+m*p();}return
d;}main(I)N{printf("Case #%d:\n",I++);t=T;N
for(gets(t);*++t;);}N gets(F),t=T,printf("%f\n",p());}}}
1d
/ /!{x
s/^$/Case #Y:/
:i
s/9Y/Y0/
ti
s/#Y/#0Y/
s/:/:0123456789/
s/(.)Y(.*):[0-9]*\1(.).*/\3\2Y:/
x
G
s/.*\n|Y//gp
z
:p
N
/[()]/s/ |\n//g
y/()/JK/
tp
H
d}
G
s/\n[^J]*/ %/
s/[^JK]*$//
:c
s/J1?([.-9]+)(.*)K/\2@\1/
/%@/by
:b
/J/s/T//
s/J([^JK]*)K/TC\1B/
tb
/ (.+) .*%\1C/{s/%[^C]*/%/
s/T.*B//
by}
s/%.*T/%/
:y
y/CB/JK/
tc
s/.\.0*\b//g
:r
/@.*@/{s/\w*@\w*$/C&B/
s/C(\w)(.*B)/\1C\2~/
s/"[^"]*/&0/g
:t
s/(\w)(C.*)(\w)B(.*~)/\1\2B\3\4\1\3/
T
s/~(10|2[01]|3[0-2]|4[0-3]|5[0-4]|6[0-5]|7[0-6]|8[0-7]|9.)/&Q/
s/(.)(.)Q/\2\1/
s/~0\w/`00/
s/~1\B/`0/
s/~22/`04/
s/~23/`06/
s/~24/`08/
s/~33/`09/
s/~25/`10/
s/~26|~34/`12/
s/~27/`14/
s/~28|~44/`16/
s/~29|~36/`18/
s/~35/`15/
s/~45/`20/
s/~37/`21/
s/~38|~46/`24/
s/~55/`25/
s/~39/`27/
s/~47/`28/
s/~56/`30/
s/~48/`32/
s/~57/`35/
s/~49|~66/`36/
s/~58/`40/
s/~67/`42/
s/~59/`45/
s/~68/`48/
s/~77/`49/
s/~69/`54/
s/~78/`56/
s/~79/`63/
s/~88/`64/
s/~89/`72/
s/~99/`81/
s/`(.)(.)/~\1'\2/
bt
:
s/(~.)'/\1/
s/..'/K&/
/K/bk
:v
s/=(,?.)'/\1/
s/,/1'/
t
s/B(.*)~/\1B"/
tr
s/"(\w*)0/A\1/g
/A.*A/{s/A[^A]*$/J&K/
:k
s/([^A])(J.*)([^A])K/\2K\1\3/
s/K(10|2[01]|3[0-2]|4[0-3]|5[0-4]|6[0-5]|7[0-6]|8[^9]|9.)/&Q/
s/(.)(.)Q/\2\1/
s/K0/=/
s/K11/=2/
s/K12/=3/
s/K13|K22/=4/
s/K14|K23/=5/
s/K15|K24|K33/=6/
s/K16|K25|K34/=7/
s/K(17|26|35|44)/=8/
s/K(18|27|36|45)/=9/
s/K(19|28|37|46|55)/W0/
s/K(29|38|47|56)/W1/
s/K(39|48|57|66)/W2/
s/K49|K58|K67/W3/
s/K59|K68|K77/W4/
s/K69|K78/W5/
s/K79|K88/W6/
s/K89/W7/
s/K99/W8/
s/W/=,/
/'/bv
s/\b=/K:/
tk
s/[:JK]A?//g
s/,/,0123456789GF/
s/(.),.*\1(.).*F/\2/
s/G/,0/
tk}
/A.*A/bv}
s/\w*C.*A//
tr
s/.*@/./
1d
/ /!{x
s/\n.*//
s/.*/echo 0&+1|bc/e
x
g
s/.*/Case #&:/p
:p
N
/[()]/s/ |\n//g
y/()/JK/
tp
H
d}
G
s/\n[^J]*/ %/
s/[^JK]*$//
:c
s/J([.-9]+)(.*)K/\2*\1/
/%\*/s/.*%.(.*)/echo \1|bc -l/e
:b
/J/s/T//
s/J([^JK]*)K/TC\1B/
tb
/ (.+) .*%\1C/{s/%[^C]*/%/
s/T.*B//
b}
s/%.*T/%/
:
y/CB/JK/
tc
<?php function q(){return trim(fgets(STDIN));}for($n=q($x=0);$x++<$n;){for($s=q($t='');$s--;$t.=q());echo"Case #$x:\n";for($z=q();$z--;){$l=explode(' ',q());$l[0]=0;printf("%f\n",eval('return'.preg_replace(array('/\(/','/(\w+),/','/(\d\)*),\((\d)/','/^./'),array(',(','*(in_array("$1",$l,1)?','$1:$2'),$t).';'));}}
#define R(x,f,t) for(int x=f;x<t;x++){
#define S(x) x.size()
#define H string
#define U while
#define I if
#define D cin>>
#define X t.substr(p,S(t))
using namespace std;
int main(){int h,l,n,a,p,Y,W;D h;for(int q=1;q<=h;q++){D l;H s;char c;D c;R(i,0,l)H L;getline(cin,L);R(j,0,S(L))I (L[j]==41||L[j]==40)s+=32;s+=L[j];I(L[j]==40)s+=32;}}D a;printf("Case #%d:\n",q);R(i,0,a)H N;D N;D n;vector<H>f;R(j,0,n)D N;f.push_back(N);}H t=s;float P=1;p=0;U(p<S(t)-1){p=0;U(t[p]!=48&&t[p]!=49)p++;t=X;stringstream T(t);float V;T>>V;H F;T>>F;P*=V;I(F[0]==41)break;Y=0;R(j,0,S(f))if(F==f[j])Y=1;}p=t.find(40)+1;t=X;p=0;I(Y==0){W=1;U (W>0){I(t[p]==40)W++;I(t[p]==41)W--;p++;}t=X;p=0;}}cout<<P<<endl;}}return 0;}
import Text.ParserCombinators.ReadP
main=f(\t->do putStrLn$"Case #"++show t++":";s<-r g;r$print.fst.head.($id=<<s).readP_to_S.d.tail.words=<<g)
d x=do"("<-e;w<-e;c<-do{f<-e;y<-d x;n<-d x;u$if elem f x then y else n}<++u 1.0;e;u$c*read w
f x=do n<-g;mapM x[1..read n]
e=readS_to_P lex
r=f.const
g=getLine
u=return
r='replace'
q=readline
for(n=0,t=q();t-n++;){for(print('Case #'+n+':'),d='',x=q();x--;d+=q());for(x=q();x--;)print(eval(d[r](/([a-z]+)/g,'*({'+q()[r](/ /g,':1,z')+':1}.z$1?')[r](/\) *\(/g,'):')))}
#define _ ENDDO
#define A READ(t(k:l-1),*),a
#define Q j=1,n;READ"(A)",s
#define R READ*,n;DO
#define S k+SCAN(t(k:),'()')
CHARACTER(999)s,t,u;R i=1,n;t="";PRINT"('Case #'i0':')",i
R Q;t=TRIM(t)//s;_;R Q;d=1;k=1;DO;k=S;l=S-1
IF(t(l:l)>"(")EXIT;A,u;d=d*a;k=l;m=0
IF(INDEX(s," "//TRIM(u)//" ")>0)CYCLE;DO;IF(')'>t(k:k))m=m+2;m=m-1;k=k+1
IF(1>m)EXIT;k=S-1;_;_;A;d=d*a;PRINT*,d;_;_;END
CHARACTER(999)s,t,u
READ*,n
DO i=1,n
    t=""
    PRINT"('Case #'I0':')",i
    READ*,n
    DO j=1,n
        READ"(A)",s
        t=TRIM(t)//s
    ENDDO
    READ*,n
    DO j=1,n
        READ"(A)",s
        d=1
        k=1
        DO
            k=k+SCAN(t(k:),'()')
            l=k+SCAN(t(k:),'()')-1
            IF(t(l:l)>"(")THEN
                READ(t(k:l-1),*),a
                d=d*a
                PRINT*,d
                EXIT
            ELSE
                READ(t(k:l-1),*),a,u
                d=d*a
                k=l
                m=0
                IF(INDEX(s," "//TRIM(u)//" ")>0)CYCLE
                DO
                    IF(')'>t(k:k))m=m+2
                    m=m-1
                    k=k+1
                    IF(1>m)EXIT
                    k=k+SCAN(t(k:),'()')-1
                ENDDO
            ENDIF
        ENDDO
    ENDDO
ENDDO
END
(for i 1((= r read))(prn"Case #"i":")(r)
(= z(r))(repeat(r)(r)(loop(= g(n-of(r)(r
))c z p 1)c(= p(*(pop c)p)c(if(pos(pop c
)g)c.0 cadr.c)))prn.p))
(for i 1 ((= r read))
  (prn "Case #" i ":")
  (r)
  (= z (r))
  (repeat (r)
    (r)
    (loop (= g (n-of (r) (r))
             c z
             p 1)
       c
       (= p (* (pop c) p)
          c (if (pos (pop c) g)
                (c 0)
                (cadr c))))
    (prn p)))
#load"str.cma";;open List;;open String;;open Str;;let x=length and
y=Printf.printf and e=global_replace and h=float_of_string and b=regexp and
k=index and r=read_line and a=read_int and w s m c=sub s(c+1)(m-c-1);;for i=1to
a()do y"Case #%d:\n"i;let t=let n=a()in let rec g d j=if j>n then d else
g(d^(r()))(j+1)in e(b" ")""(e(b"\\b")"^"(g""1))and n=a()in let rec z j=if j>n
then()else let q=tl(split(b" ")(r()))in let rec g l j s p=let o=k s '('and c=k
s ')'in if j then let f=w s c o in if contains f '('then let m=k s '^'in let
c=index_from s(m+1)'^'in g 0(mem(w s c m)q)(w s(x s)c)(h(w s m o)*.p)else h f*.p
else if o<c then g(l+1)j(w s(x s)o)p else g(l-1)(l=1)(w s(x s)c)p in y"%f\n"(g
0(0=0)t 1.);z(j+1)in z 1done
x=#lambda
w=#read
#(letrec((v(x(a)(map a(iota(w)1))))(c(x(f q)(*(car q)(if(any list? q)(c
f((if(memq(cadr q)f)caddr cadddr)q))1)))))(v(x(i)(w)(set! @(w))(format
#t"Case #~a:
~{~y~}"i(v(x i(w)(c(v(x i(w)))@)))))))
#define CONTEXTSTACK_SIZE 50
n=0
g=gsub
eval(parse(text=g('[^
]* [0-9]+( [^
]*|
)','f=c(\\1)
cat(eval(d),"
")
',g('
\\(','
cat("Case #",n<-n+1,":
",sep="")
d=expression(',g('" "','","',g(')\\s*\\(',',',g(' *("[a-z]+")\\s*\\(','*ifelse(\\1%in%f,',g('([a-z]+)','"\\1"',paste(readLines('A'),collapse='
')))))))))
import java.util.*;enum A{_;Scanner c,d;float p(String a){return
d.nextFloat()*(d.hasNext("\\D+")?a.contains(' '+d.next()+' ')?p(a)+0*p(a):0*p(a)+p(a):1);}{c=new
Scanner(System.in);for(int n=c.nextInt(),i=0,l;i++<n;){String
s="";for(l=c.nextInt();l-->=0;)s+=c.nextLine();System.out.println("Case #"+i+":");for(l=c.nextInt();l-->0;){c.next();d=new
Scanner(s.replaceAll("[()]"," "));System.out.println(p(c.nextLine()+' '));}}System.exit(0);}}
open System.Text.RegularExpressions
type t=T of float*(string*t*t)option
let rec e=function x,T(w,Some(s,a,b))->e(x,if Set.contains s x then a else b)*w|x,T(w,_)->w
let rec h x=Regex.Matches(x, @"\(|\)|\d\.\d+|\S+")|>Seq.cast<Match>|>Seq.map (fun x -> x.Value)|> Seq.toList
let rec p=function ")"::y->p y|"("::w::x::y->match x with ")"->T(float w,None),y|n->let a,f=p y in let b,g=p f in T(float w,Some(n,a,b)),g
let solve input =
    Regex.Matches(input,@"(\(((?<s>\()|[^()]|(?<-s>\)))*\)(?(s)(?!)))\s+\d+\s+((\S+\s\d(.+)?\s*)+)")
    |>Seq.cast<Match>
    |>Seq.map(fun m->fst(p(h(m.Groups.[1].Value))), [for a in m.Groups.[3].Value.Trim().Split([|'\n'|])->set(a.Split([|' '|]))])
    |>Seq.iteri(fun i (r,c)->printfn"Case #%i"(i+1);c|>Seq.iter(fun x->printfn"%.7F"(e(x, r))))
open System.Text.RegularExpressions

type decisionTree = T of float * (string * decisionTree * decisionTree) option
let rec eval = function 
    | x, T(w, Some(s, a, b)) -> eval(x, if Set.contains s x then a else b) * w
    | x, T(w, _) -> w

// creates a token stream
let rec tokenize tree =
    Regex.Matches(tree, @"\(|\)|\d\.\d+|\S+")
    |> Seq.cast<Match>
    |> Seq.map (fun x -> x.Value)
    |> Seq.toList

// converts token stream into a decisionTree
let rec parse = function
    | ")"::xs -> parse xs
    | "("::weight::x::xs ->
        match x with
        | ")" -> T(float weight, None), xs
        | name ->
            let t1, xs' = parse xs
            let t2, xs'' = parse xs'
            T(float weight, Some(name, t1, t2)), xs''

// uses regex to transform input file into a Seq<decisionTree, list<set<string>>, which each item in our 
// list will be tested against the decisionTree
let solve input =
    Regex.Matches(input, @"(\(((?<s>\()|[^()]|(?<-s>\)))*\)(?(s)(?!)))\s+\d+\s+((\S+\s\d(.+)?\s*)+)")
    |> Seq.cast<Match>
    |> Seq.map (fun m -> fst(parse(tokenize(m.Groups.[1].Value))), [for a in m.Groups.[3].Value.Trim().Split([|'\n'|]) -> set(a.Split([|' '|])) ])
    |> Seq.iteri (fun i (tree, testCases) ->
            printfn "Case #%i" (i+1)
            testCases |> Seq.iter (fun testCase -> printfn "%.7F" (eval (testCase, tree)))
        )
define(T,`translit($@)')define(Q,`patsubst($@)')define(I,0)Q
(T(T(T(Q(Q(Q(Q(Q(Q(T(include(A),(),<>),>\s*>,>>),>\s*<,>;),\
([a-z]+\)\s*<,`*ifElsE<rEgExp<P;``````` \1 ''''''';0>;0;<'),
^<,`defiNe<````I';iNcr<I>>\\"Case `#'I:\\"defiNe<`A'''';'),^
[0-9]*),.+ [0-9]+.*,`dEfiNE<```P';`\& '''>A'),<>;N,`(),n'),E
,e),()),.*,`syscmd(`echo "\&"|bc -l')')