본문 바로가기
Computer Science

팰린드롬

by OKOK 2019. 2. 10.

1. 팰린드롬

2. 다른 코드가 있는 것 아님

3. 찾을 수 있음

4. 중앙점, 가운데점

5. 오케이


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
#include <stdio.h>
 
char s[15];
 
int main(void)
{
    freopen("input.txt""r", stdin);
    int t = 0, tc = 0;
 
    s[0= 1;
    scanf("%d"&t);
    while (tc++ < t)
    {
        scanf("%s", s + 1);
 
        int max = 1, len;
        for (len = 1; s[len + 1]; len++);
 
        // 아이는 팰린드롬 중앙점, hm은 양쪽으로 펴져가는 길이와 max 검사 가치치기용, len 은 전체 거리
        for (int i = 2, hm, l; i <= len; i++)
        {
            hm = max / 2;
            if (i + hm <= len) 
            {
                for (l = 1; s[i - l] == s[i + l]; l++); // 홀수 일때 검사
                if (2 * l - 1 > max)
                {
                    max = 2 * l - 1;
                }
            }
 
            // 짝수 팰린드롬 검사
            hm = (max - 1/ 2// max와 관계
            if (i + hm <= len)
            {
                for (l = 1; s[i - l] == s[i + l - 1]; l++); // 짝수일 때 검사
                l--;
                if (2 * l > max)
                {
                    max = 2 * l;
                }
            }
        }
 
        printf("#%d %d\n", tc, max);
    }
}
cs

 


'Computer Science' 카테고리의 다른 글

올해의 조련사  (0) 2019.02.10
Intersection  (0) 2019.02.10
이진 문자열 복원  (0) 2019.02.08
운동  (0) 2019.02.07
  (0) 2019.02.03

댓글