算法问题:从数据集中按规则取指定数量的数据集合
遇到一个算法问题,一直不得求解,恳请大神指点!
现有数据:
<code><?php $data = array( array('e', 'l'), array('a', 'e', 'g', 'k', 'o', 'v', 'y'), array('d', 'g', 'l', 'v', 's', 'j', 'r', 'z', 'y', 'e', 'p', 't', 'w', 'q', 'x', 'i', 'f', 'c'), array('c', 'd', 'g', 'i', 'n', 's', 'u'), array('b', 'l', 'q', 'n', 't', 'm', 'p', 'x', 'e', 'k', 'd', 'h', 's', 'f', 'g', 'v', 'z', 'o', 'r', 'j'), array('l', 't', 'u', 'w'), array('c', 'f', 'g', 'h', 'i', 'l', 'n', 'o', 'p', 'q', 'u', 'w', 'y'), array('b', 'd', 'e', 'f', 'g', 'i', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'y'), array('g', 'a', 's', 'z', 'd', 'w', 'k', 'n', 'h', 'b', 'r'), array('j', 'g', 'c', 'u', 'h', 'o', 'w', 'z', 's', 'f', 'e', 'x', 'i', 'k', 'r', 'a', 'y', 't', 'l', 'p', 'q', 'v', 'n', 'b'), array('a', 'b', 'g', 'h', 'j', 'k', 'l', 'o', 'p', 'v', 'w', 'x'), array('m', 'q', 't', 'e', 'w', 'n', 'l', 'j', 'v', 'x', 'h', 'p', 'b', 'a', 'g', 'f', 'z', 'c', 'd', 'k', 'u', 'o', 's', 'r', 'y', 'i'), array('k', 'n', 'p', 'r'), array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'n', 'o', 'p', 'q', 'r', 'v', 'x', 'y'), array('j', 'k', 'p', 'z', 'g', 'c'), array('j', 'c', 'q', 'x', 'z', 'u', 'r', 't', 'm', 'l', 'f', 'n', 'h', 'y', 'w', 'p', 'd', 'k', 'b', 'i', 'a', 'o', 'e'), array('a', 'b', 'c', 'd', 'e', 'h', 'j', 'n', 'p', 'q'), array('a', 'c', 'd', 'e', 'h', 'i', 'j', 'p', 'q', 'r', 'y'), array('m', 'a', 'c', 'p', 't', 'z', 'd', 'u', 'l', 'f'), array('b', 'q', 't', 'u', 'w'), array('f', 'k', 'o', 'r'), array('y', 'n', 'l', 't', 'b', 'j', 'f', 'i', 'k', 'd', 'r', 'o', 'g', 'v', 'c', 'a', 'm', 'w', 'z'), array('q', 'u', 's', 'p', 'o', 'g', 'h', 'l', 'j', 'y', 'z', 'b'), array('c', 'y', 'j', 'v', 'a', 'g', 'f', 'l', 'm', 'z', 'o', 'e', 'u', 'q', 'k', 'n'), array('e', 'y', 'i', 'z', 'g', 'x', 'd', 's', 'h', 'w', 'j', 'b', 'm', 'v', 'u', 'a', 'k', 'n', 'r', 'q', 'p', 'f', 'o', 't', 'c'), array('a', 'c', 'd', 'e', 'f', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 'u', 'v', 'w', 'x', 'y'), array('b', 'g', 'h', 'i', 'j', 'l', 'm', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y'), array('n', 'b', 'c', 'i', 'o', 'k', 'g', 's', 'e', 't', 'w', 'z', 'a', 'l', 'j', 'u', 'h'), array('a', 'd', 'i', 'k', 'r', 'y'), array('e', 'f', 'g', 'j', 'l', 'n', 'o', 'r', 't', 'u', 'w', 'x'), array('t', 's', 'n', 'f', 'w', 'm', 'u', 'l', 'o', 'z', 'g', 'y', 'a', 'j', 'q', 'v', 'h', 'k', 'c', 'd'), array('q', 'g', 'b', 'p', 'z', 'i', 's', 'm', 'k', 'c', 'y', 'w', 'e', 'h'), array('h', 'x', 'r', 'e', 'f', 'b', 'd', 'n', 's', 'j', 'k', 'a', 'p', 'z', 'm', 'o', 'y', 'v', 'c', 'w', 't', 'g', 'u'), array('d', 'e', 'h', 'm', 'p'), array('u', 'm', 'd', 'k', 't', 'l', 'y', 'o', 'e', 'x', 'z', 'w', 'g', 'h', 'i', 'v', 'j', 'f', 'p', 'a', 'q', 'c', 'b', 'n', 's', 'r'), array('g', 'l', 's', 'y'), array('w', 'j', 'u', 's', 'h', 'i', 'z', 'o', 'm', 'r', 'v'), array('f', 'o', 'q', 'h', 'x', 's', 'w', 'g', 'p', 'd', 'l', 'j', 'k', 'c', 'n', 'e', 'i', 'z', 't', 'y', 'b', 'm'), array('n', 't', 'h', 'v', 'i', 'j', 'g', 'u', 'x', 'z', 'c', 'k'), array('h', 'e', 'r', 'z', 'w', 's', 'b'), array('k', 'i', 'w', 'm', 'g', 'a', 'r', 'n', 'y', 'x', 't', 'v', 'h', 'b', 'e', 'd', 'j', 'f', 'u', 'q', 's', 'p', 'c', 'z', 'o', 'l'), array('a', 'c', 'd', 'h', 'i', 'k', 'p', 'r', 'v', 'w', 'y'), array('m', 'a', 'n', 't', 'i', 'u', 'h', 'd', 'b', 'v', 'x', 'z', 'w', 'g', 's', 'y', 'j', 'l', 'e', 'o', 'c'), array('y', 't', 'r', 'w', 'n', 'u', 'z'), array('z', 'b', 'q', 'i', 'm', 'v', 't', 'h', 'a', 'k', 'e', 'o', 'w', 'f', 's', 'g', 'l', 'd', 'y', 'n', 'c', 'x', 'r', 'p', 'u', 'j'), array('b', 'c', 'e', 'h', 'k', 'l', 'n', 'o', 'p', 'q', 'v'), array('a', 'e', 'h', 'k', 'n', 'p', 'q', 'r', 't', 'x', 'y'), array('j', 'u', 'v', 'm', 'g', 'l', 'f', 'z', 'a', 'd', 'n', 'b', 'h', 'q', 'c', 'y', 'w'), array('g', 'j', 'r', 'u'), array('c', 'd', 'e', 'h', 'i', 'k', 'l', 'o', 'p', 'q', 'r', 'u', 'v', 'y'), array('y', 't', 'u', 'n', 'f', 'z', 'r', 'c', 'i', 'v', 'o', 'j', 'g', 'p', 'b', 'h', 'w', 'm', 'e', 'k', 'l', 's', 'd', 'q'), array('d', 'f', 'g', 'i', 'j', 'o', 'q', 'u', 'x'), array('a', 'b', 'c', 'e', 'f', 'g', 'h', 'i', 'k', 'l', 'p', 'q', 't', 'u', 'x'), array('p', 'u', 'r', 'y', 'v', 'l', 'b', 'n', 'z', 'k', 'q', 'm', 'a', 't', 'e', 'c'), array('k', 'j', 'x', 'v', 'p', 'd', 't', 'f', 'l', 's', 'c', 'z', 'b'), array('j', 'm', 't', 'e', 'v', 'u', 'k', 'w', 'c', 'b', 'q', 'y', 'o', 'i', 'p', 'h', 'g', 'z', 'd', 's', 'f', 'l', 'r', 'n'), array('a', 'r', 's'), array('n', 'd', 'e', 'g', 'r', 's', 'o', 'm', 'j', 'v', 't', 'x', 'z', 'a', 'i', 'y', 'f', 'k', 'h'), array('b', 'x', 'k', 'j', 'o', 'g', 'm', 'c', 'i', 'q', 'r', 'h', 'e', 'p', 'w', 'a', 'd', 'v', 'z'), array('x', 'o', 'l', 'z', 'j', 'h', 'y', 'g', 'k', 'u', 'p', 's', 'v', 'q', 'm', 'a', 'n', 'i', 't', 'b', 'e', 'd', 'c', 'f', 'r'), array('h', 'i', 'm', 'o', 'x', 'y'), array('m', 'y', 'n', 'p', 'l', 'e', 'o', 't', 'r', 'q', 'v', 'd', 'z'), array('a', 'c', 'd', 'f', 'g', 'h', 'i', 'j', 'k', 'o', 'p', 'q', 's', 'u', 'w', 'x'), array('a', 'd', 'h', 'i', 'j', 'l', 'm', 'n', 'o', 'r', 's', 'w', 'x', 'y'), array('f', 'k', 'l', 'y', 'p', 'e', 'n', 'j', 'v', 's', 't', 'm', 'd', 'x', 'z', 'r', 'i'), array('e', 'y', 'v', 't', 'x', 'r', 'k', 'n', 'p', 'm', 'i', 'z', 'j', 'l', 'c', 'g'), array('x', 'z', 'r', 'c', 'n', 'q', 'h', 'j', 'a', 'l'), array('p', 'z', 'x', 'm', 'r'), array('s', 'r', 'x', 'l', 'b', 'e', 't', 'g', 'y', 'w', 'm', 'z', 'o', 'a', 'v', 'i', 'q', 'd', 'p', 'c'), array('s', 'g', 'u', 'd', 'n', 'm', 't', 'a', 'y', 'x', 'w', 'v', 'r', 'l', 'f', 'z', 'q', 'j', 'k', 'e', 'p', 'h', 'b'), array('h', 'y', 'j', 'o', 'k', 'z', 'e', 'a', 'r', 'f', 'c'), array('z', 'm', 's'), array('c', 'd', 'e', 'f', 'g', 'j', 'l', 'm', 'o', 'q', 'r', 's', 't', 'u', 'v'), array('w', 'c', 'q', 'j', 'i', 'b', 'v', 'a', 'k', 'z', 'm', 'g', 'n', 'l', 's', 'd', 'u', 't', 'e', 'f', 'r', 'x'), array('a', 'o', 'k', 'd', 'm', 'q', 'p', 'e', 'r', 's', 'z', 'y'), array('a', 'c', 'f', 'g', 'h', 'j', 'n', 'q', 'r', 'u', 'x'), array('x', 'z', 'd', 's', 'f', 'i', 'e', 'h', 'c', 'j'), array('h', 'i', 'y'), array('z', 's', 'm', 'i', 'o', 'c', 'd', 'j', 'f', 't', 'p', 'h', 'q', 'x', 'r', 'e', 'k'), array('t', 'b', 'f', 'q', 'c', 'i', 'd', 'v', 'm', 'k', 'n', 's', 'e', 'p', 'x', 'h', 'z', 'y', 'r'), array('c', 'j', 'l', 'o', 'r'), array('z', 'd'), array('z', 'o', 'e', 'y', 'b', 'd', 'l', 'w', 'v'), array('k', 'd', 'z', 'o', 'm', 'c', 'i', 'n', 'p', 'x', 'e', 'y', 'w', 'b', 'f', 'h', 'r', 'g', 'q', 's', 'v'), array('k', 's'), array('o', 'g', 'z', 'b', 'm', 'n', 'u', 'w', 'q', 's', 't', 'i', 'c', 'h', 'd', 'x', 'f', 'y', 'a', 'k'), array('a', 'b', 'c', 'd', 'e', 'f', 'h', 'i', 'j', 'k', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x'), array('a', 'x', 't', 'm', 'l', 'i', 'v', 'd', 'y', 'n', 'q', 'o', 'h', 's', 'c', 'b', 'g', 'u', 'z', 'e', 'k', 'j', 'p'), array('j', 'k', 'p', 'z', 'u', 'q', 'n', 't', 'm', 'f', 'i', 'h', 'l', 'c', 'x', 'a', 'e', 'v', 'b', 's', 'r', 'y', 'w', 'd', 'g', 'o'), array('l', 'm', 'v', 'y'), array('c', 'f', 'h', 'i', 'j', 'n', 'p', 'q', 's', 't', 'w', 'x'), array('d', 'm', 's'), array('h', 'w', 'x', 'u', 'e', 'r', 'd', 'i', 'k', 'j', 'a', 'l', 'g', 'y', 'b', 'f', 't', 'n', 'o', 's', 'q', 'z', 'm', 'v'), array('f', 'y'), array('d', 'f', 'i', 'p', 'u'), array('a', 'b', 'j', 'm', 'r'), array('n', 'h', 'd', 'z', 'e', 'v', 'x', 'j', 'f', 'b', 'q', 'r', 'y', 'c', 'w', 'k', 'm'), array('x', 'y', 'q', 'h', 'w', 'r', 's', 'g', 'j', 'z', 'e', 'v', 'u', 'p'), array('h', 'w', 'm', 'l', 'f', 'p', 'a', 'v', 'b', 'q', 'g', 'y', 'c', 'u', 'r', 't', 'j', 'o', 'k', 'z', 'x', 'n', 'e', 'i', 's'), array('i', 'u'), ); $times = array( 'a' => 29, 'b' => 11, 'c' => 33, 'd' => 84, 'e' => 46, 'f' => 67, 'g' => 19, 'h' => 18, 'i' => 88, 'j' => 8, 'k' => 54, 'l' => 86, 'm' => 88, 'n' => 29, 'o' => 96, 'p' => 1, 'q' => 4, 'r' => 100, 's' => 89, 't' => 44, 'u' => 53, 'v' => 68, 'w' => 12, 'x' => 54, 'y' => 23, 'z' => 78, ); ?></code>
其中$data包含100组数据,每组数据由字母组成,数量和内容都是随机的。
其中$times是每个字母出现的次数。
现在需要从$data中取20组数据,使这20组数据排重过滤后组成的新数据中,所有字母出现次数总和最小(相比于其他的可能的组合)。
穷举法比较的路子走不通,因为从100组数据中取所有20组数据可能的组合,这个数据量太大。
请问,应该如何获取?
回复内容:
遇到一个算法问题,一直不得求解,恳请大神指点!
现有数据:
<code><?php $data = array( array('e', 'l'), array('a', 'e', 'g', 'k', 'o', 'v', 'y'), array('d', 'g', 'l', 'v', 's', 'j', 'r', 'z', 'y', 'e', 'p', 't', 'w', 'q', 'x', 'i', 'f', 'c'), array('c', 'd', 'g', 'i', 'n', 's', 'u'), array('b', 'l', 'q', 'n', 't', 'm', 'p', 'x', 'e', 'k', 'd', 'h', 's', 'f', 'g', 'v', 'z', 'o', 'r', 'j'), array('l', 't', 'u', 'w'), array('c', 'f', 'g', 'h', 'i', 'l', 'n', 'o', 'p', 'q', 'u', 'w', 'y'), array('b', 'd', 'e', 'f', 'g', 'i', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'y'), array('g', 'a', 's', 'z', 'd', 'w', 'k', 'n', 'h', 'b', 'r'), array('j', 'g', 'c', 'u', 'h', 'o', 'w', 'z', 's', 'f', 'e', 'x', 'i', 'k', 'r', 'a', 'y', 't', 'l', 'p', 'q', 'v', 'n', 'b'), array('a', 'b', 'g', 'h', 'j', 'k', 'l', 'o', 'p', 'v', 'w', 'x'), array('m', 'q', 't', 'e', 'w', 'n', 'l', 'j', 'v', 'x', 'h', 'p', 'b', 'a', 'g', 'f', 'z', 'c', 'd', 'k', 'u', 'o', 's', 'r', 'y', 'i'), array('k', 'n', 'p', 'r'), array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'n', 'o', 'p', 'q', 'r', 'v', 'x', 'y'), array('j', 'k', 'p', 'z', 'g', 'c'), array('j', 'c', 'q', 'x', 'z', 'u', 'r', 't', 'm', 'l', 'f', 'n', 'h', 'y', 'w', 'p', 'd', 'k', 'b', 'i', 'a', 'o', 'e'), array('a', 'b', 'c', 'd', 'e', 'h', 'j', 'n', 'p', 'q'), array('a', 'c', 'd', 'e', 'h', 'i', 'j', 'p', 'q', 'r', 'y'), array('m', 'a', 'c', 'p', 't', 'z', 'd', 'u', 'l', 'f'), array('b', 'q', 't', 'u', 'w'), array('f', 'k', 'o', 'r'), array('y', 'n', 'l', 't', 'b', 'j', 'f', 'i', 'k', 'd', 'r', 'o', 'g', 'v', 'c', 'a', 'm', 'w', 'z'), array('q', 'u', 's', 'p', 'o', 'g', 'h', 'l', 'j', 'y', 'z', 'b'), array('c', 'y', 'j', 'v', 'a', 'g', 'f', 'l', 'm', 'z', 'o', 'e', 'u', 'q', 'k', 'n'), array('e', 'y', 'i', 'z', 'g', 'x', 'd', 's', 'h', 'w', 'j', 'b', 'm', 'v', 'u', 'a', 'k', 'n', 'r', 'q', 'p', 'f', 'o', 't', 'c'), array('a', 'c', 'd', 'e', 'f', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 'u', 'v', 'w', 'x', 'y'), array('b', 'g', 'h', 'i', 'j', 'l', 'm', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y'), array('n', 'b', 'c', 'i', 'o', 'k', 'g', 's', 'e', 't', 'w', 'z', 'a', 'l', 'j', 'u', 'h'), array('a', 'd', 'i', 'k', 'r', 'y'), array('e', 'f', 'g', 'j', 'l', 'n', 'o', 'r', 't', 'u', 'w', 'x'), array('t', 's', 'n', 'f', 'w', 'm', 'u', 'l', 'o', 'z', 'g', 'y', 'a', 'j', 'q', 'v', 'h', 'k', 'c', 'd'), array('q', 'g', 'b', 'p', 'z', 'i', 's', 'm', 'k', 'c', 'y', 'w', 'e', 'h'), array('h', 'x', 'r', 'e', 'f', 'b', 'd', 'n', 's', 'j', 'k', 'a', 'p', 'z', 'm', 'o', 'y', 'v', 'c', 'w', 't', 'g', 'u'), array('d', 'e', 'h', 'm', 'p'), array('u', 'm', 'd', 'k', 't', 'l', 'y', 'o', 'e', 'x', 'z', 'w', 'g', 'h', 'i', 'v', 'j', 'f', 'p', 'a', 'q', 'c', 'b', 'n', 's', 'r'), array('g', 'l', 's', 'y'), array('w', 'j', 'u', 's', 'h', 'i', 'z', 'o', 'm', 'r', 'v'), array('f', 'o', 'q', 'h', 'x', 's', 'w', 'g', 'p', 'd', 'l', 'j', 'k', 'c', 'n', 'e', 'i', 'z', 't', 'y', 'b', 'm'), array('n', 't', 'h', 'v', 'i', 'j', 'g', 'u', 'x', 'z', 'c', 'k'), array('h', 'e', 'r', 'z', 'w', 's', 'b'), array('k', 'i', 'w', 'm', 'g', 'a', 'r', 'n', 'y', 'x', 't', 'v', 'h', 'b', 'e', 'd', 'j', 'f', 'u', 'q', 's', 'p', 'c', 'z', 'o', 'l'), array('a', 'c', 'd', 'h', 'i', 'k', 'p', 'r', 'v', 'w', 'y'), array('m', 'a', 'n', 't', 'i', 'u', 'h', 'd', 'b', 'v', 'x', 'z', 'w', 'g', 's', 'y', 'j', 'l', 'e', 'o', 'c'), array('y', 't', 'r', 'w', 'n', 'u', 'z'), array('z', 'b', 'q', 'i', 'm', 'v', 't', 'h', 'a', 'k', 'e', 'o', 'w', 'f', 's', 'g', 'l', 'd', 'y', 'n', 'c', 'x', 'r', 'p', 'u', 'j'), array('b', 'c', 'e', 'h', 'k', 'l', 'n', 'o', 'p', 'q', 'v'), array('a', 'e', 'h', 'k', 'n', 'p', 'q', 'r', 't', 'x', 'y'), array('j', 'u', 'v', 'm', 'g', 'l', 'f', 'z', 'a', 'd', 'n', 'b', 'h', 'q', 'c', 'y', 'w'), array('g', 'j', 'r', 'u'), array('c', 'd', 'e', 'h', 'i', 'k', 'l', 'o', 'p', 'q', 'r', 'u', 'v', 'y'), array('y', 't', 'u', 'n', 'f', 'z', 'r', 'c', 'i', 'v', 'o', 'j', 'g', 'p', 'b', 'h', 'w', 'm', 'e', 'k', 'l', 's', 'd', 'q'), array('d', 'f', 'g', 'i', 'j', 'o', 'q', 'u', 'x'), array('a', 'b', 'c', 'e', 'f', 'g', 'h', 'i', 'k', 'l', 'p', 'q', 't', 'u', 'x'), array('p', 'u', 'r', 'y', 'v', 'l', 'b', 'n', 'z', 'k', 'q', 'm', 'a', 't', 'e', 'c'), array('k', 'j', 'x', 'v', 'p', 'd', 't', 'f', 'l', 's', 'c', 'z', 'b'), array('j', 'm', 't', 'e', 'v', 'u', 'k', 'w', 'c', 'b', 'q', 'y', 'o', 'i', 'p', 'h', 'g', 'z', 'd', 's', 'f', 'l', 'r', 'n'), array('a', 'r', 's'), array('n', 'd', 'e', 'g', 'r', 's', 'o', 'm', 'j', 'v', 't', 'x', 'z', 'a', 'i', 'y', 'f', 'k', 'h'), array('b', 'x', 'k', 'j', 'o', 'g', 'm', 'c', 'i', 'q', 'r', 'h', 'e', 'p', 'w', 'a', 'd', 'v', 'z'), array('x', 'o', 'l', 'z', 'j', 'h', 'y', 'g', 'k', 'u', 'p', 's', 'v', 'q', 'm', 'a', 'n', 'i', 't', 'b', 'e', 'd', 'c', 'f', 'r'), array('h', 'i', 'm', 'o', 'x', 'y'), array('m', 'y', 'n', 'p', 'l', 'e', 'o', 't', 'r', 'q', 'v', 'd', 'z'), array('a', 'c', 'd', 'f', 'g', 'h', 'i', 'j', 'k', 'o', 'p', 'q', 's', 'u', 'w', 'x'), array('a', 'd', 'h', 'i', 'j', 'l', 'm', 'n', 'o', 'r', 's', 'w', 'x', 'y'), array('f', 'k', 'l', 'y', 'p', 'e', 'n', 'j', 'v', 's', 't', 'm', 'd', 'x', 'z', 'r', 'i'), array('e', 'y', 'v', 't', 'x', 'r', 'k', 'n', 'p', 'm', 'i', 'z', 'j', 'l', 'c', 'g'), array('x', 'z', 'r', 'c', 'n', 'q', 'h', 'j', 'a', 'l'), array('p', 'z', 'x', 'm', 'r'), array('s', 'r', 'x', 'l', 'b', 'e', 't', 'g', 'y', 'w', 'm', 'z', 'o', 'a', 'v', 'i', 'q', 'd', 'p', 'c'), array('s', 'g', 'u', 'd', 'n', 'm', 't', 'a', 'y', 'x', 'w', 'v', 'r', 'l', 'f', 'z', 'q', 'j', 'k', 'e', 'p', 'h', 'b'), array('h', 'y', 'j', 'o', 'k', 'z', 'e', 'a', 'r', 'f', 'c'), array('z', 'm', 's'), array('c', 'd', 'e', 'f', 'g', 'j', 'l', 'm', 'o', 'q', 'r', 's', 't', 'u', 'v'), array('w', 'c', 'q', 'j', 'i', 'b', 'v', 'a', 'k', 'z', 'm', 'g', 'n', 'l', 's', 'd', 'u', 't', 'e', 'f', 'r', 'x'), array('a', 'o', 'k', 'd', 'm', 'q', 'p', 'e', 'r', 's', 'z', 'y'), array('a', 'c', 'f', 'g', 'h', 'j', 'n', 'q', 'r', 'u', 'x'), array('x', 'z', 'd', 's', 'f', 'i', 'e', 'h', 'c', 'j'), array('h', 'i', 'y'), array('z', 's', 'm', 'i', 'o', 'c', 'd', 'j', 'f', 't', 'p', 'h', 'q', 'x', 'r', 'e', 'k'), array('t', 'b', 'f', 'q', 'c', 'i', 'd', 'v', 'm', 'k', 'n', 's', 'e', 'p', 'x', 'h', 'z', 'y', 'r'), array('c', 'j', 'l', 'o', 'r'), array('z', 'd'), array('z', 'o', 'e', 'y', 'b', 'd', 'l', 'w', 'v'), array('k', 'd', 'z', 'o', 'm', 'c', 'i', 'n', 'p', 'x', 'e', 'y', 'w', 'b', 'f', 'h', 'r', 'g', 'q', 's', 'v'), array('k', 's'), array('o', 'g', 'z', 'b', 'm', 'n', 'u', 'w', 'q', 's', 't', 'i', 'c', 'h', 'd', 'x', 'f', 'y', 'a', 'k'), array('a', 'b', 'c', 'd', 'e', 'f', 'h', 'i', 'j', 'k', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x'), array('a', 'x', 't', 'm', 'l', 'i', 'v', 'd', 'y', 'n', 'q', 'o', 'h', 's', 'c', 'b', 'g', 'u', 'z', 'e', 'k', 'j', 'p'), array('j', 'k', 'p', 'z', 'u', 'q', 'n', 't', 'm', 'f', 'i', 'h', 'l', 'c', 'x', 'a', 'e', 'v', 'b', 's', 'r', 'y', 'w', 'd', 'g', 'o'), array('l', 'm', 'v', 'y'), array('c', 'f', 'h', 'i', 'j', 'n', 'p', 'q', 's', 't', 'w', 'x'), array('d', 'm', 's'), array('h', 'w', 'x', 'u', 'e', 'r', 'd', 'i', 'k', 'j', 'a', 'l', 'g', 'y', 'b', 'f', 't', 'n', 'o', 's', 'q', 'z', 'm', 'v'), array('f', 'y'), array('d', 'f', 'i', 'p', 'u'), array('a', 'b', 'j', 'm', 'r'), array('n', 'h', 'd', 'z', 'e', 'v', 'x', 'j', 'f', 'b', 'q', 'r', 'y', 'c', 'w', 'k', 'm'), array('x', 'y', 'q', 'h', 'w', 'r', 's', 'g', 'j', 'z', 'e', 'v', 'u', 'p'), array('h', 'w', 'm', 'l', 'f', 'p', 'a', 'v', 'b', 'q', 'g', 'y', 'c', 'u', 'r', 't', 'j', 'o', 'k', 'z', 'x', 'n', 'e', 'i', 's'), array('i', 'u'), ); $times = array( 'a' => 29, 'b' => 11, 'c' => 33, 'd' => 84, 'e' => 46, 'f' => 67, 'g' => 19, 'h' => 18, 'i' => 88, 'j' => 8, 'k' => 54, 'l' => 86, 'm' => 88, 'n' => 29, 'o' => 96, 'p' => 1, 'q' => 4, 'r' => 100, 's' => 89, 't' => 44, 'u' => 53, 'v' => 68, 'w' => 12, 'x' => 54, 'y' => 23, 'z' => 78, ); ?></code>
其中$data包含100组数据,每组数据由字母组成,数量和内容都是随机的。
其中$times是每个字母出现的次数。
现在需要从$data中取20组数据,使这20组数据排重过滤后组成的新数据中,所有字母出现次数总和最小(相比于其他的可能的组合)。
穷举法比较的路子走不通,因为从100组数据中取所有20组数据可能的组合,这个数据量太大。
请问,应该如何获取?
<code><?php $startMemoryUsage = memory_get_usage(); $startTimeStamp = microtime(true); $data = array( array('e', 'l'), array('a', 'e', 'g', 'k', 'o', 'v', 'y'), array('d', 'g', 'l', 'v', 's', 'j', 'r', 'z', 'y', 'e', 'p', 't', 'w', 'q', 'x', 'i', 'f', 'c'), array('c', 'd', 'g', 'i', 'n', 's', 'u'), array('b', 'l', 'q', 'n', 't', 'm', 'p', 'x', 'e', 'k', 'd', 'h', 's', 'f', 'g', 'v', 'z', 'o', 'r', 'j'), array('l', 't', 'u', 'w'), array('c', 'f', 'g', 'h', 'i', 'l', 'n', 'o', 'p', 'q', 'u', 'w', 'y'), array('b', 'd', 'e', 'f', 'g', 'i', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'y'), array('g', 'a', 's', 'z', 'd', 'w', 'k', 'n', 'h', 'b', 'r'), array('j', 'g', 'c', 'u', 'h', 'o', 'w', 'z', 's', 'f', 'e', 'x', 'i', 'k', 'r', 'a', 'y', 't', 'l', 'p', 'q', 'v', 'n', 'b'), array('a', 'b', 'g', 'h', 'j', 'k', 'l', 'o', 'p', 'v', 'w', 'x'), array('m', 'q', 't', 'e', 'w', 'n', 'l', 'j', 'v', 'x', 'h', 'p', 'b', 'a', 'g', 'f', 'z', 'c', 'd', 'k', 'u', 'o', 's', 'r', 'y', 'i'), array('k', 'n', 'p', 'r'), array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'n', 'o', 'p', 'q', 'r', 'v', 'x', 'y'), array('j', 'k', 'p', 'z', 'g', 'c'), array('j', 'c', 'q', 'x', 'z', 'u', 'r', 't', 'm', 'l', 'f', 'n', 'h', 'y', 'w', 'p', 'd', 'k', 'b', 'i', 'a', 'o', 'e'), array('a', 'b', 'c', 'd', 'e', 'h', 'j', 'n', 'p', 'q'), array('a', 'c', 'd', 'e', 'h', 'i', 'j', 'p', 'q', 'r', 'y'), array('m', 'a', 'c', 'p', 't', 'z', 'd', 'u', 'l', 'f'), array('b', 'q', 't', 'u', 'w'), array('f', 'k', 'o', 'r'), array('y', 'n', 'l', 't', 'b', 'j', 'f', 'i', 'k', 'd', 'r', 'o', 'g', 'v', 'c', 'a', 'm', 'w', 'z'), array('q', 'u', 's', 'p', 'o', 'g', 'h', 'l', 'j', 'y', 'z', 'b'), array('c', 'y', 'j', 'v', 'a', 'g', 'f', 'l', 'm', 'z', 'o', 'e', 'u', 'q', 'k', 'n'), array('e', 'y', 'i', 'z', 'g', 'x', 'd', 's', 'h', 'w', 'j', 'b', 'm', 'v', 'u', 'a', 'k', 'n', 'r', 'q', 'p', 'f', 'o', 't', 'c'), array('a', 'c', 'd', 'e', 'f', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 'u', 'v', 'w', 'x', 'y'), array('b', 'g', 'h', 'i', 'j', 'l', 'm', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y'), array('n', 'b', 'c', 'i', 'o', 'k', 'g', 's', 'e', 't', 'w', 'z', 'a', 'l', 'j', 'u', 'h'), array('a', 'd', 'i', 'k', 'r', 'y'), array('e', 'f', 'g', 'j', 'l', 'n', 'o', 'r', 't', 'u', 'w', 'x'), array('t', 's', 'n', 'f', 'w', 'm', 'u', 'l', 'o', 'z', 'g', 'y', 'a', 'j', 'q', 'v', 'h', 'k', 'c', 'd'), array('q', 'g', 'b', 'p', 'z', 'i', 's', 'm', 'k', 'c', 'y', 'w', 'e', 'h'), array('h', 'x', 'r', 'e', 'f', 'b', 'd', 'n', 's', 'j', 'k', 'a', 'p', 'z', 'm', 'o', 'y', 'v', 'c', 'w', 't', 'g', 'u'), array('d', 'e', 'h', 'm', 'p'), array('u', 'm', 'd', 'k', 't', 'l', 'y', 'o', 'e', 'x', 'z', 'w', 'g', 'h', 'i', 'v', 'j', 'f', 'p', 'a', 'q', 'c', 'b', 'n', 's', 'r'), array('g', 'l', 's', 'y'), array('w', 'j', 'u', 's', 'h', 'i', 'z', 'o', 'm', 'r', 'v'), array('f', 'o', 'q', 'h', 'x', 's', 'w', 'g', 'p', 'd', 'l', 'j', 'k', 'c', 'n', 'e', 'i', 'z', 't', 'y', 'b', 'm'), array('n', 't', 'h', 'v', 'i', 'j', 'g', 'u', 'x', 'z', 'c', 'k'), array('h', 'e', 'r', 'z', 'w', 's', 'b'), array('k', 'i', 'w', 'm', 'g', 'a', 'r', 'n', 'y', 'x', 't', 'v', 'h', 'b', 'e', 'd', 'j', 'f', 'u', 'q', 's', 'p', 'c', 'z', 'o', 'l'), array('a', 'c', 'd', 'h', 'i', 'k', 'p', 'r', 'v', 'w', 'y'), array('m', 'a', 'n', 't', 'i', 'u', 'h', 'd', 'b', 'v', 'x', 'z', 'w', 'g', 's', 'y', 'j', 'l', 'e', 'o', 'c'), array('y', 't', 'r', 'w', 'n', 'u', 'z'), array('z', 'b', 'q', 'i', 'm', 'v', 't', 'h', 'a', 'k', 'e', 'o', 'w', 'f', 's', 'g', 'l', 'd', 'y', 'n', 'c', 'x', 'r', 'p', 'u', 'j'), array('b', 'c', 'e', 'h', 'k', 'l', 'n', 'o', 'p', 'q', 'v'), array('a', 'e', 'h', 'k', 'n', 'p', 'q', 'r', 't', 'x', 'y'), array('j', 'u', 'v', 'm', 'g', 'l', 'f', 'z', 'a', 'd', 'n', 'b', 'h', 'q', 'c', 'y', 'w'), array('g', 'j', 'r', 'u'), array('c', 'd', 'e', 'h', 'i', 'k', 'l', 'o', 'p', 'q', 'r', 'u', 'v', 'y'), array('y', 't', 'u', 'n', 'f', 'z', 'r', 'c', 'i', 'v', 'o', 'j', 'g', 'p', 'b', 'h', 'w', 'm', 'e', 'k', 'l', 's', 'd', 'q'), array('d', 'f', 'g', 'i', 'j', 'o', 'q', 'u', 'x'), array('a', 'b', 'c', 'e', 'f', 'g', 'h', 'i', 'k', 'l', 'p', 'q', 't', 'u', 'x'), array('p', 'u', 'r', 'y', 'v', 'l', 'b', 'n', 'z', 'k', 'q', 'm', 'a', 't', 'e', 'c'), array('k', 'j', 'x', 'v', 'p', 'd', 't', 'f', 'l', 's', 'c', 'z', 'b'), array('j', 'm', 't', 'e', 'v', 'u', 'k', 'w', 'c', 'b', 'q', 'y', 'o', 'i', 'p', 'h', 'g', 'z', 'd', 's', 'f', 'l', 'r', 'n'), array('a', 'r', 's'), array('n', 'd', 'e', 'g', 'r', 's', 'o', 'm', 'j', 'v', 't', 'x', 'z', 'a', 'i', 'y', 'f', 'k', 'h'), array('b', 'x', 'k', 'j', 'o', 'g', 'm', 'c', 'i', 'q', 'r', 'h', 'e', 'p', 'w', 'a', 'd', 'v', 'z'), array('x', 'o', 'l', 'z', 'j', 'h', 'y', 'g', 'k', 'u', 'p', 's', 'v', 'q', 'm', 'a', 'n', 'i', 't', 'b', 'e', 'd', 'c', 'f', 'r'), array('h', 'i', 'm', 'o', 'x', 'y'), array('m', 'y', 'n', 'p', 'l', 'e', 'o', 't', 'r', 'q', 'v', 'd', 'z'), array('a', 'c', 'd', 'f', 'g', 'h', 'i', 'j', 'k', 'o', 'p', 'q', 's', 'u', 'w', 'x'), array('a', 'd', 'h', 'i', 'j', 'l', 'm', 'n', 'o', 'r', 's', 'w', 'x', 'y'), array('f', 'k', 'l', 'y', 'p', 'e', 'n', 'j', 'v', 's', 't', 'm', 'd', 'x', 'z', 'r', 'i'), array('e', 'y', 'v', 't', 'x', 'r', 'k', 'n', 'p', 'm', 'i', 'z', 'j', 'l', 'c', 'g'), array('x', 'z', 'r', 'c', 'n', 'q', 'h', 'j', 'a', 'l'), array('p', 'z', 'x', 'm', 'r'), array('s', 'r', 'x', 'l', 'b', 'e', 't', 'g', 'y', 'w', 'm', 'z', 'o', 'a', 'v', 'i', 'q', 'd', 'p', 'c'), array('s', 'g', 'u', 'd', 'n', 'm', 't', 'a', 'y', 'x', 'w', 'v', 'r', 'l', 'f', 'z', 'q', 'j', 'k', 'e', 'p', 'h', 'b'), array('h', 'y', 'j', 'o', 'k', 'z', 'e', 'a', 'r', 'f', 'c'), array('z', 'm', 's'), array('c', 'd', 'e', 'f', 'g', 'j', 'l', 'm', 'o', 'q', 'r', 's', 't', 'u', 'v'), array('w', 'c', 'q', 'j', 'i', 'b', 'v', 'a', 'k', 'z', 'm', 'g', 'n', 'l', 's', 'd', 'u', 't', 'e', 'f', 'r', 'x'), array('a', 'o', 'k', 'd', 'm', 'q', 'p', 'e', 'r', 's', 'z', 'y'), array('a', 'c', 'f', 'g', 'h', 'j', 'n', 'q', 'r', 'u', 'x'), array('x', 'z', 'd', 's', 'f', 'i', 'e', 'h', 'c', 'j'), array('h', 'i', 'y'), array('z', 's', 'm', 'i', 'o', 'c', 'd', 'j', 'f', 't', 'p', 'h', 'q', 'x', 'r', 'e', 'k'), array('t', 'b', 'f', 'q', 'c', 'i', 'd', 'v', 'm', 'k', 'n', 's', 'e', 'p', 'x', 'h', 'z', 'y', 'r'), array('c', 'j', 'l', 'o', 'r'), array('z', 'd'), array('z', 'o', 'e', 'y', 'b', 'd', 'l', 'w', 'v'), array('k', 'd', 'z', 'o', 'm', 'c', 'i', 'n', 'p', 'x', 'e', 'y', 'w', 'b', 'f', 'h', 'r', 'g', 'q', 's', 'v'), array('k', 's'), array('o', 'g', 'z', 'b', 'm', 'n', 'u', 'w', 'q', 's', 't', 'i', 'c', 'h', 'd', 'x', 'f', 'y', 'a', 'k'), array('a', 'b', 'c', 'd', 'e', 'f', 'h', 'i', 'j', 'k', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x'), array('a', 'x', 't', 'm', 'l', 'i', 'v', 'd', 'y', 'n', 'q', 'o', 'h', 's', 'c', 'b', 'g', 'u', 'z', 'e', 'k', 'j', 'p'), array('j', 'k', 'p', 'z', 'u', 'q', 'n', 't', 'm', 'f', 'i', 'h', 'l', 'c', 'x', 'a', 'e', 'v', 'b', 's', 'r', 'y', 'w', 'd', 'g', 'o'), array('l', 'm', 'v', 'y'), array('c', 'f', 'h', 'i', 'j', 'n', 'p', 'q', 's', 't', 'w', 'x'), array('d', 'm', 's'), array('h', 'w', 'x', 'u', 'e', 'r', 'd', 'i', 'k', 'j', 'a', 'l', 'g', 'y', 'b', 'f', 't', 'n', 'o', 's', 'q', 'z', 'm', 'v'), array('f', 'y'), array('d', 'f', 'i', 'p', 'u'), array('a', 'b', 'j', 'm', 'r'), array('n', 'h', 'd', 'z', 'e', 'v', 'x', 'j', 'f', 'b', 'q', 'r', 'y', 'c', 'w', 'k', 'm'), array('x', 'y', 'q', 'h', 'w', 'r', 's', 'g', 'j', 'z', 'e', 'v', 'u', 'p'), array('h', 'w', 'm', 'l', 'f', 'p', 'a', 'v', 'b', 'q', 'g', 'y', 'c', 'u', 'r', 't', 'j', 'o', 'k', 'z', 'x', 'n', 'e', 'i', 's'), array('i', 'u'), ); $times = array( 'a' => 29, 'b' => 11, 'c' => 33, 'd' => 84, 'e' => 46, 'f' => 67, 'g' => 19, 'h' => 18, 'i' => 88, 'j' => 8, 'k' => 54, 'l' => 86, 'm' => 88, 'n' => 29, 'o' => 96, 'p' => 1, 'q' => 4, 'r' => 100, 's' => 89, 't' => 44, 'u' => 53, 'v' => 68, 'w' => 12, 'x' => 54, 'y' => 23, 'z' => 78, );</code>
<code>/** * 算法思路: * 1.先排重 * 2.排重之后的操作见注释 */ $newData = array(); foreach ($data as $oldKey => $item){ //去重后 $removeDuplicatedItem = array_unique($item,SORT_NATURAL); $newData[$oldKey] = $removeDuplicatedItem; } uasort($newData,function($a,$b){ $countForA = count($a); $countForB = count($b); if ($countForA == $countForB){ return 0; } return ($countForA > $countForB ? 1 : -1); }); $counter = 0; $targetData = array(); foreach ($newData as $targetKey =>$newItem){ $targetData[$targetKey] = $newItem; $counter++; if($counter == 20){ break; } } $endMemoryUsage = memory_get_usage(); $endTimeStamp = microtime(true); $totalMemoryUsage = $endMemoryUsage - $startMemoryUsage; $totalTimeStamp = $endTimeStamp - $startTimeStamp; print_r($totalMemoryUsage); echo "\n"; print_r($totalTimeStamp); echo "\n"; print_r($targetData);</code>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
