115 lines
2.6 KiB
C#
115 lines
2.6 KiB
C#
namespace ConsoleApp1;
|
|
|
|
public class Node
|
|
{
|
|
public string spot;
|
|
public List<Node> son;
|
|
public string session;
|
|
public string title;
|
|
public bool isOverride;
|
|
}
|
|
|
|
public class EpisodeGroup
|
|
{
|
|
private Node _root;
|
|
|
|
public readonly List<EpisodeInfo> episodes = new List<EpisodeInfo>();
|
|
|
|
private Node FindOrCreateShow(Node node, string spot)
|
|
{
|
|
if (node.son == null)
|
|
{
|
|
node.son = new List<Node>();
|
|
}
|
|
|
|
Node target = null;
|
|
foreach (var son in node.son)
|
|
{
|
|
if (son.spot == spot)
|
|
{
|
|
target = son;
|
|
break;
|
|
}
|
|
}
|
|
if (target == null)
|
|
{
|
|
target = new Node
|
|
{
|
|
spot = spot,
|
|
isOverride = false
|
|
};
|
|
node.son.Add(target);
|
|
}
|
|
return target;
|
|
}
|
|
|
|
private void Add(EpisodeInfo episode)
|
|
{
|
|
if (_root == null)
|
|
{
|
|
_root = new Node
|
|
{
|
|
spot = "",
|
|
isOverride = false
|
|
};
|
|
}
|
|
|
|
var curr = _root;
|
|
var spots = episode.path.Split('/');
|
|
foreach (var spot in spots)
|
|
{
|
|
curr = FindOrCreateShow(curr, spot);
|
|
}
|
|
curr.session = episode.session;
|
|
curr.title = episode.name;
|
|
curr.isOverride = true;
|
|
}
|
|
|
|
public void Run()
|
|
{
|
|
_root = null;
|
|
foreach (var episode in episodes)
|
|
{
|
|
Add(episode);
|
|
}
|
|
|
|
DoRun(_root);
|
|
|
|
foreach (var episode in episodes)
|
|
{
|
|
var curr = _root;
|
|
var spots = episode.path.Split('/');
|
|
foreach (var spot in spots)
|
|
{
|
|
curr = FindOrCreateShow(curr, spot);
|
|
if (curr.isOverride)
|
|
{
|
|
episode.name = curr.title;
|
|
episode.session = curr.session;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void DoRun(Node node)
|
|
{
|
|
if (node == null) return;
|
|
if (node.son == null) return;
|
|
foreach (var son in node.son)
|
|
{
|
|
DoRun(son);
|
|
}
|
|
var query = (from son in node.son
|
|
where son.isOverride
|
|
select son).GroupBy(node => (node.title, node.session));
|
|
foreach (var group in query)
|
|
{
|
|
if (group.Count() * 2 > node.son.Count)
|
|
{
|
|
node.isOverride = true;
|
|
(node.title, node.session) = group.Key;
|
|
}
|
|
}
|
|
}
|
|
} |