Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace LongestAlphabeticalWordVar
- {
- class Program
- {
- static void Main()
- {
- string w = Console.ReadLine();//"softwareuniversity";
- int n = int.Parse(Console.ReadLine());// 7;
- List<string> words = new List<string>();
- int pos = 0;
- for (int i = 0; i < n; i++)
- {
- string row = "";
- string col = "";
- for (int k = 0; k < n; k++)
- {
- row += w[pos % w.Length];
- col += w[(k * n + i) % w.Length];
- pos++;
- }
- words.Add(row);
- words.Add(col);
- char[] chars1 = row.ToCharArray();
- Array.Reverse(chars1);
- words.Add(new String(chars1));
- char[] chars2 = col.ToCharArray();
- Array.Reverse(chars2);
- words.Add(new String(chars2));
- }
- List<string> possibleMaximums = new List<string>();
- foreach (var item in words)
- {
- for (int k = 0; k < item.Length; k++)
- {
- string possibleMax = item[k].ToString();
- pos = k;
- while (pos + 1 < n && item[pos] < item[pos + 1])
- {
- possibleMax += item[pos + 1];
- pos++;
- }
- possibleMaximums.Add(possibleMax);
- }
- }
- string possibleBest = "";
- foreach (var item in possibleMaximums)
- {
- if (item.Length > possibleBest.Length)
- {
- possibleBest = item;
- }
- }
- List<string> allMaximums = new List<string>();
- foreach (var item in possibleMaximums)
- {
- if (possibleBest.Length == item.Length)
- {
- allMaximums.Add(item);
- }
- }
- allMaximums.Sort();
- Console.WriteLine(allMaximums[0]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement