-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlongest-palindrome-in-a-string.cpp
More file actions
76 lines (67 loc) · 1.54 KB
/
longest-palindrome-in-a-string.cpp
File metadata and controls
76 lines (67 loc) · 1.54 KB
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
#define lli long long int
#define mod 1000000007
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin>>t;
while(t--)
{
// aaaabbaa
string s;
cin>>s;
string res = "";
string ans = "";
int p,q;
int n = s.size();
if(n == 1)
cout<<s[0]<<"\n";
if(n == 2)
if(s[0] == s[1])
cout<<s<<"\n";
else
cout<<s[0]<<"\n";
if(n == 1 || n == 2)
continue;
for (int i = 0; i < s.size(); ++i)
{
for (int k = 0; k < 2; ++k)
{
ans = "";
bool f = 0;
if(k == 0)
{
p = i - 1, q = i + 1;
if(p >= 0 && q < n)
ans += s[i], f = 1;
}
else if(s[i] == s[i+1])
{
p = i - 1, q = i + 2;
ans = ans + s[i];
ans += s[i];
if(p >= 0 && q < n)
{
f = 1;
}
}
while(p >= 0 && q < n && s[p] == s[q] && f)
{
ans = s[p] + ans + s[q];
p--,q++;
}
if(ans.size() > res.size())
res = ans;
// cout<<ans<<" "<<res<<"\n";
}
}
if(res.size() == 1)
cout<<s[0]<<"\n";
else
cout<<res<<"\n";
}
return 0;
}