Codeforces Round #499 (Div. 2) A. Stages(思维)

题目链接:http://codeforces.com/contest/1011/problem/A

题意:给你n个字符,然后让你从中选m个,要求不能有重复的,字符不能相邻,比如第一个是a,第二个就只能是c之后的字母,如果是d,下一个就不能是e,只能是f,g.......之后的字母,其中a的值为1,b为2......z为26,问选出来的m个字符的最小值是多少。

思路:直接看代码吧

#include <bits/stdc++.h>
using namespace std;
inline int read()
{
    char ch = getchar(); int x = 0, f = 1;
    while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
    while('0' <= ch && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
    return x * f;
}
int n,k,v[32];
 char s[28];
int main()
{
	n = read(); m = read();
	scanf("%s", s+1);
	for(int i = 1; i <= n; i++) v[s[i]-'a'] = 1;
	int now = -999, cnt = 0, ans = 0;
	for(int i = 0; i < 26; i++)
	if(v[i] && i-now>1 && cnt<k) cnt++, now = i, ans += i+1;
	printf("%d\n", cnt < k ? -1 : ans);
}

猜你喜欢

转载自blog.csdn.net/sugarbliss/article/details/81252013