[BOJ] 13275 가장 긴 팰린드롬 부분 문자열

Time Lapse :None

13275.c

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
#include <stdio.h>
#define pc(x) putchar_unlocked(x)
#define gc() getchar_unlocked()
int main(int argc, char** argv){
register int r = 0, p = 0, answer = 0, len = 0 , i = 0;
int DP[200001] = { 0, };
char s[200001];
do {
s[len++] = '#'; s[len++] = gc();
} while (s[len-1] != '\n');
s[len++] = '#';
for (; i < len-answer; i++) {
if (i <= r) DP[i] = DP[(p<<1) - i] > r - i ? r - i : DP[(p<<1) - i];
while (i - DP[i] - 1 >= 0 && i + DP[i] + 1 < len && s[i - DP[i] - 1] == s[i + DP[i] + 1]) DP[i]++;
if (r < i + DP[i]) {
r = i + DP[i]; p = i;
}
answer = DP[i] > answer ? DP[i] : answer;
}
r = p = 0;
while (!(answer % 10)) { p++; answer /= 10; } //obtain the count of the number of 0s
while (answer) { r = (r << 3) + (r << 1) + answer % 10; answer /= 10; } //store reverse of N in rev
while (r) { pc(r % 10 + '0'); r /= 10; }
while (p--) pc('0');
return 0; //정상종료시 반드시 0을 리턴해야 합니다.
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/23/PS/BOJ/13275/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.