CodeForces 59C Title 模拟+回文

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.

Example
Input
3
a?c
Output
IMPOSSIBLE
Input
2
a??a
Output
abba
Input
2
?b?a
Output
abba


根据回文串的特性,先填补单个,然后再填补a  最后填补其他



#include <iostream> 
#include <cstdio>
#include <stdio.h>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define mod 1000000007
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pi  3.1415927
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=5e2+10;
using namespace std;
typedef  long long ll;
typedef  unsigned long long  ull; 
inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;
while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')
fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,
rx=getchar();return ra*fh;}
//#pragma comment(linker, "/STACK:102400000,102400000")
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}


int k;
string s;
int flag[100];
int main()
{	
	ios::sync_with_stdio(false);
	cin >> k>>s;
	
	int i,j;
	
	for(i=0,j=s.size()-1;i<=j;i++,j--)
	{
		if(s[i]!=s[j]&&s[i]!='?'&&s[j]!='?')
		{
			cout<<"IMPOSSIBLE"<<endl;
			return 0;
		}
	}
	
	int ctt=0;
	for(i=0,j=s.size()-1;i<=j;i++,j--)
	{
		if(s[i]==s[j]&&s[i]!='?')
		{
			flag[s[i]-'a']++;
		}
		else if(s[i]=='?'&&s[j]!='?')
		{
			s[i] = s[j];
			flag[s[j]-'a']++;
		}
		else if(s[i]!='?'&&s[j]=='?')
		{
			s[j] = s[i];
			flag[s[i]-'a']++;
		}
		else
		{
			ctt++;
		}
	}
	
	int ct = 0; 
	for(int i=0;i<k;i++)
	{
		if(flag[i]==0)
		{
			ct++;
		}
	}
	
	if(ctt<ct)
	{
		cout<<"IMPOSSIBLE"<<endl;
		return 0;
	}
	
	
	int aa = ctt-ct;
	for(i=0,j=s.size()-1;i<=j&&aa;i++,j--)
	{
		if(s[i]==s[j]&&s[i]=='?')
		{
			s[j]='a';
			s[i]=s[j];
			aa--;
		}
	}
	
	
	for(i=0,j=s.size()-1;i<=j;i++,j--)
	{
		if(s[i]==s[j]&&s[i]=='?')
		{
			for(int z=0;z<k;z++)
			{
				if(flag[z]==0)
				{
					s[j]='a'+z;
					s[i]=s[j];
					flag[z]++;
					break;
				}
			 } 
		}
	}
	
	cout<<s<<endl;
	return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值