Advertisement
Guest User

AlphabeticalHamal

a guest
Apr 15th, 2014
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.82 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class LongestAlphabeticalWord
  5. {
  6.     static void Main()
  7.     {
  8.         string word = Console.ReadLine();
  9.         int n = int.Parse(Console.ReadLine());
  10.         int longestCount = 1;
  11.         int start = 0;
  12.         char rememberLetter = 'z';
  13.         char dir = 'r';
  14.         int pos = 0;
  15.         string newWord = "";
  16.         for (int i = 0; i < n; i++)
  17.         {
  18.             for (int j = 0; j < n; j++)
  19.             {
  20.                 newWord = newWord + word[pos];
  21.                 if (pos != word.Length - 1)
  22.                 {
  23.                     pos++;
  24.                 }
  25.                 else
  26.                 {
  27.                     pos = 0;
  28.                 }
  29.             }
  30.         }
  31.  
  32.         //right
  33.         for (int i = 0; i < newWord.Length-1; i++)
  34.         {
  35.             if (i%(n-1)==0)
  36.             {
  37.                 continue;
  38.             }
  39.             pos = i;
  40.             int curCount = 1;
  41.             if (newWord[i]<newWord[i+1])
  42.             {
  43.                 curCount++;
  44.                 pos++;
  45.                 if (pos<=newWord.Length-2)
  46.                 {
  47.                     while (newWord[pos] < newWord[pos + 1] && pos % (n - 1) != 0)
  48.                     {
  49.                         curCount++;
  50.                         pos++;
  51.                         if (pos>=newWord.Length-1)
  52.                         {
  53.                             break;
  54.                         }
  55.                     }
  56.                 }
  57.                 if (curCount>longestCount)
  58.                 {
  59.                     longestCount = curCount;
  60.                     start = i;
  61.                     rememberLetter = newWord[i];
  62.                     dir = 'r';
  63.                 }
  64.                 else if (curCount==longestCount)
  65.                 {
  66.                     if (newWord[i]<rememberLetter)
  67.                     {
  68.                       longestCount = curCount;
  69.                       start = i;
  70.                       rememberLetter = newWord[i];
  71.                       dir = 'r';
  72.                     }
  73.                 }
  74.             }
  75.         }
  76.         //left
  77.         for (int i = newWord.Length-1; i >= 1; i--)
  78.         {
  79.             if (i % (n - 1) == 0)
  80.             {
  81.                 continue;
  82.             }
  83.             pos = i;
  84.             int curCount = 1;
  85.             if (newWord[i] < newWord[i - 1])
  86.             {
  87.                 curCount++;
  88.                 pos--;
  89.                 if (pos>=1)
  90.                 {
  91.                     while (newWord[pos] < newWord[pos - 1] && pos % (n - 1) != 0)
  92.                     {
  93.                         curCount++;
  94.                         pos--;
  95.                         if (pos<1)
  96.                         {
  97.                             break;
  98.                         }
  99.                     }
  100.                 }
  101.                 if (curCount > longestCount)
  102.                 {
  103.                     longestCount = curCount;
  104.                     start = i;
  105.                     rememberLetter = newWord[i];
  106.                     dir = 'l';
  107.                 }
  108.                 else if (curCount == longestCount)
  109.                 {
  110.                     if (newWord[i] < rememberLetter)
  111.                     {
  112.                         longestCount = curCount;
  113.                         start = i;
  114.                         rememberLetter = newWord[i];
  115.                         dir = 'l';
  116.                     }
  117.                 }
  118.             }
  119.         }
  120.         //up
  121.  
  122.         //down
  123.         if (dir=='r')
  124.         {
  125.             for (int i = start; i <= start+longestCount-1; i++)
  126.             {
  127.                 Console.Write(newWord[i]);
  128.             }
  129.         }
  130.         else if (dir=='l')
  131.         {
  132.              for (int i = start; i >= start-longestCount+1; i--)
  133.             {
  134.                 Console.Write(newWord[i]);
  135.             }
  136.         }
  137.     }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement