小编dc.*_*dc.的帖子

Trie实施

我试图在Java中实现一个非常简单的Trie,支持3个操作.我希望它有一个insert方法,一个has方法(即trie中的某个单词),以及一个以字符串形式返回trie的toString方法.我相信我的插入工作正常,但是并且toString被证明是困难的.这是我到目前为止所拥有的.

特里班.


public class CaseInsensitiveTrie implements SimpleTrie {

    //root node
    private TrieNode r;

    public CaseInsensitiveTrie() {
        r = new TrieNode();
    }

    public boolean has(String word) throws InvalidArgumentUosException {
        return r.has(word);
    }

    public void insert(String word) throws InvalidArgumentUosException {
        r.insert(word);
    }

    public String toString() {
        return r.toString();
    }

    public static void main(String[] args) {

        CaseInsensitiveTrie t = new CaseInsensitiveTrie();

        System.out.println("Testing some strings");
        t.insert("TEST");
        t.insert("TATTER");
        System.out.println(t.has("TEST"));
    }
}
Run Code Online (Sandbox Code Playgroud)

和节点类


public class TrieNode {

    //make child nodes
    private TrieNode[] c;
    //flag for end …
Run Code Online (Sandbox Code Playgroud)

java abstract-data-type trie radix radix-tree

14
推荐指数
2
解决办法
3万
查看次数

为什么自定义MKMapView注释图像在触摸时会消失?

我正在注释我的地图并设置好图像,但是当我点击MapView上的注释时,图像会从我的自定义图像返回到红色图钉.为什么是这样?

- (MKAnnotationView *)mapView:(MKMapView *)newMapView viewForAnnotation:(id )newAnnotation {
    MKPinAnnotationView *annotation = [[MKPinAnnotationView alloc] initWithAnnotation:newAnnotation reuseIdentifier:@"currentloc"];
    if (annotation == nil) {
        annotation = [[MKAnnotationView alloc] initWithAnnotation:newAnnotation reuseIdentifier:@"currentloc"];
    }

    annotation.image = [UIImage imageNamed:@"anno.png"];
    annotation.canShowCallout = YES;
    annotation.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bus_stop_30x30.png"]];
    annotation.leftCalloutAccessoryView = imgView;

    return annotation;
}
Run Code Online (Sandbox Code Playgroud)

我的代码看起来与一些不会产生此问题的示例代码相同.

iphone annotations mapkit

6
推荐指数
1
解决办法
6692
查看次数

树:链接列表与数组(效率)

这是一个分配问题,我在回答问题时遇到了问题.

"假设一个树每个节点最多可以有k个子节点.假设v是每个节点的平均子节点数.对于v的值,将子节点存储在一个节点中的效率更高(就使用的空间而言)链表与数组中的存储?为什么?"

我相信我能回答"为什么?" 或多或少用简单的英语 - 使用链表更有效率,因为而不是拥有一堆空节点(如果你的平均值低于最大值,数组中的空索引)占用内存你只需要分配空间当您实际填写值时,对于链接列表中的节点.

因此,如果在最大值为200时平均有6个子节点,则在创建树时,阵列将为每个节点的所有200个子节点创建空间,但链接列表将仅根据需要为节点分配空间.因此,使用链表,使用的空间大约是(?)平均值; 使用数组,间隔使用将是最大值.

...我不知道何时使用该阵列会更有效率.这是一个棘手的问题吗?我是否必须考虑到数组在创建时需要对节点总数进行限制的事实?

theory arrays tree performance linked-list

5
推荐指数
2
解决办法
5718
查看次数

套接字编程:sendto始终以errno 22(EINVAL)失败

我总是没有发送任何字节,使用此代码的错误为22(EINVAL,无效参数).在destination_host别处设置并且已知有效,所以我真的不知道发生了什么. MAXMSGSIZE是1000.没有错误或警告.我正在编译-Wall -Werror -pedantic

char *data_rec;
u_int data_len;

int sockfd;
uint16_t *ns;
struct sockaddr_in address;
struct sockaddr *addr;

char *ip;

int i;
int errno;
int bytes_sent;

data_len = MAXMSGSIZE;
data_rec = malloc(sizeof(char)*MAXMSGSIZE);
ns = malloc(MAXMSGSIZE*sizeof(uint16_t));
ip = malloc(MAXMSGSIZE*sizeof(char));

data_rec = "some random test stuff";

sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if(sockfd<0) {
    printf("socket() failed\n");
}

inet_ntop(AF_INET,destination_host->h_addr,ip,MAXMSGSIZE);
memset(&address, 0, sizeof(address));
address.sin_family = AF_INET;
address.sin_port = htons(theirPort);
address.sin_addr.s_addr = (unsigned long)ip;

addr = (struct sockaddr*)&address;
bind(sockfd,addr,sizeof(address));
/*Convert the message …
Run Code Online (Sandbox Code Playgroud)

c sockets udp sendto

5
推荐指数
2
解决办法
3万
查看次数

Prolog中的时间比较

假设我有一个time格式的结构time(hour, minute).我如何编写规则来比较它们?如果time1严格在time2之前,则compareTime(time1,time2)的行返回yes.

在与C合作多年后,我刚刚开始使用Prolog,整个语言对我来说非常混乱.

prolog

4
推荐指数
2
解决办法
2055
查看次数

Haskell的基本总结

我正在练习Haskell,并编写一个求和函数,它接受两个数字(上限和下限)并进行求和.

即,summation 0 10将返回55

我可以让它主要工作,但很难找到如何只使用两个参数获得它.

这是我到目前为止:

summation :: Integer -> Integer -> Integer -> Integer
summation x y sum =
    if (y<x) then
        sum
    else
        summation x (y-1) (sum+y)
Run Code Online (Sandbox Code Playgroud)

所以这很好,但我需要做的summation 0 10 0是让它正常工作.我不知道如何才能在Haskell中只使用两个参数.

recursion haskell

4
推荐指数
2
解决办法
5436
查看次数

Perl 的反引号/系统给出“tcsetattr:输入/输出错误”

我正在编写一个 perl 脚本,它使用 ssh 在几个不同的服务器上启动一个脚本。只要此脚本正在运行,远程脚本就需要运行:

#!/usr/bin/perl
require 'config.cfg'

#@servers is defined in config.cfg
#Contains server info as [username,hostname]
#
# @servers = ([username,server1.test.com],[username,server2.test.com]) 
#
foreach $server ( @servers ) {
    my $pid = fork();
    if ( $pid == 0 ) {
        $u = ${$server}[0];
        $h = ${$server}[1];
        print "Running script on $h \n";
        print `ssh -tl $u $h perl /opt/scripts/somescript.pl`;
        exit 0;
    } else {
        die "Couldn't start the process: $!\n";
    }
}
[...]
Run Code Online (Sandbox Code Playgroud)

当我运行这个脚本时,我得到以下输出:

./brokenscript.pl
在 server01 上运行脚本
$ …

ssh perl

4
推荐指数
1
解决办法
5471
查看次数