본문 바로가기
Algorithms/simulation

온라인 팰린드롬

by OKOK 2018. 11. 14.

어떤 단어 앞뒤로 거꾸로 팰린드롬

가장 긴 팰린드롬 부문 문자열의 길이를 구하는 프로그램을 작성 

 

샘플 예제는 Longest Subsequence로 해결해야 하지만,

이 문제를 맞추기 위해서는 연속 문자열으로 해결해야 합니다

#include <stdio.h>
int N, M;
#define NN 1002
char A[NN];

int myslen() {
    int t = 0;
    for (; A[t] && t<NN; t++); // t는 최대로 돌리고, A[t]가 빈숫자면 탈출하도록 되어있음
    return t;
}

int mymax(int a, int b) {
    return (a>b) ? a : b;
}

int checkOdd(int c) {
    int t;
    for (t = 1; c >= t && c + t<N; t++) {
        if (A[c + t] != A[c - t])
            return 2 * (t - 1) + 1;
    }
    return 2 * (t - 1) + 1;
}

int checkEven(int c) {
    int t;
    for (t = 1; c >= t && c + t - 1<N; t++) {
        if (A[c + t - 1] != A[c - t])
            return 2 * (t - 1);
    }
    return 2 * (t - 1);
}

int prog() {
    int i, ret = 1;
    for (i = 0; i<N; i++) {
        int m1 = checkEven(i); // 짝수인가
        int m2 = checkOdd(i); // 홀 수인가
        ret = mymax(ret, m1); // 각각 최대값 찾기
        ret = mymax(ret, m2);
    }
    return ret;
}

int main(void)
{
    freopen("test.txt", "r", stdin);
    int test_case;
    int T;
    
    setbuf(stdout, NULL);
    scanf("%d", &T);

    int i, p, j;

    for (test_case = 1; test_case <= T; ++test_case)
    {
        
        scanf("%s", A);
        N = myslen();

        int M = prog();

        printf("#%d %d\n", test_case, M);
    }
    return 0; //정상종료시 반드시 0을 리턴해야 합니다.
}

- 코드 보기
- 길이를 재는 함수
- 프로그레스 함수
- 짝수, 홀수 체크
- 그리고 결과값가지고 체크하기

 

'Algorithms > simulation' 카테고리의 다른 글

링크드 리스트 백준 조세퍼스  (0) 2018.11.16
user 청소봇  (0) 2018.11.14
온라인 지구 온난화  (0) 2018.11.14
두 개의 미로  (0) 2018.11.14
2-1 조 구성하기  (0) 2018.11.14

댓글