Advertisement
dim4o

LongestAlphabeticalWordVar

Jul 24th, 2014
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.27 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace LongestAlphabeticalWordVar
  6. {
  7.     class Program
  8.     {
  9.         static void Main()
  10.         {          
  11.             string w = Console.ReadLine();//"softwareuniversity";
  12.             int n = int.Parse(Console.ReadLine());// 7;
  13.             List<string> words = new List<string>();
  14.             int pos = 0;
  15.             for (int i = 0; i < n; i++)
  16.             {
  17.                 string row = "";
  18.                 string col = "";
  19.                 for (int k = 0; k < n; k++)
  20.                 {
  21.                     row += w[pos % w.Length];
  22.                     col += w[(k * n + i) % w.Length];
  23.                     pos++;
  24.                 }
  25.                 words.Add(row);
  26.                 words.Add(col);
  27.                 char[] chars1 = row.ToCharArray();
  28.                 Array.Reverse(chars1);
  29.                 words.Add(new String(chars1));
  30.                 char[] chars2 = col.ToCharArray();
  31.                 Array.Reverse(chars2);
  32.                 words.Add(new String(chars2));
  33.             }
  34.              
  35.             List<string> possibleMaximums = new List<string>();
  36.             foreach (var item in words)
  37.             {
  38.                 for (int k = 0; k < item.Length; k++)
  39.                 {
  40.                     string possibleMax = item[k].ToString();
  41.                     pos = k;
  42.                     while (pos + 1 < n && item[pos] < item[pos + 1])
  43.                     {
  44.                         possibleMax += item[pos + 1];
  45.                         pos++;
  46.                     }
  47.                     possibleMaximums.Add(possibleMax);
  48.                 }
  49.             }
  50.             string possibleBest = "";
  51.             foreach (var item in possibleMaximums)
  52.             {
  53.                 if (item.Length > possibleBest.Length)
  54.                 {
  55.                     possibleBest = item;
  56.                 }
  57.             }
  58.             List<string> allMaximums = new List<string>();
  59.             foreach (var item in possibleMaximums)
  60.             {
  61.                 if (possibleBest.Length == item.Length)
  62.                 {
  63.                     allMaximums.Add(item);
  64.                 }
  65.             }
  66.             allMaximums.Sort();
  67.             Console.WriteLine(allMaximums[0]);
  68.         }
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement