Problem

A new academic year approaches, and the dean must make a schedule of classes for first-year students. There must be n classes in the schedule. The dean should take into account the following interesting observation made in recent years: students skip all classes with even numbers and attend all classes with odd numbers (the classes are numbered from 1). Of course the dean wants students to attend as many important classes as possible, so he tries to assign subjects that are more important to places with odd numbers and subjects that are less important to places with even numbers. The method of estimating the quality of the schedule at the Department of Mathematics and Mechanics must be as formal as possible.

The first-year schedule may contain any of 26 subjects taught at the department. We denote them by English letters from a to  z. The importance of a subject corresponds to its position in the English alphabet. Thus, subject a has importance 1, and subject  z has importance 26. The quality of a schedule is the sum of importances of subjects in it, where subjects on odd places are counted with a plus sign, and subjects on even places are counted with a minus sign.

Unfortunately, a shedule has some restrictions due to administrative reasons. First, the schedule must contain at least k different subjects, so the dean cannot occupy all odd places with mathematical analysis and all even places with philosophy. Second, certain subjects must be assigned to certain places. Help the dean to make a schedule of maximum quality under these restrictions.

Input

The first line contains a string of length n (1 ≤ n ≤ 10 5) consisting of lowercase English letters and question marks. The string specifies the subjects that are already in the schedule. The letters denote these subjects, and the question marks stand for vacant places. In the second line you are given an integer k (1 ≤ k≤ 26), which is the minimum number of different subjects in the schedule.

Output

If it is impossible to replace all question marks by lowercase English letters so that the string would contain at least k different letters, output “-1” (without quotation marks). Otherwise, output any of the resulting strings that maximizes the quality of the schedule given by the string.

Example

input output
??
1
za
??
3
-1
aza
1
aza
aza
3
-1

Notes

In the first sample the dean can make any schedule with two subjects (even identical), but the quality of the schedule “za” is 26 − 1 = 25, and this is the maximum possible value of the quality.

In the second sample it is impossible to make a schedule consisting of two classes with three different subjects.

In the third sample the dean has only one variant. Though the schedule is bad (1 − 26 + 1 = −24), nothing better can be proposed.

In the fourth sample the only possible variant doesn’t contain three different subjects.

题解:(抽象理解一下qwq)要安排课程,课程的种类是a~z,每个课程都有不同的权值,a~z对应1~26,如果在奇数位上就是加上这个权值,如果是偶数位,就减去这个权值,现在给你一串字符串,里面有字母和问号,问号是由你来安排,字母是已经安排好的不需要再改变,再给你一个数k,问能够安排大于k种类的课程,且要求权值最大。

只需要找出来添加的种类,添加的字符只添加一个,并且是偶数位就从a开始找,是奇数就从z开始找,其他位置是奇数位就输出z,是偶数就输出a。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <set>
using namespace std;
typedef long long ll;
char a[100005]; // a是读入的字符串
int num[505] = {0}; // 用来统计那个字符出现过
int main()
{
int n, i, j, k;
scanf("%s %d", &a, &k);
int len = strlen(a);
int sum1 = 0, sum2 = 0, n1 = 0, n2 = 0; //sum1是问号的个数 sum2是出现字符种类
for(i = 0; i < len; i++) // n1记录出现的?在奇数位的个数,n2记录在偶数位
{
if(a[i] >= 'a' && a[i] <= 'z')num[a[i]]++;
else sum1++;
if(a[i] == '?')
{
if(i % 2 == 0)n1++;
else n2++;
}
}
for(i = 'a'; i <= 'z'; i++) // 记录出现的字符个数
{
if(num[i])sum2++;
}
if(sum2 + sum1 < k) // 如果不足够k 则不满足情况
{
printf("-1\n");
return 0;
}
int sum = max(k - sum2, 0); // 记录还需要添加的种类
for(i = 0; sum != 0; i++) // 通过去掉需要添加的,判断可以任意安排的个数
{
if(num['a' + i] == 0 && n2 > 0) // 从两边开始寻找,那么差值一定最小,即权值一定最大
{
n2--;
sum--;
if(sum == 0)break;
}
if(num['z' - i] == 0 && n1 > 0)
{
n1--;
sum--;
if(sum == 0)break;
}
}
for(i = 0; i < len; i++) // 处理、输出
{
if(a[i] != '?')printf("%c", a[i]); // 如果已经安排好的,直接输出
else
{
if(i % 2 == 0) // 如果是奇数位
{
if(n1 > 0) //如果还有可以自由安排的奇数位数的地方,输出z,权值高
{
printf("z");
n1--;
}
else // 输出任意之后,输出需要的种类
{
for(j = 'z'; j >= 'a'; j--)
{
if(num[j] == 0)break; // 因为奇数位相加,所以从最大的开始遍历,如果原来不存在则输出
}
num[j]++; // 输出后标记
printf("%c", j);
}
}
else
{
if(n2 > 0) // 同理找偶数位
{
printf("a");
n2--;
}
else
{
for(j = 'a'; j <= 'z'; j++)
{
if(num[j] == 0)break;
}
num[j]++;
printf("%c", j);
}
}
}
}
printf("\n");
return 0;
}

