Advertisement
Guest User

Annonymos Vox

a guest
Feb 29th, 2020
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.56 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _23._02._2020
  8. {
  9.     class Program
  10.     {
  11.  
  12.         static bool isLetter(char a)
  13.         {
  14.             return (a >= 'A' && a <= 'Z') || (a >= 'a' && a <= 'z');
  15.         }
  16.  
  17.         static Dictionary<int, string> getStrings(string text)
  18.         {
  19.             string temp = "";
  20.             int startPosition = -1;
  21.             Dictionary<int, string> strings = new Dictionary<int, string>();
  22.             for (int i = 0; i < text.Length; i++)
  23.             {
  24.                 if (isLetter(text[i]))
  25.                 {
  26.                     temp += text[i];
  27.                     if (startPosition < 0) startPosition = i;
  28.                 }
  29.                 else
  30.                 {
  31.                     if (temp != "") strings.Add(startPosition, temp);
  32.                     temp = "";
  33.                     startPosition = -1;
  34.                 }
  35.             }
  36.             if (temp != "") strings.Add(startPosition, temp);
  37.  
  38.             return strings;
  39.         }
  40.  
  41.         static void Main(string[] args)
  42.         {
  43.             string text = Console.ReadLine();
  44.             string temp = "";
  45.             Dictionary<int, string> strings = new Dictionary<int, string>();
  46.             Dictionary<int, string> sortedStrings = new Dictionary<int, string>();
  47.             Dictionary<int, string> seqStr = new Dictionary<int, string>();
  48.             Dictionary<int, int> startEnds = new Dictionary<int, int>();
  49.  
  50.             strings = getStrings(text);
  51.  
  52.             foreach (var pair in strings.OrderBy(key => key.Value))
  53.             {
  54.                 sortedStrings.Add(pair.Key, pair.Value);
  55.                 //Console.WriteLine(pair.Key + " " + pair.Value);
  56.             }
  57.  
  58.  
  59.             for (int i = 0; i < sortedStrings.Count - 1; i++)
  60.             {
  61.                 KeyValuePair<int, string> pair = new KeyValuePair<int, string>(sortedStrings.Keys.ElementAt(i), sortedStrings.Values.ElementAt(i));
  62.                 var nextPair = new KeyValuePair<int, string>(sortedStrings.Keys.ElementAt(i + 1), sortedStrings.Values.ElementAt(i + 1));
  63.  
  64.                 if (pair.Value == nextPair.Value)
  65.                 {
  66.                     if (!seqStr.ContainsKey(pair.Key)) seqStr.Add(pair.Key, pair.Value);
  67.                     if (!seqStr.ContainsKey(nextPair.Key)) seqStr.Add(nextPair.Key, nextPair.Value);
  68.                 }
  69.             }
  70.             int lastindex = -1;
  71.             for (int i = 0; i < seqStr.Count - 1; i++)
  72.             {
  73.                 KeyValuePair<int, string> pair = new KeyValuePair<int, string>(seqStr.Keys.ElementAt(i), seqStr.Values.ElementAt(i));
  74.                 KeyValuePair<int, string> nextPair = new KeyValuePair<int, string>(seqStr.Keys.ElementAt(i + 1), seqStr.Values.ElementAt(i + 1));
  75.                 if (lastindex < 0) lastindex = pair.Key;
  76.                 if (pair.Value != nextPair.Value)
  77.                 {
  78.                     Console.WriteLine(pair.Value + " - " + nextPair.Value);
  79.                     startEnds.Add(lastindex, pair.Key);
  80.                     lastindex = -1;
  81.                 }
  82.                 if (i == seqStr.Count - 2)
  83.                 {
  84.                     startEnds.Add(lastindex, nextPair.Key);
  85.                 }
  86.             }
  87.  
  88.  
  89.             foreach (var pair in seqStr)
  90.             {
  91.                 Console.WriteLine(pair.Key + " " + pair.Value);
  92.             }
  93.  
  94.             foreach (var pair in startEnds)
  95.             {
  96.                 Console.WriteLine(pair.Key + " " + pair.Value);
  97.             }
  98.         }
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement