출처: https://cses.fi/problemset/task/1069 


문제 설명

You are given a DNA sequence: a string consisting of characters A, C, G, and T. Your task is to find the longest repetition in the sequence. This is a maximum-length substring containing only one type of character.

입력 

The only input line contains a string of n characters.

출력

Print one integer: the length of the longest repetition.

입력 예

ATTCGGGA

출력 예

3


문제 풀이

1 <= n <= 10^6

문자열을 입력받아서, 앞에서부터 문자 하나씩 읽어 가면서, 직전 문자와 같으면 카운터를 증가시키고, 다르면 초기화 시킨다. 가장 큰 카운터 값을 기록하고 있다가, 변경된 카운터 값과 비교해서 최대값을 유지시킨다.

프로그램 내용

더보기
#define MAX_N 1000000

int main()
{
    string inString;
    cin >> inString;

    int s_count = 0;
    int t_count = 1;
    for (int i=1; i <= inString.size(); ++i)
        char t_char = inString[i-1];
        char tt_char = inString[i];

        if ( t_char == tt_char)
            t_count++;
        else
            t_count = 1;

        s_count = max (s_count, t_count);

	cout << s_count << endl;

 

 

Introductory problems ( link )

'CSES' 카테고리의 다른 글

CSES 1. Permutations (1070)  (0) 2019.09.19
CSES 1. Increasing Array (1094)  (0) 2019.09.19
CSES 1. Missing Number (1083)  (0) 2019.09.15
CSES - Introductory Problems  (0) 2019.09.14
CSES 1. Weird Algorithm (1068)  (2) 2019.09.14

+ Recent posts