-
Notifications
You must be signed in to change notification settings - Fork 47
/
PTIT_SSAM119D.cpp
58 lines (51 loc) · 1.33 KB
/
PTIT_SSAM119D.cpp
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
#include <iostream>
#include<vector>
#include<string>
using namespace std;
/*Chen vao xau ban dau cac ki tu #, vi du : abba -> #a#b#b#a#*/
void Chenkitu(string &str)
{
string temp;
for (int i=0; i<str.length(); i++)
{
temp.push_back('#');
temp.push_back(str[i]);
}
temp.push_back('#');
str = temp;
return;
}
int Manacher(string &str)
{
Chenkitu(str);
int lens = str.length(); // lens = do dai cua xau str
int maxres = 0; // do dai cua xau duoi xung dai nhat
for (int center=0; center<lens; center++) // Vi tri trung tam
{
int right = center+1; // Vi tri phai xut phat bat dau tu center
int res;
if (str[center] == '#') res = 0; else res = 1;
while((str[right] == str[2*center-right]) && (2*center-right >= 0) && (right <lens))
{
if (str[right] != '#') res = res + 2;
right++;
}
if (maxres < res) maxres = res;
}
return maxres;
}
int main()
{
int T; // so luong bo TEST T
cin >> T;
vector<string> vt;
for (int i=0; i<T; i++)
{
string temp;
cin >> temp; // Doc gia tri tu man hinh luu vao temp
vt.push_back(temp); //
}
for (int i=0; i<T; i++)
cout << Manacher(vt[i]) << endl;
return 0;
}