Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- class LongestAlphabeticalWord
- {
- static void Main()
- {
- string w = Console.ReadLine();
- int n = int.Parse(Console.ReadLine());
- char[] letters = w.ToCharArray();
- char[][] block = new char[n][];
- int k = 0;
- for (int i = 0; i < n; i++)
- {
- block[i] = new char[n]; // Create inner array
- for (int j = 0; j < n; j++)
- {
- if (k == letters.Length)
- {
- k = 0;
- }
- block[i][j] = letters[k];
- k++;
- }
- }
- List<string> all = new List<string>();
- string word = "";
- char next;
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- {
- if(word ==""){
- word = block[i][j].ToString();
- continue;
- }
- next = block[i][j];
- char last = word[word.Length - 1];
- if (((int)last) < ((int)next))
- {
- word += next.ToString();
- }
- else
- {
- all.Add(word);
- word = next.ToString(); ;
- }
- if (j == n - 1)
- {
- all.Add(word);
- word = "";
- continue;
- }
- }
- }
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- {
- if (word == "")
- {
- word = block[j][i].ToString();
- continue;
- }
- next = block[j][i];
- char last = word[word.Length - 1];
- if (((int)last) < ((int)next))
- {
- word += next.ToString();
- }
- else
- {
- all.Add(word);
- word = next.ToString(); ;
- }
- if (j == n - 1)
- {
- all.Add(word);
- word = "";
- continue;
- }
- }
- }
- for (int i = n-1; i >= 0; i--)
- {
- for (int j = n - 1; j >= 0; j--)
- {
- if (word == "")
- {
- word = block[i][j].ToString();
- continue;
- }
- next = block[i][j];
- char last = word[word.Length - 1];
- if (((int)last) < ((int)next))
- {
- word += next.ToString();
- }
- else
- {
- all.Add(word);
- word = next.ToString(); ;
- }
- if (j == 0)
- {
- all.Add(word);
- word = "";
- continue;
- }
- }
- }
- for (int i = n - 1; i >= 0; i--)
- {
- for (int j = n - 1; j >= 0; j--)
- {
- if (word == "")
- {
- word = block[j][i].ToString();
- continue;
- }
- next = block[j][i];
- char last = word[word.Length - 1];
- if (((int)last) < ((int)next))
- {
- word += next.ToString();
- }
- else
- {
- all.Add(word);
- word = next.ToString(); ;
- }
- if (j == 0)
- {
- all.Add(word);
- word = "";
- continue;
- }
- }
- }
- int longest = 0;
- foreach(string value in all){
- if (value.Length > longest)
- {
- longest = value.Length;
- }
- }
- List<string> longest_arr = new List<string>();
- foreach (string wo in all)
- {
- if (wo.Length == longest)
- {
- string value = wo;
- int index = longest_arr.IndexOf(value);
- if (index > -1)
- {
- }
- else
- {
- longest_arr.Add(wo);
- }
- }
- }
- var result = longest_arr[0];
- foreach (string res in longest_arr)
- {
- if (String.Compare(res, result) < 0)
- {
- result = res;
- }
- }
- Console.Write(result);
- Console.ReadLine();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment