Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace _03.Problem
- {
- public class Startup
- {
- public static void Main()
- {
- DataSet dataSet = new DataSet();
- dataSet.InitializeCache();
- string inputLine;
- while (!(inputLine = Console.ReadLine()).Equals("thetinggoesskrra"))
- {
- string[] args = SplitString(inputLine);
- if (args.Length == 1)
- {
- string dataSetKey = args[0];
- dataSet.AddDataSetKey(dataSetKey);
- }
- else
- {
- string dataKey = args[0];
- long dataSize = long.Parse(args[1]);
- string dataSetKey = args[2];
- dataSet.AddDataSetValue(dataSetKey, dataKey, dataSize);
- }
- }
- string result = dataSet.GetHighestSumDataSet();
- Console.Write(result);
- }
- private static string[] SplitString(string toSplit)
- {
- return toSplit.Split(new[] { " ", "->", "|" }, StringSplitOptions.RemoveEmptyEntries);
- }
- }
- public class DataSet
- {
- private Dictionary<string, Dictionary<string, long>> data;
- private DataSet cache;
- public DataSet()
- {
- this.data = new Dictionary<string, Dictionary<string, long>>();
- }
- public void InitializeCache()
- {
- this.cache = new DataSet();
- }
- public string GetHighestSumDataSet()
- {
- if (this.data.Count == 0)
- {
- return string.Empty;
- }
- KeyValuePair<string, Dictionary<string, long>> resultSet = this.data
- .OrderByDescending(kvp => kvp.Value.Values.Sum())
- .FirstOrDefault();
- StringBuilder result = new StringBuilder();
- long totalSize = resultSet.Value.Values.Sum();
- string header = $"Data Set: {resultSet.Key}, Total Size: {totalSize}";
- result.AppendLine(header);
- foreach (KeyValuePair<string, long> kvp in resultSet.Value)
- {
- result.AppendLine($"$.{kvp.Key}");
- }
- return result.ToString();
- }
- public void AddDataSetKey(string dataSetKey, bool useCache = true)
- {
- if (!this.data.ContainsKey(dataSetKey))
- {
- this.data[dataSetKey] = new Dictionary<string, long>();
- if (useCache)
- {
- this.ExtractFromCache(dataSetKey);
- }
- }
- }
- public void AddDataSetValue(string dataSetKey, string dataKey, long dataSize, bool useCache = true)
- {
- if (!this.data.ContainsKey(dataSetKey))
- {
- if (useCache)
- {
- this.AddToCache(dataSetKey, dataKey, dataSize);
- }
- }
- else
- {
- this.data[dataSetKey][dataKey] = dataSize;
- }
- }
- private void ExtractFromCache(string dataSetKey)
- {
- if (!this.cache.data.ContainsKey(dataSetKey))
- {
- return;
- }
- foreach (KeyValuePair<string, Dictionary<string, long>> cacheKvp in this.cache.data)
- {
- string cacheDataSetKey = cacheKvp.Key;
- foreach (KeyValuePair<string, long> cacheDataKeyAndValue in cacheKvp.Value)
- {
- string cacheDataKey = cacheDataKeyAndValue.Key;
- long cacheDataSize = cacheDataKeyAndValue.Value;
- this.AddDataSetValue(cacheDataSetKey, cacheDataKey, cacheDataSize, useCache: false);
- }
- }
- this.cache.data.Remove(dataSetKey);
- }
- private void AddToCache(string dataSetKey, string dataKey, long dataSize)
- {
- this.cache.AddDataSetKey(dataSetKey, useCache: false);
- this.cache.AddDataSetValue(dataSetKey, dataKey, dataSize, useCache: false);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement