using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.IO;public class TestReadFile
{
    public static void Main(String[] args)
    {        FileStream fs = new FileStream(@"C:\\Documents and Settings\\nzhang\\Desktop\\1.txt", FileMode.Open, FileAccess.Read);
        StreamReader sr = new StreamReader(fs);        Dictionary<string, int> dictsource = new Dictionary<string, int>();
        String line = sr.ReadLine();
        string[] separetor = new string[] { "\t", "\t" };
        while (line != null)
        {
            String[] lines;
            lines = line.Split(separetor, StringSplitOptions.RemoveEmptyEntries);
            if (lines.Length == 3)
            {
                if (!dictsource.ContainsKey(lines[1]))
                {
                    int curValue;
                    if (int.TryParse(lines[2], out curValue))
                    {
                        dictsource.Add(lines[1], curValue);
                    }
                }
                else
                {
                    int curValue;
                    if (int.TryParse(lines[2], out curValue))
                    {
                        dictsource[lines[1]] += curValue;
                    }
                }
            }
            line = sr.ReadLine();
        }
        foreach (string key in dictsource.Keys)
        {
            //Console.WriteLine("{0}--->{1}", key, dictsource[key]);
        }
        Dictionary<string, int> dict = new Dictionary<string, int>();
        Dictionary<string, int> dict1 = new Dictionary<string, int>();
        process1(dict, dictsource);
        process2(dict1, dictsource,dict);
        Console.ReadKey();
        sr.Close();
        fs.Close();
    }
    public static void process1(Dictionary<string, int> dict, Dictionary<string, int> dictsource)
    {
        String[] separetor = new string[] { " " };
        foreach (string key in dictsource.Keys)
        {
            string[] line;
            line = key.Split(separetor, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < line.Length; i++)
            {
                string s1 = "";
                for (int j = 0; j < line.Length - i; j++)
                {
                    if (j == 0)
                    {
                        s1 = line[i];
                    }
                    else
                    {
                        s1 = s1 + " " + line[j + i];
                    }
                    if (!dict.ContainsKey(s1))
                    {
                        dict.Add(s1, dictsource[key]);
                    }
                    else
                    {
                        dict[s1] += dictsource[key];
                    }
                }
            }        }
        foreach (string key in dict.Keys)
        {
            //Console.WriteLine("{0}--->{1}", key, dict[key]);
        }
    }
    public static void process2(Dictionary<string, int> dict1, Dictionary<string, int> dictsource,Dictionary<string, int> dict)
    {
        String[] separetor = new string[] { " " };
        foreach (string key in dictsource.Keys)
        {
            string[] line;
            string s1="";
            line = key.Split(separetor, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < line.Length; i++)
            {
                if (line.Length==1)
                {
                    s1 = line[0];
                }
                if (dict[line[0]]>4*dict[line[0]+" "+line[1]]&&line.Length>1)

                {
                    s1 = line[0];
                }
                else if (dict[line[0] + " " + line[1]] > 2 * dict[line[0] + " " + line[1] + " " + line[2]] && line.Length > 2)
                {
                    s1 = line[0] + " " + line[1];
                }
                else if (dict[line[0] + " " + line[1] + " " + line[2]] > 2 * dict[line[0] + " " + line[1] + " " + line[2] + " " + line[3]] && line.Length >3)
                {
                    s1 = line[0] + " " + line[1] + " " + line[2];
                }
                else 
                {
                    s1=line[0]+" "+line[1]+" "+line[2]+" "+line[3];
                }
                if (!dict1.ContainsKey(s1))
                {
                   dict1.Add(s1, dict[s1]);
                }
                else
                {
                   dict1[s1] += dict[s1];
                }
                
            }        }
        foreach (string key in dict1.Keys)
        {
            Console.WriteLine("{0}--->{1}", key, dict[key]);
        }
    }
}