fragment-header
fragment-markdown
홈
로그인
로그아웃
내 블로그
설정
로그인
백준 23732 (C++) Successful String
최초 업로드: 2025-09-22 08:06:34
최근 수정 시간: 2025-09-22 08:07:24
게시자: rlatjwls3333
카테고리: 백준
조회수: 4
# [Gold IV] Successful String [문제 링크](https://www.acmicpc.net/problem/23732) ## 문제 설명 <p>Yunee is studying English vocabulary. Among many words that Yunee has learned, "success" is Yunee's favorite word and "failure" is Yunee's least favorite word.</p> <p>Yunee noticed that two identical characters appear in a row in the word "success" but not in the word "failure." So Yunee decided to call a string "successful string" when it has a property of the word "success." Formally, a string $S$ is a successful string when there is a position $i$ such that $S_i = S_{i+1}$. Here $S_i$ denotes the $i$-th character of $S$.</p> <p>Yunee wants to count how many successful strings there are among the substrings of a given string. Help Yunee write a program that counts the number of successful substrings. <strong>You have to consider two substrings distinct when their positions are different, even if the two strings are the same.</strong></p> ## 입력 <p>The first line contains an integer $N$ that represents the length of a string. $(1 \leq N \leq 10^6)$</p> <p>The second line contains a string of length $N$ consisting of lowercase alphabets.</p> ## 출력 <p>Output the number of successful strings among the substrings of the given string.</p> ## 풀이 현재 문자를 마지막으로 포함하는 최소 길이의 successful string을 구하고 왼쪽에 있는 여분의 문자의 개수+1개씩 더해주었습니다. ``` c++ #include<bits/stdc++.h> using namespace std; typedef long long ll; int main() { ios::sync_with_stdio(0); cin.tie(0); ll n; string s; cin >> n >> s; ll cnt=0, start=0, end=0; for(int i=1;i<n;i++) { if(s[i]==s[i-1]) start = i-1; else if(start==end) start = i; end = i; if(start!=end) cnt += start+1; } cout << cnt; } ```