算法问题:从数据集中按规则取指定数量的数据集合
遇到一个算法问题,一直不得求解,恳请大神指点!
现有数据:
<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>

Heiße KI -Werkzeuge

Undresser.AI Undress
KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover
Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool
Ausziehbilder kostenlos

Clothoff.io
KI-Kleiderentferner

Video Face Swap
Tauschen Sie Gesichter in jedem Video mühelos mit unserem völlig kostenlosen KI-Gesichtstausch-Tool aus!

Heißer Artikel

Heiße Werkzeuge

Notepad++7.3.1
Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version
Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1
Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6
Visuelle Webentwicklungstools

SublimeText3 Mac-Version
Codebearbeitungssoftware auf Gottesniveau (SublimeText3)

Heiße Themen



PHP 8.4 bringt mehrere neue Funktionen, Sicherheitsverbesserungen und Leistungsverbesserungen mit einer beträchtlichen Menge an veralteten und entfernten Funktionen. In dieser Anleitung wird erklärt, wie Sie PHP 8.4 installieren oder auf PHP 8.4 auf Ubuntu, Debian oder deren Derivaten aktualisieren. Obwohl es möglich ist, PHP aus dem Quellcode zu kompilieren, ist die Installation aus einem APT-Repository wie unten erläutert oft schneller und sicherer, da diese Repositorys in Zukunft die neuesten Fehlerbehebungen und Sicherheitsupdates bereitstellen.

Wenn Sie ein erfahrener PHP-Entwickler sind, haben Sie möglicherweise das Gefühl, dass Sie dort waren und dies bereits getan haben. Sie haben eine beträchtliche Anzahl von Anwendungen entwickelt, Millionen von Codezeilen debuggt und eine Reihe von Skripten optimiert, um op zu erreichen

Visual Studio Code, auch bekannt als VS Code, ist ein kostenloser Quellcode-Editor – oder eine integrierte Entwicklungsumgebung (IDE) –, die für alle gängigen Betriebssysteme verfügbar ist. Mit einer großen Sammlung von Erweiterungen für viele Programmiersprachen kann VS Code c

JWT ist ein offener Standard, der auf JSON basiert und zur sicheren Übertragung von Informationen zwischen Parteien verwendet wird, hauptsächlich für die Identitätsauthentifizierung und den Informationsaustausch. 1. JWT besteht aus drei Teilen: Header, Nutzlast und Signatur. 2. Das Arbeitsprinzip von JWT enthält drei Schritte: Generierung von JWT, Überprüfung von JWT und Parsingnayload. 3. Bei Verwendung von JWT zur Authentifizierung in PHP kann JWT generiert und überprüft werden, und die Funktionen und Berechtigungsinformationen der Benutzer können in die erweiterte Verwendung aufgenommen werden. 4. Häufige Fehler sind Signaturüberprüfungsfehler, Token -Ablauf und übergroße Nutzlast. Zu Debugging -Fähigkeiten gehört die Verwendung von Debugging -Tools und Protokollierung. 5. Leistungsoptimierung und Best Practices umfassen die Verwendung geeigneter Signaturalgorithmen, das Einstellen von Gültigkeitsperioden angemessen.

Dieses Tutorial zeigt, wie XML -Dokumente mit PHP effizient verarbeitet werden. XML (Extensible Markup-Sprache) ist eine vielseitige textbasierte Markup-Sprache, die sowohl für die Lesbarkeit des Menschen als auch für die Analyse von Maschinen entwickelt wurde. Es wird üblicherweise für die Datenspeicherung ein verwendet und wird häufig verwendet

Eine Zeichenfolge ist eine Folge von Zeichen, einschließlich Buchstaben, Zahlen und Symbolen. In diesem Tutorial wird lernen, wie Sie die Anzahl der Vokale in einer bestimmten Zeichenfolge in PHP unter Verwendung verschiedener Methoden berechnen. Die Vokale auf Englisch sind a, e, i, o, u und sie können Großbuchstaben oder Kleinbuchstaben sein. Was ist ein Vokal? Vokale sind alphabetische Zeichen, die eine spezifische Aussprache darstellen. Es gibt fünf Vokale in Englisch, einschließlich Großbuchstaben und Kleinbuchstaben: a, e, ich, o, u Beispiel 1 Eingabe: String = "TutorialPoint" Ausgabe: 6 erklären Die Vokale in der String "TutorialPoint" sind u, o, i, a, o, ich. Insgesamt gibt es 6 Yuan

Statische Bindung (statisch: :) implementiert die späte statische Bindung (LSB) in PHP, sodass das Aufrufen von Klassen in statischen Kontexten anstatt Klassen zu definieren. 1) Der Analyseprozess wird zur Laufzeit durchgeführt.

Was sind die magischen Methoden von PHP? Zu den magischen Methoden von PHP gehören: 1. \ _ \ _ Konstrukt, verwendet, um Objekte zu initialisieren; 2. \ _ \ _ Destruct, verwendet zur Reinigung von Ressourcen; 3. \ _ \ _ Call, behandeln Sie nicht existierende Methodenaufrufe; 4. \ _ \ _ GET, Implementieren Sie den dynamischen Attributzugriff; 5. \ _ \ _ Setzen Sie dynamische Attributeinstellungen. Diese Methoden werden in bestimmten Situationen automatisch aufgerufen, wodurch die Code -Flexibilität und -Effizienz verbessert werden.
