一种可能性是采用std::vector< std::vector<std::string> >,其中外部向量存储族,内部向量存储族中包含的单词。您可以按以下方式使用它(简化,每个单词最多包含一次特定字母的出现):
int word_length = 4;
vector< vector<string> > families(word_length+1,vector(0,string()));
/* parse your .txt file or the previous list here and insert words in the appropriate vector contained by family */
/* that means if the letter is at the n-th position you insert in families[n], if the letter is not contained it comes in families[0] */
//checking for the largest family:
unsigned int max = 0, max_index;
for(unsigned int ii=0; ii<families.size(); ii++)
{
if(families[ii].size() > max) {
max = families[ii].size();
max_index = ii;
}
}
//then you keep the vector at position max_index and this will be the starting point for estimating subsequent families;
由于您还可以在一个单词中多次出现一个字母,因此您必须扩展您的家庭向量的大小。要获得可能出现的次数,您可以使用二项式系数 (http://en.wikipedia.org/wiki/Binomial_coefficient) 并对所有出现次数求和(即从 1 到 word_length-1,因为一个单词不会只包含一个字母)。在这里,您必须确定您的家庭向量中的顺序(首先没有出现然后所有 1 出现,然后所有 2 出现,依此类推)。
或者,您可以使用 std::map 并使用 std::tupel 作为键和 std::vector<std::string> 作为值。如果 N 是单词的长度,则元组将是 N-tupel。通过使用std::tupel<bool> tupel 将保存如果有一个'E'在第一位,第二位等等。例如(f 表示假,t 表示真):“ALLY”对应于家庭(f,f,f,f),因为每个字母都不是“E”。 “BETA”对应(f,t,f,f),“FLEW”对应(f,f,t,f),以此类推。对于每个单词,您创建相应的tupel,并使用tupel 作为键在map(即家庭)的适当位置插入单词。
编辑:我不是百分百确定,但可能是元组不能用作 std::map 的键,因为它们是不可散列的对象(需要散列键以提供快速查看-在地图内)。或者,您可以使用相同的想法使用 std::string 作为键。而不是元组(f,f,f,f),您只需使用"0000",(f,t,f,f) 将转到"0100",依此类推。在这里您不必使用1s 和0s,但您可以使用任何您希望的编码(例如"False,True,False,False" 或类似的东西)。