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 ...
随机推荐
- 【MySQL案例】error.log的Warning:If a crash happens thisconfiguration does not guarantee that the relay lo(转)
标签: 1.1.1. If a crash happens thisconfiguration does not guarantee that the relay log info will be c ...
- 从零打造在线网盘系统之Struts2框架起步
欢迎浏览Java工程师SSH教程从零打造在线网盘系统系列教程,本系列教程将会使用SSH(Struts2+Spring+Hibernate)打造一个在线网盘系统,本系列教程是从零开始,所以会详细以及着重 ...
- PHP获取目录和的方法通过魔术变量;通过超级全局变量;通过相关函数等等:
<?php /** * PHP获取路径或目录实现 * @link http://www.phpddt.com */ //魔术变量,获取当前文件的绝对路径 echo "__FILE__: ...
- opencv学习笔记——minMaxIdx函数的含义及用法
opencv中有时需要对Mat数据需要对其中的数据求取最大值和最小值.opencv提供了直接的函数 CV_EXPORTS_W void minMaxLoc(InputArray src, CV_OUT ...
- Eclipse For Android 代码自动提示功能
Eclipse for android 实现代码自动提示智能提示功能,介绍 Eclipse for android 编辑器中实现两种主要文件 java 与 xml 代码自动提示功能,解决 eclips ...
- centos shell脚本编程2 if 判断 case判断 shell脚本中的循环 for while shell中的函数 break continue test 命令 第三十六节课
centos shell脚本编程2 if 判断 case判断 shell脚本中的循环 for while shell中的函数 break continue test 命令 ...
- java-mybaits-00602-查询缓存-mybatis整合ehcache
ehcache是一个分布式缓存框架.EhCache 是一个纯Java的进程内缓存框架,是一种广泛使用的开源Java分布式缓存,具有快速.精干等特点,是Hibernate中默认的CacheProvide ...
- EOS token 代币兑换的资料
eos token 兑换价格预估查询: https://eosscan.io/ https://steemit.com/eos/@sandwich/how-to-check-which-eos-p ...
- Openstack(十)部署nova服务(计算节点)
在计算节点安装 10.1安装nova计算服务 # 阿里云源详见2.3配置 # yum install openstack-nova-compute 10.2配置nova计算服务 10.2.1配置nov ...
- (转)二十三种设计模式及其python实现
本文源码寄方于github:https://github.com/w392807287/Design_pattern_of_python 参考文献: <大话设计模式>——吴强 <Py ...