/* cs */ /* * Problem : SUBSTR * Contest : UOI-2006 (Dnepropetrovsk) * Type : Solution * Date : March 25, 2006 * Author : Shamil Yagiyayev * Language : C# * Compiler : .NET 1.1 * Algorithm: Simple Solution */ using System; using System.IO; using System.Text; class Substr { static readonly string FILE_IN = "substr.dat"; static readonly string FILE_OUT = "substr.sol"; static string buffer = ""; static int index = 0; static string best = ""; static StreamReader sr = new StreamReader(FILE_IN); static readonly int MAX_SUBSTR = 26 + 1; static char[] currStr = new char[MAX_SUBSTR]; static int fillNext = 0; static char GetChar() { while (buffer!=null && index == buffer.Length) { buffer = sr.ReadLine(); index = 0; } if (buffer == null) return '-'; // means EOF return buffer[index++]; } static void AddChar(char c) { currStr[fillNext] = c; fillNext = (fillNext + 1) % MAX_SUBSTR; } static string GetCurrentWord(int l) { int wordStart = (MAX_SUBSTR + fillNext - l) % MAX_SUBSTR; StringBuilder sb = new StringBuilder(); for (int i=wordStart; i!=fillNext; i = (i+1) % MAX_SUBSTR) sb.Append(currStr[i]); return sb.ToString(); } static char GetBackChar(int n) { return currStr[ (fillNext+MAX_SUBSTR - n) % MAX_SUBSTR]; } static void Process() { int curr = 0; int wordLeng = 0; int currWordLeng = 0; char c = GetChar(); while (c != '-') { AddChar(c); curr++; wordLeng = 1; while (wordLeng<=currWordLeng && c != GetBackChar(wordLeng+1)) wordLeng++; currWordLeng = wordLeng; if (wordLeng > best.Length) best = GetCurrentWord(wordLeng); c = GetChar(); } } static void WriteSolution() { StreamWriter sw = File.CreateText(FILE_OUT); sw.WriteLine(best); sw.Close(); } static void Main() { Process(); WriteSolution(); } }