๋ณธ๋ฌธ์œผ๋กœ ๋ฐ”๋กœ๊ฐ€๊ธฐ
728x90
๐Ÿ”—๋ฌธ์ œ ๋ณด๋Ÿฌ๊ฐ€๊ธฐ
 

16139๋ฒˆ: ์ธ๊ฐ„-์ปดํ“จํ„ฐ ์ƒํ˜ธ์ž‘์šฉ

์ฒซ ์ค„์— ๋ฌธ์ž์—ด $S$๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋Š” $200,000$์ž ์ดํ•˜์ด๋ฉฐ ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋กœ๋งŒ ๊ตฌ์„ฑ๋˜์—ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ์ค„์—๋Š” ์งˆ๋ฌธ์˜ ์ˆ˜ $q$๊ฐ€ ์ฃผ์–ด์ง€๋ฉฐ, ๋ฌธ์ œ์˜ ์ˆ˜๋Š” $1\leq q\leq 200,000$์„ ๋งŒ์กฑํ•œ๋‹ค. ์„ธ ๋ฒˆ์งธ

www.acmicpc.net

 

๐Ÿง‘‍๐Ÿ’ปํ’€์ด ๊ณผ์ •


๋‚˜์˜ ๋ฉ์ฒญํ•œ ์‹ค์ˆ˜

์ด์ „ ๋ฌธ์ œ์—์„œ ๋ˆ„์  ํ•ฉ์„ ์ž˜ ๊ตฌํ•ด๋†“๊ณ , ์—ฌ๊ธฐ์—์„œ๋Š” ์™œ ์ ์šฉ์„ ๋ชปํ–ˆ๋‚˜ ์‹ถ์Šต๋‹ˆ๋‹ค..

์ฒ˜์Œ์—๋Š” DP[26][S.length()] ๋ฐฐ์—ด์„ ์„ ์–ธํ•˜์—ฌ, ๊ฐ ์•ŒํŒŒ๋ฒณ๋งˆ๋‹ค ๋“ฑ์žฅํ•œ ํšŸ์ˆ˜๋ฅผ ๋ˆ„์ ํ•˜์—ฌ ์ €์žฅํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ ‘๊ทผํ•˜์˜€์Šต๋‹ˆ๋‹ค.

const int ALPHABET_COUNT = 26;
const int LOWER_A_ASCII = 97;
string S;

vector<vector<int>> DP(ALPHABET_COUNT, vector<int>(S.length()));
for (int i = 0; i < S.length(); i++)
{
    // i-1๋ฒˆ ์—ด์˜ ๋ˆ„์  ๊ฐœ์ˆ˜๋“ค ๊ทธ๋Œ€๋กœ ๊ฐ€์ ธ์˜ค๊ธฐ
    for (int j = 0; (i != 0) and (j < ALPHABET_COUNT); j++)
        if (DP[j][i-1] != 0)
        	DP[j][i] = DP[j][i-1];
    
    // S[i]์— ํ•ด๋‹นํ•˜๋Š” ์•ŒํŒŒ๋ฒณ ๊ฐœ์ˆ˜ ์ถ”๊ฐ€
    int index = S[i] - LOWER_A_ASCII;
    DP[index][i]++;
}

 

์ด๋ ‡๊ฒŒ ์ž˜ ๊ตฌํ•ด๋†“๊ณ , ์™œ ์งˆ๋ฌธ์—์„œ DP[alphabet][r] - DP[alphabet][l - 1]์„ ์ƒ๊ฐ ๋ชป ํ–ˆ๋‚˜ ์‹ถ๋„ค์š”...

์•„๋ฌดํŠผ, ์œ„ ์‹์„ ์ƒ๊ฐ ๋ชปํ•˜๋Š” ๋ฐ”๋žŒ์— ์ด์ง„ ํƒ์ƒ‰(Binary Search)๋กœ ํ’€์ด ๋ฐฉํ–ฅ์„ ๋ฐ”๊พธ์—ˆ์Šต๋‹ˆ๋‹ค..

 

์ด์ง„ ํƒ์ƒ‰ ๋ฐฉ์‹ ์ ‘๊ทผ

  1. ์ฃผ์–ด์ง„ S ๋ฌธ์ž์—ด์—์„œ, ๊ฐ๊ฐ์˜ ์•ŒํŒŒ๋ฒณ๋“ค์ด ๋“ฑ์žฅํ•˜๋Š” ์ธ๋ฑ์Šค ์œ„์น˜๋“ค์„ ๋ฐฐ์—ด๋กœ ๋‹ด์•˜์Šต๋‹ˆ๋‹ค.
    • for ๋ฌธ์œผ๋กœ ์ˆœ์ฐจ ํƒ์ƒ‰ํ•œ ๊ฑฐ๋ผ, ์ž๋™์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ์ด ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.
  2. ๋ฌธ์ œ์—์„œ ์ฃผ์–ด์ง„ l, r์— ๋Œ€ํ•˜์—ฌ, l์—๋Š” lower_bound(), r์—๋Š” upper_bound()๋ฅผ ์ ์šฉํ•ฉ๋‹ˆ๋‹ค.
    • lower_bound()์™€ upper_bound() ๊ฒฐ๊ณผ ๊ฐ๊ฐ์— vector.begin()์„ ๋นผ๋ฉด ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ๊ฐ€ ๋‚˜์˜ต๋‹ˆ๋‹ค.
  3. 2๋ฒˆ์—์„œ ๊ตฌํ•œ ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ ์ฐจ์ด๊ฐ€ ์ •๋‹ต์ž…๋‹ˆ๋‹ค.

 

โœ๏ธ์†Œ์Šค ์ฝ”๋“œ ๋ฐ ๊ฒฐ๊ณผ

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
const int ALPHABET_COUNT = 26;
const int LOWER_A_ASCII = 97;

int main()
{
	ios::sync_with_stdio(false);
	cout.tie(NULL); cin.tie(NULL);

	int quesitionCount;
	string sequence;
	cin >> sequence >> quesitionCount;

	// 1. ๊ฐ ์•ŒํŒŒ๋ฒณ๋งˆ๋‹ค, ๋‚˜์˜ค๋Š” ์œ„์น˜๋“ค์„ ์ €์žฅ
	vector<vector<int>> alphabetPositions(ALPHABET_COUNT);
	for (int i = 0; i < sequence.length(); i++)
	{
		int index = sequence[i] - LOWER_A_ASCII;
		alphabetPositions[index].emplace_back(i);
	}

	// 2. ์ด์ง„ ํƒ์ƒ‰์„ ์ด์šฉํ•˜์—ฌ, ์ธ๋ฑ์Šค ์ฐจ์ด๋ฅผ ๊ณ„์‚ฐ ํ›„ ์ถœ๋ ฅ
	for (int i = 0; i < quesitionCount; i++)
	{
		char find;
		int l, r;
		cin >> find >> l >> r;

		int index = find - LOWER_A_ASCII;
		int start = lower_bound(alphabetPositions[index].begin(), alphabetPositions[index].end(), l) - alphabetPositions[index].begin();
		int last = upper_bound(alphabetPositions[index].begin(), alphabetPositions[index].end(), r) - alphabetPositions[index].begin();

		cout << last - start << "\n";
	}

	return 0;
}

 

728x90
๋ฐ˜์‘ํ˜•