Lexicography——CF1267L構造題

PushyTao發表於2020-10-05
										L. Lexicography
										time limit per test3 seconds
										memory limit per test512 megabytes
										inputstandard input
										outputstandard output

Lucy likes letters. She studied the definition of the lexicographical order at school and plays with it.

At first, she tried to construct the lexicographically smallest word out of given letters. It was so easy! Then she tried to build multiple words and minimize one of them. This was much harder!

Formally, Lucy wants to make n words of length l each out of the given n⋅l letters, so that the k-th of them in the lexicographic order is lexicographically as small as possible.

Input
The first line contains three integers n, l, and k (1≤k≤n≤1000; 1≤l≤1000) — the total number of words, the length of each word, and the index of the word Lucy wants to minimize.

The next line contains a string of n⋅l lowercase letters of the English alphabet.

Output
Output n words of l letters each, one per line, using the letters from the input. Words must be sorted in the lexicographic order, and the k-th of them must be lexicographically as small as possible. If there are multiple answers with the smallest k-th word, output any of them.
樣例輸入1

3 2 2
abcdef

樣例輸出1

af
bc
ed

樣例輸入2

2 3 1
abcabc

樣例輸出2

aab
bcc

題目大意:
給出一個長度為 n * l 的字串,然後構造出一個字串使得第 k 個字串的字典序儘可能的小
PS:本題為一道特判題,可以根據自己的思路進行操作,只要使得第 k 個字串的字典序儘可能小就是了

思路:按照每一位進行處理,在處理每一位的時候,首先處理前 k 個 ,然後在處理從k + 1往後的部分

int n, len, k;
string s[1008];
char yt[maxn];
int cnt[30];///用來統計數量
int main()
{
	cin >> n >> len >> k;
	cin >> yt;
	for (int i = 0; i < n * len; i++) {
		cnt[yt[i] - 'a']++;///統計字母的數量
	}
	int l = 0, r = k - 1;///左右兩側
	/// 做出前k個
	for (int i = 0; i < len; i++) {///長度,列舉每一位
		/// 一開始的時候先處理前k個
		for (int j = l; j < k; j++) {/// 1 -- k-1
			/// 遍歷26個字母
			for (int t = 0; t <= 25; t++) {///按位按照字典序從a->z放置
				if (cnt[t]) {///有就放上
					s[j] += (t + 'a');///加上這個字元
					cnt[t]--;///對應的數量減小1
					break;///每次加上一個,然後直接break就好
				}
			}
		}
		for (int j = l; j < k; j++) {
			if (s[j][i] != s[k - 1][i]) {
				while (s[j].size() < len) {///如果同一位不同並且size不到 l 給他放上大的
					for (int t = 25; t >= 0; t--) {
						if (cnt[t]) {
							s[j] += ('a' + t);
							cnt[t]--;
							break;
						}
					}
				}
				l = j + 1;///第j個放好了就直接下一次從j+1開始
			}///就順便將j+1賦值給 i
		}
	}
	/// 做從k+1開始的串
	for (int i = k; i < n; i++) {
		while (s[i].size() < len) {
			for (int t = 25; t >= 0; t--) {
				if (cnt[t]) {
					s[i] += ('a' + t);
					cnt[t]--;
					break;
				}
			}
		}
	}
	///cout << s[1] << endl;
	sort(s, s + n);
	for (int i = 0; i < n; i++) {
		cout << s[i] << endl;
	}
	return 0;
}

相關文章