-
Notifications
You must be signed in to change notification settings - Fork 0
/
Level1.java
56 lines (48 loc) · 1 KB
/
Level1.java
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
import java.io.*;
import java.util.*;
public class Level1
{
public static void main(String args[])throws IOException
{
Scanner sc = new Scanner(System.in);
String s = sc.next();
int len = s.length();
int best_len = 0;
int cen = 0;
String long_pali = "";
for(int i = 0; i < (2*len - 1); i ++)
{
int j = i/2;
int c = 1;
if(i % 2 == 0)
{
while(((j - c) >= 0) && ((j + c) < len) && (s.charAt(j - c) == s.charAt(j + c)))
{
c ++;
}
if((c * 2 - 1) > best_len)
{
best_len = c * 2 - 1;
cen = i/2;
}
}
else
{
while((j - c + 1) >= 0 && (j + c) < len && s.charAt(j - c + 1) == s.charAt(j + c))
{
c ++;
}
if(2 * (c - 1) > best_len)
{
best_len = 2 * (c - 1);
cen = i/2;
}
}
}
if(best_len % 2 == 0)
long_pali = s.substring(cen - best_len/2 + 1, cen + best_len/2 + 1);
else
long_pali = s.substring(cen - best_len/2, cen + best_len/2 + 1);
System.out.println(long_pali);
}
}