#!perl -nla
push@v,($,,$r)=@F;$,eq$r||push@{$e{$r}},$,}sub v{$v{$_}=2;$v{$_}?$v{$_}&2&&die:&v for@{$e{$_}};$v{$_}=print}{$v{$_}||&v for@v
|
Only a few strokes off. Need new algorithm
#!perl -nla
@v{($,,$r)=@F}=0;$,eq$r||push@{$e{$r}},$,}sub v{$v{$_}=2;$v{$_}?$v{$_}&2&&die:&v for@{$e{$_}};$v{$_}=print}{$v{$_}||&v for keys%v
|
Here's algorithm #3. I love google
#!perl -nla
@v{($,,$r)=@F}=();$,eq$r||push@{$e{$r}},$,}sub
v{$v{$_}=1;$v{$_}?$v{$_}&1&&die:&v for@{$e{$_}};print;$v{$_}=2}{$v{$_}||&v for keys%v
|
#!perl -nla
$p{$_}+=0for($,,$r)=@F;$,eq$r||++$p{$r}+push@{$s{$,}},$r}for(@q=grep!$p{$_},keys%p;@q;push@q,grep!--$p{$_},@{$s{
$_}}){print$_=pop@q}die if grep$_,values%p;{
|
This will probably land in the sand trap, too. This takes forever on the last test, but at least it uses constant memory this time
#!perl -nla
++$s{$_}for@F;$F[0]eq$F[1]||push@r,[@F]}&p(keys%s);sub p{$,=$/,print(@o),exit 0if!@_;for(@_){my$x=shift;$c{$o[$o++]=$x}=1,&p,$c{$x}=0,$o--if!grep{($,,$;)=@$_;$,eq$x&&$c{$;}||$;eq$x&&!$c{$,}}@r;push@_,$x}}die;{
|
This seems to work, but is probably the least efficient program I have ever written, so I expect the sand trap for my efforts
#!perl -an
$set{$_}++||push@set,$_ for@F;
$F[0]eq$F[1]or$rerules.=qr/(?>(?=(?>.*?^\Q$F[0]\E$).*^\Q$F[1]\E$))/ms;
}
sub perm{
my@a;
for(@_){
my$x=shift;
push@a,"$x\n$_"for&perm;
push@_,$x;
}
@a?@a:'';
}
for(&perm(@set)){
/$rerules/and print(),exit 0
}
die;
{
|
There I was thinking I needed recursion, when all I needed was to steal from tchrist's ppt :-) Lets see how easy this is to shorten
#!perl -pla
($l, $r) = @F;
$npred {$_} += 0for@F;
next if $l eq $r;
$pairs{$l}{$r}++;
++$npred{$r};
push @{$succ{$l}}, $r;
}
@list = grep !$npred{$_}, keys %npred;
while (@list) {
$_ = pop @list;
push @order,$_;
foreach my $child (@{$succ{$_}}) {
push @list, $child unless --$npred{$child};
}
}
die if grep $npred{$_}, keys %npred;
$_=join$/,@order;
{
|