Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.60 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6.  
  7.  
  8.  
  9. public class palindrom
  10. {
  11.     public static void rekurze(int i, int j, int hloubka)
  12.     {
  13.         if (i < j)
  14.         {
  15.             if (hlavni.s[i] == hlavni.s[j])
  16.             {
  17.                 rekurze(i + 1, j - 1, hloubka + 2);
  18.             }
  19.             else if ( (i < (j-1)) && (hlavni.s[i] == hlavni.s[j-1]) && (hlavni.s[i+1]!=hlavni.s[j]) )
  20.             {
  21.                 rekurze(i + 1, j - 2, hloubka + 2);
  22.             }
  23.             else
  24.             {
  25.                 int index = i;
  26.                 while ((index < j) && (hloubka + (j - index + 1) > hlavni.max))
  27.                 {
  28.                     int index2 = j;
  29.                     while ((index2 > index) && (hlavni.s[index] != hlavni.s[index2]) && (hloubka + (index2 - index + 1) > hlavni.max))
  30.                     {
  31.                         index2 = index2 - 1;
  32.                     }
  33.                     if (index == index2)
  34.                     {
  35.                         if ((hloubka + 1) > hlavni.max)
  36.                         {
  37.                             hlavni.max = (hloubka + 1);
  38.                         }
  39.                     }
  40.                     else
  41.                     {
  42.                         if (hlavni.s[index] == hlavni.s[index2])
  43.                         {
  44.                             rekurze(index + 1, index2 - 1, hloubka + 2);
  45.                         }
  46.                     }
  47.                     index = index + 1;
  48.                 }
  49.             }
  50.         }
  51.         else if (i == j)
  52.         {
  53.             if ((hloubka + 1) > hlavni.max)
  54.             {
  55.                 hlavni.max = (hloubka + 1);
  56.             }
  57.         }
  58.         else
  59.         {
  60.             if ((hloubka) > hlavni.max)
  61.             {
  62.                 hlavni.max = (hloubka);
  63.             }
  64.         }
  65.     }
  66. }
  67.  
  68.  
  69. class hlavni
  70. {
  71.     public static int max = 0;
  72.     public static string s = "";
  73.     public static void Main()
  74.     {
  75.         int i = 0;
  76.         int j = 0;
  77.         try
  78.         {
  79.             using (StreamReader sr = new StreamReader("vstup.txt"))
  80.             {
  81.                 String line;
  82.                 while ((line = sr.ReadLine()) != null)
  83.                 {
  84.                     s = line;
  85.                 }
  86.             }
  87.         }
  88.         catch (Exception e)
  89.         {
  90.             Console.WriteLine("The file could not be read:");
  91.             Console.WriteLine(e.Message);
  92.         }
  93.         j = s.Length - 1;
  94.  
  95.         palindrom.rekurze(i, j, 0);
  96.         Console.WriteLine(max);
  97.         Console.ReadLine();
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement