小编ami*_*man的帖子

如何选择哈希表的大小?

假设我有200.000个单词,并且我将hash*33 + word[i]用作哈希函数,那么优化表的大小应该是什么,以便最小化内存/分页问题?

使用平台 - C(c99版),

单词是英文字符,ASCII值

一次初始化哈希表(链表样式的桶),

用于搜索下一个,如字典搜索.

碰撞后,该单词将作为新节点添加到存储桶中.

c hash hashmap data-structures

7
推荐指数
1
解决办法
1万
查看次数

Bytelandian Gold Coin,动态编程,解释?

这有点不成熟,但我不得不问,

这里提到的Bytelandian金币问题 - http://www.codechef.com/problems/COINS/ ,据说是典型的DP问题,尽管我已经阅读了DP和递归的基础知识,但我发现很难理解它解,

 # include <stdio.h>
 # include <stdlib.h>

long unsigned int costArray[30][19];
unsigned int amount;

unsigned int currentValue(short int factor2,short int factor3)
{ 
    int j;
    unsigned int current = amount >> factor2;
    for(j=0;j<factor3;j++)
    current /= 3;
    return current;
}

long unsigned int findOptimalAmount(short int factor2,short int factor3)
{
     unsigned int n = currentValue(factor2,factor3);
    if(n < 12)
    { 
        costArray[factor2][factor3] = n;
        return (long unsigned int)n;
    }
    else
    { 
        if(costArray[factor2][factor3] == 0)
        costArray[factor2][factor3] = (findOptimalAmount(factor2+1,factor3) + findOptimalAmount(factor2,factor3+1) + findOptimalAmount(factor2+2,factor3)); …
Run Code Online (Sandbox Code Playgroud)

c algorithm recursion dynamic-programming

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

strcmp不适用于fgets文件读取输入字符串

我正在从文件中读取文字并需要搜索一些特定的单词,下面是我的代码

string read = malloc(50 * sizeof(char));

FILE* p = fopen("word","r");

while(fgets(read,50,p))
{
    printf("%s\n",read);
    if(strcmp(read,"apple") == 0)
     {
        printf("apple found\n");
        break;
     }
}
Run Code Online (Sandbox Code Playgroud)

样本文件'word'如下,

$: more word

liol
apple
scizzors
big
bang
mentalist
scapegrace
goat
goti
Run Code Online (Sandbox Code Playgroud)

为什么strcmp在这种情况下不工作,printf可以打印字符串读取,所以char指针工作正常.

c

2
推荐指数
1
解决办法
1901
查看次数