codeforces 55 div2 C.Title 模拟
2 seconds
256 megabytes
standard input
standard output
Vasya has recently finished writing a book. Now he faces the problem of giving it the title. Vasya wants the title to be vague and mysterious for his book to be noticeable among others. That's why the title should be represented by a single word containing at least once each of the first k Latin letters and not containing any other ones. Also, the title should be a palindrome, that is it should be read similarly from the left to the right and from the right to the left.
Vasya has already composed the approximate variant of the title. You are given the title template s consisting of lowercase Latin letters and question marks. Your task is to replace all the question marks by lowercase Latin letters so that the resulting word satisfies the requirements, described above. Each question mark should be replaced by exactly one letter, it is not allowed to delete characters or add new ones to the template. If there are several suitable titles, choose the first in the alphabetical order, for Vasya's book to appear as early as possible in all the catalogues.
The first line contains an integer k (1 ≤ k ≤ 26) which is the number of allowed alphabet letters. The second line contains s which is the given template. In s only the first k lowercase letters of Latin alphabet and question marks can be present, the length of s is from 1 to 100 characters inclusively.
If there is no solution, print IMPOSSIBLE. Otherwise, a single line should contain the required title, satisfying the given template. The title should be a palindrome and it can only contain the first k letters of the Latin alphabet. At that, each of those k letters must be present at least once. If there are several suitable titles, print the lexicographically minimal one.
The lexicographical comparison is performed by the standard < operator in modern programming languages. The line a is lexicographically smaller than the line b, if exists such an i (1 ≤ i ≤ |s|), that ai < bi, and for any j (1 ≤ j < i) aj = bj. |s| stands for the length of the given template.
3
a?c
IMPOSSIBLE
2
a??a
abba
2
?b?a
abba
题意:需要使用n个字符得到一个字典序最小的回文串;
思路:先预处理出含有的字母,跟?是不是根据回文可以得到;
从中间往前遍历,如果可以用未出现的最大的字母填入,否则用最小的字母填;
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define inf 100000000000005
#define MAXN 10000010
#define pi 4*atan(1)
#define esp 0.000000001
//#pragma comment(linker, "/STACK:102400000,102400000")
char a[];
int flag[],ans;
void init(int len)
{
for(int i=;i<=len/;i++)
{
if(a[i]=='?'&&a[len-i-]!='?')
a[i]=a[len-i-],flag[a[len-i-]-'a']=;
if(a[i]!='?'&&a[len-i-]=='?')
a[len-i-]=a[i],flag[a[i]-'a']=;
if(a[i]!='?'&&a[len-i-]!='?')
flag[a[i]-'a']=;
}
for(int i=;i<len/;i++)
{
if(a[i]!=a[len-i-]&&a[i]!='?')
{
ans=;
return;
}
}
}
int getsum(int x)
{
int sum=;
for(int i=;i<x;i++)
sum+=flag[i];
return sum;
}
int first(int x)
{
int i;
for(i=x-;i>;i--)
{
if(flag[i]==)
return i;
}
return i;
}
int main()
{
int x,y,z,i,t;
ans=;
scanf("%d",&x);
scanf("%s",a);
int len=strlen(a);
init(len);
if(ans)
{
printf("IMPOSSIBLE\n");
return ;
}
for(i=len/;i>=;i--)
{
if(a[i]=='?')
{
z=first(x);
a[i]=a[len-i-]=z+'a';
flag[z]=;
}
}
if(getsum(x)==x)
printf("%s\n",a);
else
printf("IMPOSSIBLE\n");
return ;
}
codeforces 55 div2 C.Title 模拟的更多相关文章
- Codeforces 583 DIV2 Asphalting Roads 模拟
原题链接:http://codeforces.com/problemset/problem/583/A 题意: 很迷很迷,表示没看懂..但是你看样例就秒懂了 题解: 照着样例模拟就好 代码: #inc ...
- Codeforces #180 div2 C Parity Game
// Codeforces #180 div2 C Parity Game // // 这个问题的意思被摄物体没有解释 // // 这个主题是如此的狠一点(对我来说,),不多说了这 // // 解决问 ...
- 【Codeforces #312 div2 A】Lala Land and Apple Trees
# [Codeforces #312 div2 A]Lala Land and Apple Trees 首先,此题的大意是在一条坐标轴上,有\(n\)个点,每个点的权值为\(a_{i}\),第一次从原 ...
- [codeforces 55]D. Beautiful numbers
[codeforces 55]D. Beautiful numbers 试题描述 Volodya is an odd boy and his taste is strange as well. It ...
- Codeforces #541 (Div2) - E. String Multiplication(动态规划)
Problem Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...
- Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)
Problem Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...
- Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)
Problem Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...
- Codeforces #548 (Div2) - D.Steps to One(概率dp+数论)
Problem Codeforces #548 (Div2) - D.Steps to One Time Limit: 2000 mSec Problem Description Input Th ...
- CodeForces.158A Next Round (水模拟)
CodeForces.158A Next Round (水模拟) 题意分析 校赛水题的英文版,坑点就是要求为正数. 代码总览 #include <iostream> #include &l ...
随机推荐
- IntelliJ Idea 2018 注册码
转自:http://idea.lanyus.com/ *.lanyus.com及*.qinxi1992.cn下的全部授权服务器已遭JetBrains封杀 请搭建自己的IntelliJ IDEA授权服务 ...
- 使用Spring报错:No default constructor found;
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error ...
- 树链剖分-点的分治(点数为k且距离最长的点对)
hdu4871 Shortest-path tree Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 130712/130712 K ( ...
- Spring Security使用心得
某天,你的客户提出这样一个需求,在点击购买商品的时,如果用户没有注册,并且用户没有账号,这时用户去创建账户,然后要直接返回到想购买商品的付款页面.你会该如何基于Spring Security实现? S ...
- Haskell ghci中调用pandoc的API进行markdown转换
所用环境:Windows Server 2008 + ghc 7.6.3(Haskell Platform 2013.2.0.0自带的) + pandoc 1.12.4 操作步骤: 1. 安装Hask ...
- MegaCli 监控raid状态 限戴尔服务器
MegaCli 监控raid状态 MegaCli是一款管理维护硬件RAID软件,可以通过它来了解当前raid卡的所有信息,包括 raid卡的型号,raid的阵列类型,raid 上各磁盘状态,等等.通常 ...
- Maven的pom文件配置
pom.xml文件如下: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http:// ...
- innobackupex 还原和备份实例
InnoDB 和非 InnoDB 文件的备份都是通过拷贝文件来做的,但是实现的方式不同,前者是以page为粒度做的(xtrabackup),后者是 cp 或者 tar 命令(innobackupex) ...
- java-mybaits-010-mybatis-spring-使用 SqlSession、注入映射器
一. SqlSession概述 在 MyBatis 中,你可以使用 SqlSessionFactory 来创建 SqlSession.一旦你获得一个 session 之后,你可以使用它来执行映射语句, ...
- git-【十】忽略文件
1.在Git工作区的根目录下创建一个特殊的.gitignore文件,然后把要忽略的文件名填进去,Git就会自动忽略这些文件. 不需要从头写.gitignore文件,GitHub已经为我们准备了各种配置 ...