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 ...
随机推荐
- SHTML 教程
什么是 SHTML 使用SSI(Server Side Include)的html文件扩展名,SSI(Server Side Include),通常称为“服务器端嵌入”或者叫“服务器端包含”,是一种类 ...
- java 空间四点定位,可跟据已知的四点坐标(x,y,z)及距离计算所在位置坐标
public static void main(String args[]) { try{ float point[]=new float[3]; Location loc = new Locatio ...
- linux的~和/的区别
转自:https://zhidao.baidu.com/question/166486946.html /是目录层的分隔.表示符.只有一个/表明是root,/etc/表明是根目录下面的etc目录(当然 ...
- onethink重新安装,提示已安装过解决办法!
onethink完全重新安装的时候提示已安装过,直接跳过数据库安装的解决办法 首先:删除根目录下 Data目录下的 install.lock 文件,然后: 打开\Application\Install ...
- 310实验室OTL问题----将写好的C++文件转换成Python文件,并将数据可视化
如图:文件夹 第一处:optimizer文件夹下的:optimizer.h文件中添加你所写代码的头文件 #include <OTL/Optimizer/Reference-NSGA-II/Re ...
- Database Sharding Challenges DATABASE SHARDING
w分布式查询.数据聚合.跨碎片join是可且应避免的.自增主键管理.基于-会话/事务/语句-选择碎片.通过-主键/模块/碎片索引-碎片化数据 http://www.agildata.com/datab ...
- 转!idea 破解版 安装
原博文地址:https://blog.csdn.net/everest_man/article/details/78985879 1.官网下载 Ultimate版本 2.http://idea.la ...
- SQL基础--查询之一--单表查询
SQL基础--查询之一--单表查询
- 由SOAP说开去 - - 谈谈WebServices、RMI、RPC、SOA、REST、XML、JSON
引子: 关于SOAP其实我一直模模糊糊不太理解,这种模模糊糊的感觉表述起来是这样: 在使用web服务时(功能接口),本来我就可以通过安卓中固有的http类(使用http协议),来发送http请求,并且 ...
- React Native知识
http://www.cnblogs.com/wujy/tag/React%20Native/ React Native知识12-与原生交互 React Native知识11-Props(属性) ...