Codeforces Round #705 (Div. 2) C. K-beautiful Strings(思维 枚举)

题目链接:https://codeforces.com/contest/1493/problem/C

You are given a string ss consisting of lowercase English letters and a number kk. Let’s call a string consisting of lowercase English letters beautiful if the number of occurrences of each letter in that string is divisible by kk. You are asked to find the lexicographically smallest beautiful string of length nn, which is lexicographically greater or equal to string ss. If such a string does not exist, output −1−1.

A string aa is lexicographically smaller than a string bb if and only if one of the following holds:

aa is a prefix of bb, but a≠ba≠b;
in the first position where aa and bb differ, the string aa has a letter that appears earlier in the alphabet than the corresponding letter in bb.
Input
The first line contains a single integer TT (1≤T≤100001≤T≤10000) — the number of test cases.

The next 2⋅T2⋅T lines contain the description of test cases. The description of each test case consists of two lines.

The first line of the description contains two integers nn and kk (1≤k≤n≤1051≤k≤n≤105) — the length of string ss and number kk respectively.

The second line contains string ss consisting of lowercase English letters.

It is guaranteed that the sum of nn over all test cases does not exceed 105105.

Output
For each test case output in a separate line lexicographically smallest beautiful string of length nn, which is greater or equal to string ss, or −1−1 if such a string does not exist.

Example

input

4
4 2
abcd
3 1
abc
4 3
aaaa
9 3
abaabaaaa

output

acac
abc
-1
abaabaaab

分析

首先若 n 不能被 k 整除的话答案肯定是 -1 。
维护每一个字母在每一位的前缀和。
因为要找到大于其的最小的答案,所以我们从后往前遍历,对于每一位,我们再遍历大于这一位字母的所有字母,然后该位及以前的所有的字母的数量我们都可以知道,那么我们就可以得到后面至少需要多少位置,如果够填入,则在最后依次填入,剩余的位置全部填入 ‘a’ 即可。

代码

#include<bits/stdc++.h>
using namespace std;

int n,m;
char s[100005];
int sum[100005][26];

int main()
{
	int T = 1;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d",&n,&m);
		scanf("%s",s+1);
		if(n % m != 0)
		{
			printf("-1\n");
			continue;
		}
		for(int i=1;i<=n;i++)
		{
			for(int j=0;j<26;j++) sum[i][j] = sum[i - 1][j];
			sum[i][s[i] - 'a']++;	
		}
		int flag = 1;
		for(int i=0;i<26;i++)
			if(sum[n][i] % m != 0)
			{
				flag = 0;
				break;
			}
		if(flag == 1)
		{
			printf("%s\n",s+1);
			continue;
		}
		for(int i=n;i>=1;i--)
		{
			for(int z=s[i]-'a'+1;z<26;z++)
			{
				sum[i][z]++;
				sum[i][s[i] - 'a']--;
				int rest = n - i;
				int need = 0;
				for(int j=0;j<26;j++)
					need += (m - sum[i][j] % m) % m;
				if(need > rest)
				{
					sum[i][z]--;
					sum[i][s[i] - 'a']++;
					continue;
				}
				for(int j=1;j<i;j++) printf("%c",s[j]);
				printf("%c",z + 'a');
				for(int j=1;j<=rest-need;j++) printf("a");
				for(int j=0;j<26;j++)
					for(int k=1;k<=(m - sum[i][j] % m) % m;k++)
						printf("%c",'a'+j);
				printf("\n");
				flag = 1;
				break;
			}
			if(flag == 1) break;
		}
		if(flag == 0) printf("-1\n");
	}
	return 0;
}
上一篇:CodeForces300C Beautiful Numbers


下一篇:D. Beautiful Graph(思维+二分图染色)