Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- class LongestAlphabeticalWord
- {
- static void Main()
- {
- string word = Console.ReadLine();
- int n = int.Parse(Console.ReadLine());
- int longestCount = 1;
- int start = 0;
- char rememberLetter = 'z';
- char dir = 'r';
- int pos = 0;
- string newWord = "";
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- {
- newWord = newWord + word[pos];
- if (pos != word.Length - 1)
- {
- pos++;
- }
- else
- {
- pos = 0;
- }
- }
- }
- //right
- for (int i = 0; i < newWord.Length-1; i++)
- {
- if (i%(n-1)==0)
- {
- continue;
- }
- pos = i;
- int curCount = 1;
- if (newWord[i]<newWord[i+1])
- {
- curCount++;
- pos++;
- if (pos<=newWord.Length-2)
- {
- while (newWord[pos] < newWord[pos + 1] && pos % (n - 1) != 0)
- {
- curCount++;
- pos++;
- if (pos>=newWord.Length-1)
- {
- break;
- }
- }
- }
- if (curCount>longestCount)
- {
- longestCount = curCount;
- start = i;
- rememberLetter = newWord[i];
- dir = 'r';
- }
- else if (curCount==longestCount)
- {
- if (newWord[i]<rememberLetter)
- {
- longestCount = curCount;
- start = i;
- rememberLetter = newWord[i];
- dir = 'r';
- }
- }
- }
- }
- //left
- for (int i = newWord.Length-1; i >= 1; i--)
- {
- if (i % (n - 1) == 0)
- {
- continue;
- }
- pos = i;
- int curCount = 1;
- if (newWord[i] < newWord[i - 1])
- {
- curCount++;
- pos--;
- if (pos>=1)
- {
- while (newWord[pos] < newWord[pos - 1] && pos % (n - 1) != 0)
- {
- curCount++;
- pos--;
- if (pos<1)
- {
- break;
- }
- }
- }
- if (curCount > longestCount)
- {
- longestCount = curCount;
- start = i;
- rememberLetter = newWord[i];
- dir = 'l';
- }
- else if (curCount == longestCount)
- {
- if (newWord[i] < rememberLetter)
- {
- longestCount = curCount;
- start = i;
- rememberLetter = newWord[i];
- dir = 'l';
- }
- }
- }
- }
- //up
- //down
- if (dir=='r')
- {
- for (int i = start; i <= start+longestCount-1; i++)
- {
- Console.Write(newWord[i]);
- }
- }
- else if (dir=='l')
- {
- for (int i = start; i >= start-longestCount+1; i--)
- {
- Console.Write(newWord[i]);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement