ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1612. Tram Forum

AC Solution C#: Use Linq
Posted by Hikmat Ahmedov 9 Mar 2012 00:12
// ----------SOLVED------------
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{

    static void Main()
    {
       /* Here we include all word separators */
         string[] ss = Console.In.ReadToEnd().Split(new char[] { ' ', '\t', '\n', '\r','.',',','!','?','-',':'},
             StringSplitOptions.RemoveEmptyEntries);

         /*
          From the word list select just "tram" and "trolleybus"
          by Grouping we know how many times these words appear
          by ordering we make sure that "tram" comes first in the list r[]
          if both words appear in the input
          */
           var r = (from u in ss
                     where u == "tram" || u == "trolleybus"
                     group u by u into uu
                     orderby uu.Key
                     select new { key=uu.Key, count=uu.Count() } ).ToList();
        /*
         The rest is just printing the output basing on the results of the query
          */
           if (r.Count == 0) Console.WriteLine("Bus driver");
           else
               if (r.Count == 1) Console.WriteLine("{0}", r[0].key == "tram" ? "Tram driver" : "Trolleybus driver");
               else if (r[0].count > r[1].count) Console.WriteLine("Tram driver"); else
                   if (r[0].count < r[1].count) Console.WriteLine("Trolleybus driver"); else Console.WriteLine("Bus driver");
    }
}