Advertisement
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.IO;
- public class palindrom
- {
- public static void rekurze(int i, int j, int hloubka)
- {
- if (i < j)
- {
- if (hlavni.s[i] == hlavni.s[j])
- {
- rekurze(i + 1, j - 1, hloubka + 2);
- }
- else if ( (i < (j-1)) && (hlavni.s[i] == hlavni.s[j-1]) && (hlavni.s[i+1]!=hlavni.s[j]) )
- {
- rekurze(i + 1, j - 2, hloubka + 2);
- }
- else
- {
- int index = i;
- while ((index < j) && (hloubka + (j - index + 1) > hlavni.max))
- {
- int index2 = j;
- while ((index2 > index) && (hlavni.s[index] != hlavni.s[index2]) && (hloubka + (index2 - index + 1) > hlavni.max))
- {
- index2 = index2 - 1;
- }
- if (index == index2)
- {
- if ((hloubka + 1) > hlavni.max)
- {
- hlavni.max = (hloubka + 1);
- }
- }
- else
- {
- if (hlavni.s[index] == hlavni.s[index2])
- {
- rekurze(index + 1, index2 - 1, hloubka + 2);
- }
- }
- index = index + 1;
- }
- }
- }
- else if (i == j)
- {
- if ((hloubka + 1) > hlavni.max)
- {
- hlavni.max = (hloubka + 1);
- }
- }
- else
- {
- if ((hloubka) > hlavni.max)
- {
- hlavni.max = (hloubka);
- }
- }
- }
- }
- class hlavni
- {
- public static int max = 0;
- public static string s = "";
- public static void Main()
- {
- int i = 0;
- int j = 0;
- try
- {
- using (StreamReader sr = new StreamReader("vstup.txt"))
- {
- String line;
- while ((line = sr.ReadLine()) != null)
- {
- s = line;
- }
- }
- }
- catch (Exception e)
- {
- Console.WriteLine("The file could not be read:");
- Console.WriteLine(e.Message);
- }
- j = s.Length - 1;
- palindrom.rekurze(i, j, 0);
- Console.WriteLine(max);
- Console.ReadLine();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement