본문 바로가기

코드포스

[코드포스 Practice3] A. Uniform String

You are given two integers n and k.

Your task is to construct such a string s of length n that for each i from 1 to k there is at least one i-th letter of the Latin alphabet in this string (the first letter is 'a', the second is 'b' and so on) and there are no other letters except these. You have to maximize the minimal frequency of some letter (the frequency of a letter is the number of occurrences of this letter in a string). If there are several possible answers, you can print any.

You have to answer t independent queries.

 

n, k 두개의 정수가 있다. 

당신의 일은 길이가 n인 문자열 s를 만드는 것인데, 1부터 k까지 각각의 i는 최소한 하나의 i번째 알파벳이 있어야 한다. (1번째 알파벳은 a, 2번째 알파벳은 b와 같이) 그리고 알파벳을 제외한 다른 문자는 없다. 당신은 어떤 문자의 최소 빈도를 최대로 해야한다. (문자의 빈도는 문자열에 있는 문자가 발현된 빈도이다) 만약 가능한 답이 몇개가 있다면 당신은 어느것이든 출력해도 된다. 

당신은 t개의 독립적인 질문에 답을 해야 한다. 

 

Input

The first line of the input contains one integer t (1t100) — the number of queries.

The next t lines are contain queries, one per line. The i-th line contains two integers ni and ki (1 ni 100, 1 ki min(ni,26)) — the length of the string in the i-th query and the number of characters in the i-th query.

 

입력의 첫번째 줄은 하나의 정수 t를 포함한다. (1 t 100) - 이건 질문의 개수이다.

다음 t 줄은 한 줄에 하나씩 쿼리를 포한한다. i번째 줄은 두개의 정수인 ni와 ki를 포함한다. (1  ni  100, 1  ki  min(ni,26)) ni는 i번째 쿼리의 문자열 길이이고 ki는 i번째 쿼리의 문자의 개수이다.

 

Output

Print t lines. In the i-th line print the answer to the i-th query: any string si satisfying the conditions in the problem statement with constraints from the i-th query.

 

t개의 줄을 출력한다. i번째 줄에는 i번째 퀴리의 답을 출력한다. i번째 쿼리로부터 제약과 문제의 조건을 만족시키는 어떠한 문자열 si (¿이 문장은? ¿머라는지? ¿모르겠다?)

 


<요약>

- 문자열을 만들어야

- 길이 : n, 문자의 개수 : k (알파벳 1부터 k까지)

- 최소 빈도를 최대한으로 해야 한다

 

예를 들어 n = 7이고 k = 3이라 하면, n은 7이므로 문자열의 길이는 7이고 k가 3이므로 문자열이 알파벳 a, b, c로 이루어져야 한다. 문제는 여기서 최소 빈도를 최대한으로 해야한다. 최소 빈도를 최대한으로 하려면 문자 개수가 일정해야 한다. 그래서 빈 문자열에 n을 k로 나눈 만큼 알파벳을 붙이고 남은건 a를 붙였다. 그러면 모든 문자 빈도가 공평하게 n/k개가 된다. 

 

#include <iostream>
#include <string>
using namespace std;
 
int main() {
    int t;
    scanf("%d", &t);
    
    for(int i = 0; i < t; i++){
        int n, k;
        scanf("%d %d", &n, &k);
        
        string s = "";
        for(int j = 0; j < k; j++){ //a, b까지 돈다
            for(int m = 0; m < n/k; m++){
                s += (j+97);
            }
        }
        for(int m = 0; m < n%k; m++){
            s += 97;
        }
        cout << s << endl;
    }
    
    return 0;
}

코드는 이렇게 짰다. 

먼저 t를 입력받아서 t번 반복문을 돈다. 다음으로 n, k를 입력받는다. 

이제 빈 문자열을 만들어 a -> b -> c 순으로 n/k만큼 문자를 붙인다. 

다음으로 남는 길이만큼 a를 붙인다. 

마지막으로 출력하면 끝~~


int main()
{
	int t;
	scanf("%d", &t);

	for (int i = 0; i < t; i++)
	{
		int n, k;
		scanf("%d %d", &n, &k);

		for (int j = 0; j < n; j++)
			printf("%c", 'a' + (j % k));
		printf("\n");
	}

	return 0;
}

음.. 엥.. 음.. 오./

아!

문자열 길이인 n번만큼 반복하는데 그걸 k로 나누니 계속 1 -> 2 -> 3 -> 1 -> 2 이렇게 반복된다. 그리고 문자열 만들 필요 없이 바로바로 출력하면 되는구나!