자라는 개발자/문제풀이

백준 c++ 17164 Rainbow Beads

자란다 2022. 7. 1. 13:03
728x90
반응형

17164 Rainbow Beads



문제풀이

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
void fast_io(void)
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}
int n,leng=1,maxlen;
string str;
void lenset()
{
    if(leng>maxlen)
        maxlen = leng;
    leng = 1;
}
int main(void)
{
    fast_io();
    cin >> n >> str;
    for (int i = 1; i < n;i++)
    {
        if ((str[i - 1] == 'V' && str[i] == 'R') 
        || (str[i - 1] == 'R' && str[i] == 'V'))
            lenset();
        else if ((str[i - 1] == 'V' && str[i] == 'B') 
        || (str[i - 1] == 'B' && str[i] == 'V'))
            lenset();
        else if (str[i - 1] == str[i])
            lenset();
        else
            leng++;
    }
    cout << max(leng,maxlen);
}

연속된 문자열 & violet 과 blue & violet 과 red
위의 세 가지 경우를 걸렀다.

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
void fast_io(void)
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}
int n,leng=1,maxlen;
string str;
void lenset()
{
    if(leng>maxlen)
        maxlen = leng;
    leng = 1;
}
int main(void)
{
    fast_io();
    cin >> n >> str;
    for (int i = 1; i < n;i++)
    {
        if ((str[i - 1] == 'R' && str[i] == 'B') 
        || (str[i - 1] == 'B' && str[i] == 'R'))
            leng++;
        else
            lenset();
    }
    cout << max(leng,maxlen);
}

두개 다 되네..

728x90
반응형

'자라는 개발자 > 문제풀이' 카테고리의 다른 글

백준 c++ 20920 영단어 암기는 괴로워  (0) 2022.07.04
백준 c++ 1940 주몽  (0) 2022.07.02
백준 c++ 12845 모두의 마블  (0) 2022.06.26
백준 c++ 3135 라디오  (0) 2022.06.25
백준 c++ 2828 사과 담기 게임  (0) 2022.06.24