-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
77 lines (76 loc) · 3.23 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using System;
using System.Collections;
namespace _2
{
class Program
{
static string Check(string Palindrom, int RightIndex,int LeftIndex)
{
try // Extends the length of the palindrome if the edges have the same characters
{
while(Palindrom[LeftIndex - 1]== Palindrom[RightIndex + 1])
{
RightIndex++;
LeftIndex--;
}
}
catch { }
return (Palindrom.Substring(LeftIndex, RightIndex - LeftIndex + 1));
}
static void Main(string[] args)
{
string SourceString = Console.ReadLine();
ArrayList Palindroms = new ArrayList(); // Place of recording of the found palindromes
int MinimalPalindrom = SourceString.Length;
for (int NumberInString = 0; NumberInString <= SourceString.Length - 1; NumberInString++)
{
try //Search for the middle of a palindrome if there is one character in the middle ( *a* )
{
if (SourceString[NumberInString - 1] == SourceString[NumberInString + 1])
{
string Palindrom = Check(SourceString, NumberInString, NumberInString);
if (Palindrom.Length <= MinimalPalindrom)
{
MinimalPalindrom = Palindrom.Length;
Palindroms.Add(Palindrom);
}
}
}
catch { }
try // Find the middle of the palindrome if there are two characters in the middle ( *aa* )
{
if (SourceString[NumberInString] == SourceString[NumberInString + 1])
{
string Palindrom = Check(SourceString, NumberInString, NumberInString + 1);
if (Palindrom.Length <= MinimalPalindrom)
{
MinimalPalindrom = Palindrom.Length;
Palindroms.Add(Palindrom);
}
}
}
catch { }
}
if (Palindroms.Count != 0) //The definition of the smallest palindrome
{
string palindrom = "";
for (int NumberInList = 0; NumberInList < Palindroms.Count; NumberInList++)
{
if (palindrom == "")
{
if (Palindroms[NumberInList].ToString().Length == MinimalPalindrom)
palindrom = Palindroms[NumberInList].ToString();
}
else
if (Palindroms[NumberInList].ToString().Length == MinimalPalindrom)
if (String.Compare(Palindroms[NumberInList].ToString(), palindrom) == -1)
palindrom = Palindroms[NumberInList].ToString();
}
Console.WriteLine(palindrom);
}
else
Console.WriteLine(-1);
Console.ReadKey();
}
}
}