Skip to content

Instantly share code, notes, and snippets.

@quangnle
Created May 11, 2016 12:38
Show Gist options
  • Save quangnle/677f89ef3782cccaa587af93675d1cbf to your computer and use it in GitHub Desktop.
Save quangnle/677f89ef3782cccaa587af93675d1cbf to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Tuenti2
{
class Program
{
class Node
{
public char Content { get; set; }
public int Appearances { get; set; }
public Node Parent { get; set; }
public List<Node> Children { get; set; }
public string ToWord()
{
var current = this;
var word = "";
while (current != null)
{
if (current.Content != ' ')
word = current.Content + word;
current = current.Parent;
}
word += " " + Appearances.ToString();
return word;
}
}
private static Node _first = null;
private static Node _second = null;
private static Node _third = null;
static void Main(string[] args)
{
var corpus = File.ReadAllText("corpus.txt");
var words = corpus.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
var lines = File.ReadAllLines("submitInput.txt");
var n = Convert.ToInt32(lines[0]);
var result = "";
for (int i = 1; i <= n; i++)
{
var firstIndex = Convert.ToInt32(lines[i].Split(' ')[0]) - 1;
var lastIndex = Convert.ToInt32(lines[i].Split(' ')[1]) - 1;
var root = new Node { Content = ' ', Appearances = 0 };
for (int j = firstIndex; j <= lastIndex; j++)
{
AddWord(root, words[j], 0);
}
UpdateRank(root);
result += string.Format("Case #{0}: {1},{2},{3}\n", i, _first.ToWord(), _second.ToWord(), _third.ToWord());
_first = null;
_second = null;
_third = null;
}
File.WriteAllText("submitOutput.txt", result);
}
static void UpdateRank(Node n)
{
if (n.Children == null)
{
if (_first == null) _first = n;
else
{
if (_first.Appearances < n.Appearances)
{
_third = _second;
_second = _first;
_first = n;
}
else
{
if (_second == null)
_second = n;
else
{
if (_second.Appearances < n.Appearances)
{
_third = _second;
_second = n;
}
else
{
if (_third == null || _third.Appearances < n.Appearances)
_third = n;
}
}
}
}
}
else
{
foreach (var child in n.Children)
{
UpdateRank(child);
}
}
}
static void AddWord(Node node, string word, int pointer)
{
if (pointer >= word.Length) return;
else
{
if (node.Children == null)
{
var newNode = new Node { Content = word[pointer], Appearances = (pointer == word.Length - 1) ? 1 : 0, Parent = node };
node.Children = new List<Node>();
node.Children.Add(newNode);
pointer++;
AddWord(newNode, word, pointer);
}
else
{
Node foundChild = null;
foreach (var child in node.Children)
{
if (child.Content == word[pointer])
{
foundChild = child;
break;
}
}
if (foundChild != null)
{
if (pointer == word.Length - 1)
foundChild.Appearances++;
else
{
pointer++;
AddWord(foundChild, word, pointer);
}
}
else
{
var newNode = new Node { Content = word[pointer], Appearances = (pointer == word.Length - 1) ? 1 : 0, Parent = node };
node.Children.Add(newNode);
pointer++;
AddWord(newNode, word, pointer);
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment