C. Title
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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.

Output

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 < iaj = bj. |s| stands for the length of the given template.

Examples
input
3
a?c
output
IMPOSSIBLE
input
2
a??a
output
abba
input
2
?b?a
output
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 模拟的更多相关文章

  1. Codeforces 583 DIV2 Asphalting Roads 模拟

    原题链接:http://codeforces.com/problemset/problem/583/A 题意: 很迷很迷,表示没看懂..但是你看样例就秒懂了 题解: 照着样例模拟就好 代码: #inc ...

  2. Codeforces #180 div2 C Parity Game

    // Codeforces #180 div2 C Parity Game // // 这个问题的意思被摄物体没有解释 // // 这个主题是如此的狠一点(对我来说,),不多说了这 // // 解决问 ...

  3. 【Codeforces #312 div2 A】Lala Land and Apple Trees

    # [Codeforces #312 div2 A]Lala Land and Apple Trees 首先,此题的大意是在一条坐标轴上,有\(n\)个点,每个点的权值为\(a_{i}\),第一次从原 ...

  4. [codeforces 55]D. Beautiful numbers

    [codeforces 55]D. Beautiful numbers 试题描述 Volodya is an odd boy and his taste is strange as well. It ...

  5. Codeforces #541 (Div2) - E. String Multiplication(动态规划)

    Problem   Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...

  6. Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)

    Problem   Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...

  7. Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)

    Problem   Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...

  8. Codeforces #548 (Div2) - D.Steps to One(概率dp+数论)

    Problem   Codeforces #548 (Div2) - D.Steps to One Time Limit: 2000 mSec Problem Description Input Th ...

  9. CodeForces.158A Next Round (水模拟)

    CodeForces.158A Next Round (水模拟) 题意分析 校赛水题的英文版,坑点就是要求为正数. 代码总览 #include <iostream> #include &l ...

随机推荐

  1. mysql常见的错误码

    Mysql错误代码 Mysql错误代码分为两部分,老版本一部分,4.1版本为新的部分 第一部分: mysql的出错代码表,根据mysql的头文件mysql/include/mysqld_error.h ...

  2. Jenkins持续集成【简介】

    1.1 目前主流网站部署的流程    目前主流网站部署方法,通过Hudson/Jenkins工具平台实现全自动部署+测试,是一个可扩展的持续集成引擎,是一个开源软件项目,旨在提供一个开放易用的软件平台 ...

  3. Day27-28 基础加强

    day28基础加强 今日内容 泛型 注解 Servlet3.0 动态代理 类加载器   泛型 回顾泛型类 泛型类:具有一个或多个泛型变量的类被称之为泛型类. public class A<T&g ...

  4. matplotlib-曲线和折线案例

    matplotlib-曲线和折线案例 import matplotlib.pyplot as plt import numpy as np x = np.linspace(-5, 5, 100) pr ...

  5. 一个C#读写Dxf的类库DXFLibrary

    https://github.com/Titifonky/DXFLibrary DXF 参考手册: http://docs.autodesk.com/ACD/2011/CHS/filesDXF/WSf ...

  6. HDFS的工作流程分析

    HDFS的工作机制 概述 HDFS集群分为两大角色:NameNode.DataNode NameNode负责管理整个文件系统的元数据 DataNode 负责管理用户的文件数据块 文件会按照固定的大小( ...

  7. 跟我学Makefile(六)

    shell 函数 :和反引号“`”是相同的功能 . shell 函数把执行操作系统命令后的输出作为函数返回. contents := $(shell cat foo) files := $(shell ...

  8. python学习之路-day10

    一.什么是线程 在传统操作系统中,每个进程有一个地址空间,而且默认就有一个控制线程. 线程顾名思义,就是一条流水线工作的过程,一条流水线必须属于一个车间,一个车间的工作过程是一个进程. 车间负责把资源 ...

  9. C语言——stdio.h

        int fgetc(FILE * stream); get character from stream 返回流中的一个字符,并以int的类型返回,如果碰到文件的结尾,或者一个错误发生,函数返回 ...

  10. la5135 无向图 点-双连通 运用

    大白书 P314 #include <iostream> #include <algorithm> #include <string.h> #include < ...