기억보다는 기록을 해볼까

24524 아름다운 문자열 본문

백준으로 C++ 공부하기

24524 아름다운 문자열

옥상에서 2023. 2. 23. 22:50
728x90

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

 

24524번: 아름다운 문자열

첫째 줄과 둘째 줄에 영어 소문자로만 이루어진 문자열 $S$와 $T$가 각각 주어진다. $(1\leq \left|S \right|\leq 1\,000\,000;$ $1\leq \left|T \right|\leq 26;$ $\left|T \right|\leq \left|S \right|)$ $T$의 모든 문자는 서로 다

www.acmicpc.net

 

vector 안에 queue 넣기. 이렇게 하는 군

 

vector<queue<int>> que(26, queue<int>());

 

 

#include <iostream>
#include <vector>
#include <queue>
using namespace std;

#define FASTIO cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);
#define INF 2e9

int main() {
	FASTIO;
	string S, T;
	vector<queue<int>> que(26, queue<int>());
	cin >> S >> T;
    for (int i = 0; i < S.size(); i++)
    {
        que[S[i] - 'a'].push(i);
    }
    int idx = 0;
	int pos = -1;
	int ans = 0;
	while (true) {
		int ch = T[idx] - 'a';
		while (!que[ch].empty() && pos > que[ch].front()) {
			que[ch].pop();
		}
		if (!que[ch].empty()) {
			pos = que[ch].front();
			que[ch].pop();
		}
		else {
			break;
		}
		idx += 1;
		if (idx == T.size()) {
			pos = -1;
			ans++;
			idx = 0;
		}
	}
	cout << ans;

	return 0;
}

 

참조: https://burningfalls.github.io/algorithm/boj-24524/

728x90
Comments