Dean and Schedule (URAL 2026)的更多相关文章

  1. springboot集成schedule(深度理解)

    背景 在项目开发过程中,我们经常需要执行具有周期性的任务.通过定时任务可以很好的帮助我们实现. 我们拿常用的几种定时任务框架做一个比较: 从以上表格可以看出,Spring Schedule框架功能完善 ...

  2. hdu1150-Machine Schedule(最小点覆盖)

    二分图的最小顶点覆盖:用最少的点,让每条边都至少和其中一个点关联.     最大匹配数 = 最小点覆盖数(Konig 定理) 水题…… 突然发现我以前的匈牙利算法模版有问题……因为这里左边的点时1~n ...

  3. HDU - 1150 Machine Schedule(最小点覆盖数)

    1.有两台机器A和B以及N个需要运行的任务.A机器有n种不同的模式,B机器有m种不同的模式,而每个任务都恰好在一台机器上运行.如果它在机器A上运行,则机器A需要设置为模式xi,如果它在机器B上运行,则 ...

  4. 百度之星资格赛——Disk Schedule(双调旅行商问题)

    Disk Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  5. hdu 3572 Task Schedule (dinic算法)

    pid=3572">Task Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  6. ZOJ2930 The Worst Schedule(最小割)

    题目大概说有n个任务,每个任务可以提前或推迟,提前或推迟各有一定的费用,有的任务一旦推迟另一个任务也必须推迟,问怎么安排任务使花费最少,且最少花费的条件下提前的任务数最多能多少. 问题就是要把各个任务 ...

  7. LeetCode 207. Course Schedule(拓扑排序)

    题目 There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have p ...

  8. POJ 1325 Machine Schedule(最小点覆盖)

    http://poj.org/problem?id=1325 题意: 两种机器A和B.机器A具有n种工作模式,称为mode_0,mode_1,...,mode_n-1,同样机器B有m种工作模式mode ...

  9. hdu-----(1150)Machine Schedule(最小覆盖点)

    Machine Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

随机推荐

  1. Python实现八大排序(基数排序、归并排序、堆排序、简单选择排序、直接插入排序、希尔排序、快速排序、冒泡排序)

    目录 八大排序 基数排序 归并排序 堆排序 简单选择排序 直接插入排序 希尔排序 快速排序 冒泡排序 时间测试 八大排序 大概了解了一下八大排序,发现排序方法的难易程度相差很多,相应的,他们计算同一列 ...

  2. 第1章:Python语言与Linux系统管理

    1.Python语言为什么流行 1).简单易学 2).丰富强大的库 3).开发效率高 2.Python语言有什么缺点 1).Python的执行速度不够快 2).Python的GIL锁限制并发:GIL是 ...

  3. 学习BM算法

    BM算法: 希望大家别见怪,当前博客只用于个人记录所用. [例题]Poor God Water 题意: 有肉,鱼,巧克力三种食物,有几种禁忌,对于连续的三个食物, 1.这三个食物不能都相同: 2.若三 ...

  4. MyBatis MyBatis Generator入门

    一.MGB功能简介 MyBatis Generator是一个代码生成工具. MBG是如何运行的呢?它会检查所连接到的数据库的一个或者多个table,然后生成可用来访问这些table的构建(Java代码 ...

  5. 泛型和DataTable的属性

    泛型转DataTable public DataTable ToDataTable<TResult>(this IEnumerable<TResult> value) wher ...

  6. http GET 和 POST 请求的优缺点和误区

    (1)post更安全(不会作为url的一部分,不会被缓存.保存在服务器日志.以及浏览器浏览记录中) (2)post发送的数据更大(get有url长度限制) (3)post能发送更多的数据类型(get只 ...

  7. Stanford NLP 课程笔记之计算字符串距离

    在自然语言处理任务中,有时候需要计算两个字符串之间的相似度,也可以称作是两者之间的距离,用最小编辑距离表示. 最小编辑距离用{Insertion,Deletion,Substitution}这三种操作 ...

  8. 抽奖JQ

    <!DOCTYPE html> <html lang="en"> <head>     <meta charset="UTF-8 ...

  9. docker-compose deploy replicaSet in standalone MongoDB cluster and with auth

    经过两天的折腾,终于实现了自己想要的效果,就是通过docker-compose 部署最新的mongodb replicaSet, 主要是为了测试 4.2 最新的多文档事务,下面将整个步骤分享一下: d ...

  10. str 文本函数的调用

    方法 说明 S.isdigit() 判断字符串中的字符是否全为数字 S.isalpha() 判断字符串是否全为英文字母 S.islower() 判断字符串所有字符是否全为小写英文字母 S.isuppe ...