Little Jay

[C++] 백준 24524번 - 아름다운 문자열 본문

알고리즘/BOJ

[C++] 백준 24524번 - 아름다운 문자열

Jay, Lee 2022. 7. 10. 13:37
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;

string s, t;

void solve(string &s, string &t) {
	vector<queue<int>> v;
	int ans = 0;
	int pos = -1;
	int idx = 0;
	v.resize(26);

	for (int i = 0; i < s.size(); i++) {
		v[s[i] - 'a'].push(i);
	}

	while (true) {
		int temp = t[idx] - 'a';
		while (!v[temp].empty() && pos > v[temp].front()) {
			v[temp].pop();
		}

		if (!v[temp].empty()) {
			pos = v[temp].front();
			v[temp].pop();
		}
		else break;
		idx++;

		if (idx == t.size()) {
			pos = -1;
			ans++;
			idx = 0;
		}
	}

	cout << ans << endl;
	exit(0);
}

int main() {

	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);

	cin >> s >> t;
	solve(s, t);

	return 0;
}
Comments