[PAT解题报告] Longest Symmetric String

```#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;

char s[1024];

int main() {
gets(s);
int n = strlen(s);
int answer = 0;
for (int i = 0; i < n; ++i) {
int len = 0;
for (len = 1; (i - len >= 0) && (i + len < n) && (s[i - len] == s[i + len]); ++len)
;
answer = max(((len - 1) << 1) | 1, answer);
for (len = 0; (i - len >= 0) && (i + len + 1 < n) && (s[i - len] == s[i + len + 1]); ++len)
;
}
return 0;
}
```

```#include <string>
#include <cstring>
#include <vector>
#include <cstdio>
using namespace std;

char S[1024];

int run(char *S, int n) {
string s = "#";
for (int i = 0; i < n; ++i) {
s += S[i];
s += "#";
}
n = s.length();
int center = 0, right = 0,answer = 0;
vector<int> p;
p.resize(n);
for (int i = 0; i < n; ++i) {
int ii = (center << 1) - i;
for (p[i] = (i < right)?min(p[ii], right - i):0; (i - p[i] - 1 >= 0) && (i + p[i] + 1 < n) && (s[i - p[i] - 1] == s[i + p[i] + 1]); ++p[i])
;
if (i + p[i] > right) {
right = i + p[i];
center = i;
}

}
}

int main() {
gets(S);
printf("%d\n",run(S, strlen(S)));
return 0;
}```

2022-12-31 23:19

2022-12-27 18:33

2022-12-20 00:05

2022-12-13 21:48

2022-12-15 20:08

2022-12-28 17:14

2022-12-30 21:00

2022-12-28 15:36