Advertisement
North_Point

02. Worm Ipsum

Aug 14th, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.17 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7.  
  8. namespace _02.Worm_Ipsum
  9. {
  10.     class Program
  11.     {
  12.         static Char MostRepeated(string s)
  13.         {
  14.             int[] count = new int[256];
  15.             int max = 0;
  16.             char result = char.MinValue;
  17.  
  18.             Array.Clear(count, 0, count.Length - 1);
  19.  
  20.             foreach (char @char in s)
  21.             {
  22.                 if (++count[@char] > max)
  23.                 {
  24.                     max = count[@char];
  25.                     result = @char;
  26.                 }
  27.             }
  28.             return result;
  29.         }
  30.         static void Main(string[] args)
  31.         {
  32.             var pattern = @"^[A-Z][^.]*.{1}$";
  33.             var wordPattern = @"[A-Za-z]+";
  34.             while (true)
  35.             {
  36.                 var input = Console.ReadLine();
  37.                 if (input == "Worm Ipsum")
  38.                 {
  39.                     break;
  40.                 }
  41.                 var matches = Regex.Matches(input, pattern);
  42.                 StringBuilder result = new StringBuilder();
  43.  
  44.                 foreach (Match item in matches)
  45.                 {
  46.                     var sentence = item.Value.ToString();
  47.                     var words = Regex.Matches(sentence, wordPattern);
  48.                     foreach (Match word in words)
  49.                     {
  50.                         var wo = word.ToString();
  51.                         if (wo.Distinct().Count() != wo.Count())
  52.                         {
  53.                             char @char = MostRepeated(wo);
  54.                             var re = "";
  55.                             for (int i = 0; i < wo.Length; i++)
  56.                             {
  57.                                 re += @char;
  58.                             }
  59.                             if (sentence.Contains(wo))
  60.                             {
  61.                                 sentence = sentence.Replace(wo , re);
  62.                             }
  63.                         }
  64.                     }
  65.                     Console.WriteLine(sentence.ToString());
  66.                 }
  67.             }
  68.         }
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement