Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AGC_034_B - ABC #19

Open
zeikar opened this issue Oct 7, 2021 · 0 comments
Open

AGC_034_B - ABC #19

zeikar opened this issue Oct 7, 2021 · 0 comments
Assignees
Labels
study Study

Comments

@zeikar
Copy link
Owner

zeikar commented Oct 7, 2021

Problem link

https://atcoder.jp/contests/agc034/tasks/agc034_b

Problem Summary

주어진 문자열에서 ABC를 BCA로 최대 몇 번 바꿀 수 있는지 출력하는 문제.

Solution

생각보다 쉽지 않은데 몇 가지 특징을 써보자.

A, BC는 덩어리로 움직이고 나머지 문자열은 무시할 수 있다.
=> 나머지 문자열이 나오면 더 이상 변환이 불가능하다.
A, BC들이 적절히 만났을 때 모든 변환이 끝나면 A는 맨 뒤로 이동한다. (AABCBCBC => BCBCBCAA)
=> BC가 나오면 그 전의 A 개수만큼 변환 가능하다.

A 개수를 누적으로 카운트 해주면서 BC를 만나면 A 개수를 정답에 계속 더해나가면 된다.

Source Code

#include <iostream>
#include <string>
using namespace std;

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

	// dummy
	s += "F";

	long long ans = 0;
	long long aCount = 0;

	for (int i = 0; i < s.length(); i++)
	{
		if (s[i] == 'A')
		{
			aCount++;
		}
		else if (s.substr(i, 2) == "BC")
		{
			ans += aCount;
			i++;
		}
		else
		{
			aCount = 0;
		}
	}

	cout << ans << endl;
}
@zeikar zeikar added the study Study label Oct 7, 2021
@zeikar zeikar self-assigned this Oct 7, 2021
zeikar added a commit that referenced this issue Oct 7, 2021
zeikar added a commit that referenced this issue Oct 10, 2021
@zeikar zeikar changed the title [Study] AGC_034_B - ABC AGC_034_B - ABC Oct 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
study Study
Projects
None yet
Development

No branches or pull requests

1 participant