我正在尝试创建一个哈希表来确定需要插入词法(字符串)的索引。哈希函数和表工作正常,但我面临的问题是回文和具有相同字符集的字符串。如果表中有一个字符串,可以说“nab”,如果用户输入另一个字符串“ban”,那么新的字符串会替换表中的字符串。
这是我目前正在使用的示例代码。
String lexime;
int token; //This determines the index in the table
String hashTable[20];
cin >> lexime;
token=hash(lexime);
hashTable[token]=lexime;
int function hash(String lex){
int token=0;
int length=lex.size();
for (int i=0;i<length;i++){
char temp=lex[i];
token+=(int)temp;
}
return token%20;
}