
1、问题背景
给定一个键值对字典,键是网页名称,值是网页内容。网页内容由其他网页名称组成,这些网页名称用空格分隔。目标是对于给定的网页名称,找到从首页到该网页的所有路径。
例如,给定以下字典:
{
'section-a.html': {'contents': 'section-b.html section-c.html section-d.html'},
'section-b.html': {'contents': 'section-d.html section-e.html'},
'section-c.html': {'contents': 'product-a.html product-b.html product-c.html product-d.html'},
'section-d.html': {'contents': 'product-a.html product-c.html'},
'section-e.html': {'contents': 'product-b.html product-d.html'},
'product-a.html': {'contents': ''},
'product-b.html': {'contents': ''},
'product-c.html': {'contents': ''},
'product-d.html': {'contents': ''}
}对于给定的网页名称 'product-d.html',应找到以下路径:
'section-a.html > section-b.html > section-e.html > product-d.html''section-a.html > section-c.html > product-d.html''section-a.html > section-d.html > product-c.html > product-d.html'
该软件是以ecshop作为核心的仿制万表网的商场网站源码。万表网模板 2015最新版整体简洁大气,功能实用,是一款时尚典雅的综合类模板!样式精美的商品分类树,层次分明,分类结构一目了然。首页轮播主广告分别对应切换小广告,商品宣传更到位。独家特色增加顶级频道页面、品牌页面,以及仿京东对比功能,提升网站档次,让您的网站更加高端大气!并且全站采用div+css布局,兼容性良好,更注重页面细节,增加多种j
0
2、解决方案
为了解决这个问题,可以采用以下步骤:
以下代码实现了上述步骤:
function findPathsToItem(item, pages) {
/**
* Finds all paths from the home page to a given item.
* @param {string} item - The item to find paths to.
* @param {Object} pages - A dictionary of page names to page contents.
* @returns {Array} A list of paths from the home page to the given item.
*/
// Convert the dictionary to a more usable form.
let pageContents = {};
for (let [page, contents] of Object.entries(pages)) {
pageContents[page] = new Set(contents.contents.split(' '));
}
<p>// Build a parent page dictionary.
let parentPages = {};
for (let page in pageContents) {
parentPages[page] = [];
for (let parentPage in pageContents) {
if (pageContents[parentPage].has(page)) {
parentPages[page].push(parentPage);
}
}
}</p><p>// Find all paths from the home page to the given item.
let paths = [];
let partialPaths = [[item]];
while (partialPaths.length > 0) {
let path = partialPaths.pop();
if (parentPages[path[path.length - 1]].length > 0) {
// Add as many partial paths as open from here.
for (let parentPage of parentPages[path[path.length - 1]]) {
partialPaths.push([...path, parentPage]);
}
} else {
// We've reached the home page.
paths.push(path.reverse());
}
}
return paths;
}</p><p>// Example usage
let pages = {
'section-a.html': {'contents': 'section-b.html section-c.html section-d.html'},
'section-b.html': {'contents': 'section-d.html section-e.html'},
'section-c.html': {'contents': 'product-a.html product-b.html product-c.html product-d.html'},
'section-d.html': {'contents': 'product-a.html product-c.html'},
'section-e.html': {'contents': 'product-b.html product-d.html'},
'product-a.html': {'contents': ''},
'product-b.html': {'contents': ''},
'product-c.html': {'contents': ''},
'product-d.html': {'contents': ''}
};</p><p>let paths = findPathsToItem('product-d.html', pages);
console.log(paths);输出结果:
[ ['section-a.html', 'section-b.html', 'section-e.html', 'product-d.html'], ['section-a.html', 'section-c.html', 'product-d.html'], ['section-a.html', 'section-d.html', 'product-c.html', 'product-d.html'] ]
以上就是利用字典构建层级树的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号