https://www.acmicpc.net/problem/26122

핵심 아이디어

<aside> 💡

$N$의 개수와 $S$의 개수를 다음과 같이 카운트 한다.

위 방법으로 카운트한 $N$의 개수와 $S$의 개수를 통해 유효한 자석문자열이 나올 때마다 정답을 갱신할 수 있다.


코드

#include <bits/stdc++.h>
#define FASTIO ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
using namespace std;
typedef long long ll;

int n;
string s;

signed main()
{
    FASTIO;
    cin >> n;
    cin >> s;

    int ans = 0;
    int ncnt = 0;
    int scnt = 0;
    if(s[0] == 'N') ncnt++;
    else scnt++;
    for(int i = 1; i<n; i++){
        if(s[i] == 'N'){
            if(s[i-1] == 'S') ncnt = 0;
            ncnt++;
            ans = max(ans, min(ncnt, scnt));
        }
        else{
            if(s[i-1] == 'N') scnt = 0;
            scnt++;
            ans = max(ans, min(ncnt, scnt));
        }
    }
    cout << 2*ans <<"\\n";

    
    return 0;
}