の罰金で、解決策は次のようになります。
<?php
// define lists (note the array_flip() calls)
$list = array();
$list[] = array_flip(array("hello", "hi", "bye", "lol", "rofl", "p1", "p2", "etc"));
$list[] = array_flip(array("hello", "hi", "bye", "lol", "lmao", "c1", "apple", "mango"));
$list[] = array_flip(array("hello", "hi", "bye", "lol", "rofl", "p1", "p2", "etc", "mango", "chair", "table"));
$list[] = array_flip(array("hello", "hi", "bye", "lol", "rofl", "p1", "p2", "etc", "bus", "mango", "apple", "etc"));
// create a union of all lists where each member is present only once
$keys = array();
for ($idx = 0; $idx<count($list); $idx++) {
$keys = $keys+$list[$idx];
}
// iterate over the meta-list
foreach (array_keys($keys) as $key) {
$found = array();
for ($idx = 0; $idx<count($list); $idx++) {
if (isset($list[$idx][$key]))
$found[] = $idx;
}
printf('%s found in lists %s'.PHP_EOL, $key, join(', ', $found));
}
?>
出力:
hello found in lists 0, 1, 2, 3
hi found in lists 0, 1, 2, 3
bye found in lists 0, 1, 2, 3
lol found in lists 0, 1, 2, 3
rofl found in lists 0, 2, 3
p1 found in lists 0, 2, 3
p2 found in lists 0, 2, 3
etc found in lists 0, 2, 3
lmao found in lists 1
c1 found in lists 1
apple found in lists 1, 3
mango found in lists 1, 2, 3
chair found in lists 2
table found in lists 2
bus found in lists 3
Demo。コードの複雑さを軽減するために、PHPに対する最適化がいくつかあります。
まず、すべてのキーワードリストを大きなリストに入れます。 array_flip()を使用して、キーワードではなくキーワードとしてキーワードを使用します。
次に、すべてのキーワードのリストは、単に配列を追加することによって作成されます(重複したキーの上書き、この場合、意図した動作です)。
次に、すべてのキーワードのリストを繰り返します。すべてのキーワードがカウントされ、見つかったリストの配列が表示されます。
私はC++を使用していません。任意のアルゴリズムは私を助けることができる – Nitesh